lib/finite_machine/dsl.rb in finite_machine-0.10.0 vs lib/finite_machine/dsl.rb in finite_machine-0.10.1
- old
+ new
@@ -20,13 +20,16 @@
def initialize(machine, attrs = {})
self.attrs = attrs
self.machine = machine
end
+ # Delegate attributes to machine instance
+ #
+ # @api private
def method_missing(method_name, *args, &block)
- if @machine.respond_to?(method_name)
- @machine.send(method_name, *args, &block)
+ if machine.respond_to?(method_name)
+ machine.send(method_name, *args, &block)
else
super
end
end
@@ -86,20 +89,20 @@
# @api public
def initial(value, options = {})
state = (value && !value.is_a?(Hash)) ? value : raise_missing_state
name, self.defer, silent = parse(options)
self.initial_event = name
- machine.event(name, FiniteMachine::DEFAULT_STATE => state, silent: silent)
+ event(name, FiniteMachine::DEFAULT_STATE => state, silent: silent)
end
# Trigger initial event
#
# @return [nil]
#
# @api private
def trigger_init
- machine.send(:"#{initial_event}") unless defer
+ public_send(:"#{initial_event}") unless defer
end
# Attach state machine to an object
#
# This allows state machine to initiate events in the context
@@ -115,13 +118,13 @@
# @return [FiniteMachine::StateMachine]
#
# @api public
def target(object = nil)
if object.nil?
- machine.env.target
+ env.target
else
- machine.env.target = object
+ env.target = object
end
end
# Use alternative name for target
#
@@ -139,11 +142,11 @@
#
# @return [FiniteMachine::StateMachine]
#
# @api public
def alias_target(alias_name)
- machine.env.aliases << alias_name.to_sym
+ env.aliases << alias_name.to_sym
end
# Define terminal state
#
# @example
@@ -151,11 +154,11 @@
#
# @return [FiniteMachine::StateMachine]
#
# @api public
def terminal(*values)
- machine.final_state = values
+ self.final_state = values
end
# Define state machine events
#
# @example
@@ -165,11 +168,11 @@
#
# @return [FiniteMachine::StateMachine]
#
# @api public
def events(&block)
- machine.events_dsl.call(&block)
+ events_dsl.call(&block)
end
# Define state machine callbacks
#
# @example
@@ -179,25 +182,25 @@
#
# @return [FiniteMachine::Observer]
#
# @api public
def callbacks(&block)
- machine.observer.call(&block)
+ observer.call(&block)
end
# Error handler that throws exception when machine is in illegal state
#
# @api public
def handlers(&block)
- machine.errors.call(&block)
+ errors.call(&block)
end
# Decide whether to log transitions
#
# @api public
def log_transitions(value)
- machine.log_transitions = value
+ self.log_transitions = value
end
private
# Initialize state machine properties based off attributes
@@ -254,10 +257,10 @@
# @api public
def event(name, attrs = {}, &block)
sync_exclusive do
attributes = attrs.merge!(name: name)
if block_given?
- merger = ChoiceMerger.new(self, attributes)
+ merger = ChoiceMerger.new(machine, attributes)
merger.instance_eval(&block)
else
transition_builder = TransitionBuilder.new(machine, attributes)
transition_builder.call(attrs)
end