Skip to content

Instantly share code, notes, and snippets.

@methodin
methodin / Mongo
Created February 1, 2011 15:20
Useful commands/examples for Mongo
// Adding a user auth to a DB (read/write, add true as a third param for read-only)
use somedb
db.addUser("username","pasword")
// Update an entry (don't forget $set or it will be replace only with what is provided)
db.gadgets.update({key:"Some criteria"},{$set:{weight:17.6}})
// Dump a database/collection
mongodump -d databasename -c collection -o .
@methodin
methodin / common.php
Created February 1, 2011 15:24
Common functions
<?php
// Log to a file
function dump($data,$file='/var/log/mylog.log')
{
file_put_contents($file, $data."\n", FILE_APPEND);
}
// Use some locking for a cron job
function lockit($name)
{
@methodin
methodin / rememberme.php
Created March 3, 2011 04:05
Remember me for PHP (& Mongo)
define('REMEMBER_TIMEOUT',time()+60*60*24*2);
define('REMEMBER_SALT','secretword');
define('REMEMBER_COOKIE','some_cookie_name');
function remember_me($set=false, $username='')
{
$db = mongo();
// Set the cookie and add the db record
if($set)
@methodin
methodin / jquery.background-noise.js
Created March 4, 2011 02:42
jQuery background-noise
jQuery.fn.noisy = function(opacity) {
if (typeof(opacity) === 'undefined') {
opacity = 0.1;
}
var wrapper = jQuery(this).wrapInner('<div />').children();
var canvas = document.createElement("canvas");
canvas.width = 100;
canvas.height = 100;
var ctx = canvas.getContext("2d");
@methodin
methodin / androidTest.java
Created April 30, 2011 17:42
Toast handler for Android (avoids contextual errors)
// Need a handler for toast
public void makeToast(String str) {
Message status = toaster.obtainMessage();
Bundle datax = new Bundle();
datax.putString("msg", str);
status.setData(datax);
toaster.sendMessage(status);
}
public Handler toaster = new Handler(){
@methodin
methodin / DB.java
Created October 3, 2011 01:36
General database handler for Android
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
@methodin
methodin / cron.coffee
Created November 30, 2011 03:31
Run function for a Node cron job
require './schema.js'
mongoose = require "mongoose"
mongodb = require "mongodb"
sys = require 'sys'
lock = require './lock.js'
run = require './run.js'
# Sample cron
# * * * * * /usr/local/bin/node /var/some/dir/cron.js > /var/log/cronjs.log
run.attempt('every 1 minute', () ->
@methodin
methodin / dataMap.js
Created December 13, 2011 00:33
The simplest jQuery plugin I ever wrote - map a JSON object to a container of DOM elements
/*
HTML:
<div id="someParent">
<div id="title"></div>
<img id="img"/>
</div>
Data:
var r = {title:'This is a title', img:'http://someimage.jpg'}
@methodin
methodin / binarySearch.js
Created December 28, 2011 02:52
Binary Search in JS
function binarySearch(searchFor, arr, index1, index2) {
if(index1 === undefined) index1 = 0;
if(index2 === undefined) index2 = arr.length-1;
var search = index1+Math.floor((index2-index1)/2);
if(arr[search] == searchFor) return true;
else if(index2-index1 <= 1) return false;
else return binarySearch(searchFor, arr, arr[search] > searchFor ? index1 : search, arr[search] > searchFor ? search : index2);
}
var found = binarySearch(4, [1,3,6,9,12,17,25,44]);
@methodin
methodin / fizzBuzz.js
Created December 28, 2011 03:08
Fizz Buzz
var str = '';
for(var i=1;i<=100;i++) {
var rep = '';
if(i % 3 == 0) rep = 'Fizz';
if(i % 5 == 0) rep += 'Buzz';
str += (rep||i)+', ';
}