Created
January 9, 2010 02:35
-
-
Save jpignata/272675 to your computer and use it in GitHub Desktop.
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 Object | |
def attribute(attribute, &block) | |
attribute, default_value = attribute.to_a.first if attribute.is_a?(Hash) | |
define_method(:"#{attribute}") do | |
if instance_variables.include?("@#{attribute}") | |
instance_variable_get("@#{attribute}") | |
elsif block_given? | |
instance_eval(&block) | |
else | |
default_value | |
end | |
end | |
define_method(:"#{attribute}?") { !!send(attribute.to_sym) } | |
define_method(:"#{attribute}=") { |args| instance_variable_set("@#{attribute}", args) } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment