Created
March 1, 2012 03:15
-
-
Save Nowaker/1946964 to your computer and use it in GitHub Desktop.
attr_reader and attr_writer on class instance
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 Magic | |
def property types, method_name, property_name = nil | |
property_name = method_name if property_name.nil? | |
# reader | |
self.class.send :define_method, method_name do | |
value = read_value property_name | |
raise unless types.any? do |type| | |
value.is_a? type | |
end | |
value | |
end | |
# writer | |
self.class.send :define_method, method_name + '=' do |value| | |
raise unless types.any? do |type| | |
value.is_a? type | |
end | |
change_value property_name, value | |
end | |
end | |
property [String, Integer], :online_mode | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment