Skip to content

Instantly share code, notes, and snippets.

@rmulligan
Last active August 29, 2015 13:56
Show Gist options
  • Save rmulligan/9203762 to your computer and use it in GitHub Desktop.
Save rmulligan/9203762 to your computer and use it in GitHub Desktop.
Dynamic Dispatch on Eigenclass for Regex matched callers
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