Skip to content

Instantly share code, notes, and snippets.

@danshultz
danshultz / controller.rb
Created November 5, 2012 22:30
Thoughts on Decorator
class SomeController < AC::Base
# defines the general view model along with a way to introduce the state and pre-scope models
view_model = MyGeneralViewModel do |state|
user = get_user
state.user = user
state.scope = Model.where(:user => user)
end
# interact with the view model just like an active record model
# scopes would be present and things would feel like AR
@danshultz
danshultz / page_range.py
Created November 2, 2012 17:41
Python Number List to Text Ranges
# Ref from http://stackoverflow.com/questions/2361945/detecting-consecutive-integers-in-a-list
from itertools import groupby
from operator import itemgetter
class PageRange(object):
@staticmethod
def to_text_range(array_of_numbers):
text_buffer = []
@danshultz
danshultz / nil_patch.rb
Created October 19, 2012 01:17
ActiveRecord nil patch
def nil.method_missing(method, *args)
(@@activeRecordClasses ||= ObjectSpace.each_object(Class).find_all { |c|
c.respond_to?(:ancestors) && c != ActiveRecord::Base && c.ancestors.include?(ActiveRecord::Base)
}).choice.order("rand()").first || self.method_missing(method, *args)
rescue
self.method_missing(method, *args)
end
@danshultz
danshultz / liquid_view.rb
Created June 12, 2012 03:11
liquid/lib/extras/liquid_view.rb file to use in Rails 3 with layouts
# LiquidView is a action view extension class. You can register it with rails
# and use liquid as an template system for .liquid files
#
# Example
#
# ActionView::Base::register_template_handler :liquid, LiquidView
class LiquidView
PROTECTED_ASSIGNS = %w( template_root response _session template_class action_name request_origin session template
_response url _request _cookies variables_added _flash params _headers request cookies
ignore_missing_templates flash _params logger before_filter_chain_aborted headers )
@danshultz
danshultz / problem67.coffee
Created June 5, 2012 14:30
Project Euler Problem 67 Solution in Multiple Languages
fs = require('fs')
triangle = fs.readFileSync('../triangle.txt', 'utf-8')
.split('\r\n')
.map((x) -> x.split(' ').map((y) -> +y))
.reverse()
.splice(1)
reduction = (prev, current) ->
current.map (x, i) ->
@danshultz
danshultz / explanation.md
Created May 7, 2012 19:00
mocha.js -r --require and custom assertion libraries

Explaination

I have been using mocha for a little while with the "should" assertion library on the node side and chai in the web browser. I determined I wanted to use chai for my server side js testing as well but I didn't want to require chai explicitly at the top of each file so I looked at the --require parameter for mocha and determined that I could leverage this to setup my testing framework as well as any custom matchers/other framework uils

The command I now use to run mocha is mocha -r ./test/support/setup test/my_stuff.test.js

In doing this - you no longer have to explicitly register chai/sinon

Thoughts? Good idea/bad idea - gotchas that I'm unaware of thus far?

{promise} = require('model_promise')
render: (discussion) ->
# render stuff
change: (discussion_id) ->
@discussion_id = discussion_id
$.when(promise(Discussion, discussion_id)).then =>
@render(Discussion.find(discussion_id))
window.require.define("app/controllers/house_controller": function(exports, require, module) {
var HouseController = function(house) {
//initializer
this.house = house;
};
HouseController.prototype.render = function() {
//rendering...
};
@danshultz
danshultz / index.html
Created April 18, 2012 00:41
"Stitch" solution
<html>
<header>
<script type="text/javascript" src="stitch_header.js"></script>
<script type="text/javascript" src="my-module/name.js"></script>
<script type="text/javascript">
var Name = window.require("my-module/name");
window.nameApp = new Name("My App");
</script>
</header>
<body></body>
@danshultz
danshultz / gist:1683709
Created January 26, 2012 16:44
Find number of commits after a commit
git log |egrep "^commit "|sed -n "1,/f3ec962800287e8078f56b77860d4e96be56ec71/ p" | wc -l