Last active
August 29, 2015 13:56
-
-
Save rmulligan/9203762 to your computer and use it in GitHub Desktop.
Dynamic Dispatch on Eigenclass for Regex matched callers
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
def method_missing(name, *args) | |
if name =~ /^get_stores_by_/ | |
eigenclass = class << self; self; end # flatten scope | |
eigenclass.class_eval do | |
define_method(name) do |*args| | |
query = "#{@base_url}/store/#{name.to_s.camelize(:lower)}.json?" | |
args[0].collect do |key, value| | |
if value.is_a? Array | |
value.each {|v| query << "#{key.to_s.camelize(:lower)}=#{v.to_s}&"} | |
else | |
query << "#{key.to_s.camelize(:lower)}=#{value.to_s}&" | |
end | |
end | |
execute_request(URI::escape(query.chomp("&"))) | |
end | |
end | |
self.send(name, *args) | |
else | |
super | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment