lib/finite_machine/dsl.rb in finite_machine-0.13.0 vs lib/finite_machine/dsl.rb in finite_machine-0.14.0
- old
+ new
@@ -1,10 +1,10 @@
# frozen_string_literal: true
-require_relative 'choice_merger'
-require_relative 'safety'
-require_relative 'transition_builder'
+require_relative "choice_merger"
+require_relative "safety"
+require_relative "transition_builder"
module FiniteMachine
# A generic DSL for describing the state machine
class GenericDSL
# Initialize a generic DSL
@@ -36,10 +36,17 @@
else
super
end
end
+ # Check if message can be handled by this DSL
+ #
+ # @api private
+ def respond_to_missing?(method_name, include_private = false)
+ @machine.respond_to?(method_name) || super
+ end
+
# Configure state machine properties
#
# @api private
def call(&block)
instance_eval(&block)
@@ -63,10 +70,33 @@
initial(@attrs[:initial]) if @attrs[:initial]
terminal(@attrs[:terminal]) if @attrs[:terminal]
log_transitions(@attrs.fetch(:log_transitions, false))
end
+ # Add aliases for the target object
+ #
+ # @example
+ # FiniteMachine.define do
+ # target_alias :engine
+ #
+ # on_transition do |event|
+ # engine.state = event.to
+ # end
+ # end
+ #
+ # @param [Array<Symbol>] aliases
+ # the names for target alias
+ #
+ # @api public
+ def alias_target(*aliases)
+ aliases.each do |alias_name|
+ next if env.aliases.include?(alias_name)
+
+ env.aliases << alias_name
+ end
+ end
+
# Define initial state
#
# @param [Symbol] value
# The initial state name.
# @param [Hash[Symbol]] options
@@ -195,10 +225,10 @@
#
# @return [nil]
#
# @api private
def raise_missing_state
- fail MissingInitialStateError,
- 'Provide state to transition :to for the initial event'
+ raise MissingInitialStateError,
+ "Provide state to transition :to for the initial event"
end
end # DSL
end # FiniteMachine