Skip to content

Instantly share code, notes, and snippets.

@donpdonp
Created September 14, 2009 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donpdonp/186916 to your computer and use it in GitHub Desktop.
Save donpdonp/186916 to your computer and use it in GitHub Desktop.
module States
STATES = %w(cancelled declined passive active approved pending flagged complete)
attr_accessor :state # remove this if using an activerecord column
STATES.each do |state|
const_set(state.upcase, state)
define_method(state+'?') do
self.state == state
end
end
end
irb(main):017:0> class A; include States; end
irb(main):018:0> a=A.new; a.state = States::CANCELLED
=> "cancelled"
irb(main):019:0> a.cancelled?
=> true
irb(main):020:0> a.declined?
=> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment