Created
October 17, 2010 14:38
-
-
Save fujimura/630910 to your computer and use it in GitHub Desktop.
enables jquery-like dynamic function adding. ignores closure like 'this' in js.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_support/all' | |
class HashWithJavaScriptishAccess < Hash | |
def method_missing(name, *args) | |
if name.to_s =~ /=$/ | |
name_to_be_set = name.to_s.gsub(/=$/, '').to_sym | |
self[name_to_be_set] = args.first | |
else | |
self[name] || super | |
end | |
end | |
def curry(name, *args) | |
fn = self[name] | |
lambda{fn.bind(self).call *args} | |
end | |
end | |
module JQuerish | |
def fn | |
@functions ||= HashWithJavaScriptishAccess.new | |
end | |
def method_missing(name, *args) | |
if @functions.keys.include? name | |
@functions[name].bind(self).call(*args) | |
else | |
super | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment