Skip to content

Instantly share code, notes, and snippets.

@donpdonp
Forked from Hates/gist:186893
Created September 14, 2009 20:07
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/186896 to your computer and use it in GitHub Desktop.
Save donpdonp/186896 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
def self.included(mod)
STATES.each do |state|
up_state = state.upcase
mod.const_set(up_state, state)
mod.send(:define_method, state+'?') do
self.state == state
end
end
end
end
irb(main):017:0> class A; include States; end
irb(main):018:0> a=A.new; a.state = A::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