lib/stateful_enum/machine.rb in stateful_enum-0.6.0 vs lib/stateful_enum/machine.rb in stateful_enum-0.7.0
- old
+ new
@@ -3,11 +3,11 @@
module StatefulEnum
class Machine
attr_reader :events
def initialize(model, column, states, prefix, suffix, &block)
- @model, @column, @states, @events = model, column, states, []
+ @model, @column, @states, @events = model, column, states.map(&:to_sym), []
@prefix = if prefix
prefix == true ? "#{column}_" : "#{prefix}_"
end
@suffix = if suffix
suffix == true ? "_#{column}" : "_#{suffix}"
@@ -25,11 +25,11 @@
raise ArgumentError, "event: :#{name} has already been defined." if @events.map(&:name).include? name
@events << Event.new(@model, @column, @states, @prefix, @suffix, name, &block)
end
class Event
- attr_reader :name, :value_method_name
+ attr_reader :name, :value_method_name, :transitions
def initialize(model, column, states, prefix, suffix, name, &block)
@states, @name, @transitions, @before, @after = states, name, {}, [], []
instance_eval(&block) if block
@@ -52,11 +52,11 @@
end
define_method value_method_name do
to, condition = transitions[send(column).to_sym]
#TODO better error
- if to && (!condition || instance_exec(&condition))
+ if to && (condition.nil? || instance_exec(&condition))
#TODO transaction?
run_callbacks value_method_name do
original_method = self.class.send(:_enum_methods_module).instance_method "#{prefix}#{to}#{suffix}!"
original_method.bind(self).call
end
@@ -75,10 +75,10 @@
detect_enum_conflict! column, "can_#{value_method_name}?"
define_method "can_#{value_method_name}?" do
state = send(column).to_sym
return false unless transitions.key? state
_to, condition = transitions[state]
- !condition || instance_exec(&condition)
+ condition.nil? || instance_exec(&condition)
end
# def assign_transition()
detect_enum_conflict! column, "#{value_method_name}_transition"
define_method "#{value_method_name}_transition" do