Skip to content

Instantly share code, notes, and snippets.

View bunnymatic's full-sized avatar
💭
writing computer programs

Mr Rogers bunnymatic

💭
writing computer programs
View GitHub Profile
@bunnymatic
bunnymatic / query_string_parser.js
Created November 17, 2012 22:16
simple querystring parser in javascript using the DOM
var QueryStringParser;
QueryStringParser = (function() {
function QueryStringParser(url) {
var parser, _that;
this.query_params = {};
if (!document || !document.createElement) {
throw 'This needs to be run in an HTML context with a document.';
}
@bunnymatic
bunnymatic / gist:4050373
Created November 10, 2012 08:03
chunk a string into equal length parts
class String
def chunk(ct)
result = []
dup = self.dup
val = dup.slice!(0..(ct-1))
until val.empty? || !val do
result << val
val = dup.slice!(0..(ct-1))
end
result
@bunnymatic
bunnymatic / admin_form_builder.rb
Created November 1, 2012 02:38
Rails Form Helpers for Bootstrap
# in app/helpers/admin_form_builder.rb
class AdminFormBuilder < ActionView::Helpers::FormBuilder
####
# To control classes on fields, pass in the following in your options hash
# :row_class => 'whatever' : this will add a class 'whatever' to the field wrapper (which is a row)
# :input_html => {:class => 'whatever'} : this will add all these elements to the input/textarea tag as attributes
# Note: if you use the field_row method, this will be ignored
#
def file_input_row(field, hint, opts = {})
@bunnymatic
bunnymatic / jasmine_json_fixtures.js
Created August 31, 2012 23:40
Jasmine Load JSON fixtures
/**
add ability to load json fixtures into jasmine
**/
var readJsonFixtures = function() {
return jasmine.getJsonFixtures().proxyCallTo_('read', arguments);
};
var preloadJsonFixtures = function() {
jasmine.getJsonFixtures().proxyCallTo_('preload', arguments);
@bunnymatic
bunnymatic / jquery.my_plugin.js.coffee
Last active October 9, 2015 06:48
jquery plugin boilerplate in coffeescript Javascript version is here https://gist.github.com/bunnymatic/1048060
$.myPluginDefaults =
param1: 'default param1'
param2: []
$.fn.myPlugin = (method) ->
inArgs = arguments
methods =
init: (options) ->
localSettings = $.extend({},$.myPluginDefaults, options);
@bunnymatic
bunnymatic / upto.rb
Created August 20, 2012 03:32
Trim an array to the next largest value given a value. Potentially useful for pagination needs.
def upto(arr,v); arr[0..arr.select{|x| x < v}.count] end
# usage
arr = 5.times.map{|x| 12 * (2**x)}
maxval = 26
upto(arr,maxval)
# => [12,24,48]
@bunnymatic
bunnymatic / query_string_parser.js.coffee
Created July 27, 2012 16:53
simple url/query string parser coffeescript
class QueryStringParser
constructor: (url) ->
@query_params = {}
if !document || !document.createElement
throw 'This needs to be run in an HTML context with a document.'
parser = document.createElement('a')
parser.href = url
@url = url
if (parser.origin)
@origin = parser.origin
@bunnymatic
bunnymatic / jasmine-prototype-event-helpers
Created July 15, 2012 00:48
Jasmine Helpers to fire events with Prototype
var jasmine = jasmine || {}
jasmine.getEvents = function(sel, event_name) {
var events = [];
try {
var evs = $$(sel)[0].getStorage().get('prototype_event_registry');
events = evs.get(event_name);
}
catch(e) {}
return events;
};
@bunnymatic
bunnymatic / console.rb
Created May 25, 2012 21:20
Sinatra console
#!/usr/bin/env ruby
require 'data_mapper'
require 'sinatra'
require 'sinatra/config_file'
disable :run
root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
Dir[File.join(root,"{lib,models}/**/*.rb")].each do |file|
require file
end
@bunnymatic
bunnymatic / prototype_extension_data.js
Created February 10, 2012 23:04
give prototype the .data() method like jQuery
var PrototypeExtensions = {
data: function(elem, key, val) {
var DATA_REGEX = /data-(\w+)/;
var ii = 0;
var nattr = elem.attributes.length;
if (key && val) {
elem.setAttribute('data-' + key, val);
}
else {
for (; ii < nattr; ++ii ) {