Skip to content

Instantly share code, notes, and snippets.

View stephank's full-sized avatar

Stéphan Kochen stephank

View GitHub Profile
{puts} = require 'sys'
MyClass = prototype ~>
@doSomething = -> 3 + 5
foobar = new MyClass()
puts foobar.doSomething()
AnotherClass = prototype inherits MyClass ~>
constructor = -> @abc = 3
# Reverse apply.
Object::apply = (func, args) -> func.apply this, args
# Base model class.
class Model
has_many: (what) ->
@[what] = ->
how_many = @["number_of_#{what}"]
console.log "I have #{how_many} shiney #{what}!"
class Model
# Method used in a class context.
@has_many = (names) ->
# Define the accessor method.
@[names] = ->
# Retrieve stuff from table 'names'
class Account extends Model
@has_many 'emails'
# A monkey-patch for ActionPack, which takes into account a POST's encoding.
# Browsers may specify this using a hidden _charset_ field, or in the Content-Type.
# Briefly tested on Rails 2.3.5 and Ruby 1.9.1p376.
class ActionController::Request < Rack::Request
# This is copied from Rack::Request. Rails overrides content_type, which the original relies on,
# thus breaking it, unfortunately. Instead, this version uses @env['CONTENT_TYPE'].
def media_type_params
return {} if @env['CONTENT_TYPE'].nil?
@env['CONTENT_TYPE'].split(/\s*[;,]\s*/)[1..-1].