Created
May 5, 2013 18:50
-
-
Save christopherslee/5521757 to your computer and use it in GitHub Desktop.
Introduction to Metaprogramming: Lesson 4
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
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