0
@@ -15,10 +15,11 @@ module ScottBarron #:nodoc:
0
NOOP = lambda { |o| true }
0
+ attr_reader :name
, :value0
def initialize(name, options)
0
+ @value = (options[:value] || @name).to_s
0
@after = Array(options[:after])
0
@enter = options[:enter] || NOOP
0
@exit = options[:exit] || NOOP
0
@@ -40,9 +41,11 @@ module ScottBarron #:nodoc:
0
attr_reader :from, :to, :opts
0
- @from, @to, @guard = opts[:from], opts[:to], opts[:guard]
0
+ def initialize(options)
0
+ @from = options[:from].to_s
0
+ @to = options[:to].to_s
0
+ @guard = options[:guard] || NOOP
0
@@ -51,14 +54,14 @@ module ScottBarron #:nodoc:
0
return false unless guard(record)
0
- loopback = record.current_state
== to
0
+ loopback = record.current_state
.to_s == to
0
states = record.class.read_inheritable_attribute(:states)
0
next_state = states[to]
0
- old_state = states[record.current_state
]
0
+ old_state = states[record.current_state
.to_s]
0
next_state.entering(record) unless loopback
0
- record.update_attribute(record.class.state_column,
to.to_s)
0
+ record.update_attribute(record.class.state_column,
next_state.value)
0
next_state.entered(record) unless loopback
0
old_state.exited(record) unless loopback
0
@@ -86,7 +89,7 @@ module ScottBarron #:nodoc:
0
def next_states(record)
0
- @transitions.select { |t| t.from == record.current_state
}
0
+ @transitions.select { |t| t.from == record.current_state
.to_s }
0
@@ -138,7 +141,7 @@ module ScottBarron #:nodoc:
0
def run_initial_state_actions
0
- initial = self.class.read_inheritable_attribute(:states)[self.class.initial_state.to_s
ym]
0
+ initial = self.class.read_inheritable_attribute(:states)[self.class.initial_state.to_s
]
0
@@ -151,12 +154,12 @@ module ScottBarron #:nodoc:
0
# Returns what the next state for a given event would be, as a Ruby symbol.
0
def next_state_for_event(event)
0
ns = next_states_for_event(event)
0
- ns.empty? ? nil : ns.first.to
0
+ ns.empty? ? nil : ns.first.to
.to_sym0
def next_states_for_event(event)
0
self.class.read_inheritable_attribute(:transition_table)[event.to_sym].select do |s|
0
- s.from == current_state
0
+ s.from == current_state
.to_s0
@@ -169,7 +172,7 @@ module ScottBarron #:nodoc:
0
# Returns an array of all known states.
0
- read_inheritable_attribute(:states).keys
0
+ read_inheritable_attribute(:states).keys
.collect { |state| state.to_sym }0
# Define an event. This takes a block which describes all valid transitions
0
@@ -217,9 +220,9 @@ module ScottBarron #:nodoc:
0
def state(name, opts={})
0
state = SupportingClasses::State.new(name, opts)
0
- write_inheritable_hash(:states,
name.to_sym => state)
0
+ write_inheritable_hash(:states,
state.value => state)
0
- define_method("#{state.name}?") { current_state
== state.name }
0
+ define_method("#{state.name}?") { current_state
.to_s == state.value }
0
# Wraps ActiveRecord::Base.find to conveniently find all records in
0
@@ -258,7 +261,7 @@ module ScottBarron #:nodoc:
0
def with_state_scope(state)
0
- raise InvalidState unless states.include?(state
)
0
+ raise InvalidState unless states.include?(state
.to_sym)
0
with_scope :find => {:conditions => ["#{table_name}.#{state_column} = ?", state.to_s]} do
Comments
No one has commented yet.