-
-
Save donpdonp/186896 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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