lib/finite_machine/dsl.rb in finite_machine-0.7.1 vs lib/finite_machine/dsl.rb in finite_machine-0.8.0

- old
+ new

@@ -65,13 +65,13 @@ # # @return [StateMachine] # # @api public def initial(value) - state, name, self.defer = parse(value) - self.initial_event = name - machine.event(name, from: FiniteMachine::DEFAULT_STATE, to: state) + state, name, self.defer, silent = parse(value) + self.initial_event = name + machine.event(name, FiniteMachine::DEFAULT_STATE => state, silent: silent) end # Trigger initial event # # @return [nil] @@ -156,16 +156,17 @@ # # @return [Array[Symbol,String]] # # @api private def parse(value) - unless value.is_a?(Hash) - [value, FiniteMachine::DEFAULT_EVENT_NAME, false] - else + if value.is_a?(Hash) [value.fetch(:state) { raise_missing_state }, value.fetch(:event) { FiniteMachine::DEFAULT_EVENT_NAME }, - value.fetch(:defer) { false }] + value.fetch(:defer) { false }, + value.fetch(:silent) { true }] + else + [value, FiniteMachine::DEFAULT_EVENT_NAME, false, true] end end # Raises missing state error # @@ -174,12 +175,12 @@ raise MissingInitialStateError, 'Provide state to transition :to for the initial event' end end # DSL + # A DSL for describing events class EventsDSL < GenericDSL - # Create event and associate transition # # @example # event :go, :green => :yellow # event :go, :green => :yellow, if: :lights_on? @@ -189,16 +190,22 @@ # @api public def event(name, attrs = {}, &block) sync_exclusive do attributes = attrs.merge!(name: name) FiniteMachine::StateParser.new(attrs).parse_states do |from, to| - attributes.merge!(parsed_states: { from => to }) - Transition.create(machine, attributes) + if block_given? + merger = ChoiceMerger.new(self, attributes) + merger.instance_eval(&block) + else + attributes.merge!(parsed_states: { from => to }) + Transition.create(machine, attributes) + end end end end end # EventsDSL + # A DSL for describing error conditions class ErrorsDSL < GenericDSL # Add error handler # # @param [Array] exceptions #