Skip to content

Instantly share code, notes, and snippets.

@christopherslee
Created May 5, 2013 18:50
Show Gist options
  • Save christopherslee/5521757 to your computer and use it in GitHub Desktop.
Save christopherslee/5521757 to your computer and use it in GitHub Desktop.
Introduction to Metaprogramming: Lesson 4
class Customer
attr_reader :id, :datasource
def initialize(id, datasource)
@id = id
@datasource = datasource
@datasource.methods.grep(/^get_(.*)_value/) { Customer.has_field $1 }
end
def self.has_field(fieldname)
define_method fieldname do
value = process datasource.send("get_#{fieldname}_value", id), datasource.send("get_#{fieldname}_datatype", id)
"#{fieldname.capitalize}: #{value}"
end
end
private
def process(value, type)
type.to_sym == :string ? "'#{value}'" : value
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment