Skip to content

Instantly share code, notes, and snippets.

@joshsusser
Created July 24, 2009 00:21
Show Gist options
  • Save joshsusser/153758 to your computer and use it in GitHub Desktop.
Save joshsusser/153758 to your computer and use it in GitHub Desktop.
### meta magic boolean attributes
def self.attr_boolean(attr_name)
class_eval(<<-CODE, __FILE__, __LINE__)
def #{attr_name}=(#{attr_name})
@#{attr_name} = !!(/^1|true$/i =~ #{attr_name}) # yes, we need a boolean
end
def #{attr_name}
@#{attr_name}
end
alias_method :#{attr_name}?, :#{attr_name}
CODE
end
attr_boolean :is_awesome
attr_boolean :has_mutant_powers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment