Skip to content

Instantly share code, notes, and snippets.

@kpumuk
Created January 25, 2010 10:32
Show Gist options
  • Save kpumuk/285776 to your computer and use it in GitHub Desktop.
Save kpumuk/285776 to your computer and use it in GitHub Desktop.
class Module
def name_finder(*methods)
methods.each do |method|
raise TypeError.new("method name is not symbol") unless method.is_a?(Symbol)
metaclass = class << self; self; end
metaclass.send(:define_method, method) do
instance_variable_get(self.find_by_name(method.to_s))
end
end
end
end
class Type
set_table_name 'types'
name_finder :mail, :phone, :sms
end
Type.mail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment