micromachine 1.2.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/examples/advanced.rb +10 -1
 - data/examples/basic.rb +1 -1
 - data/examples/callbacks.rb +1 -1
 - data/lib/micromachine.rb +13 -18
 - data/micromachine.gemspec +1 -1
 - data/test/introspection.rb +0 -8
 - metadata +2 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: a1e4cf4c889a6b914e2b7f2e6e94e14b6ca5e97f
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 7c27986b29e696f35424ae9c31a4dbf1307bf63b
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: be3ccf1c76220287fe4897368d10d1bfaead7adbe62b966912b54ff8660f2cc63d96536978c66358de841d2a7a8c16fdbac71cd01f69e4a4f941547128972c0c
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: eae3a5f96d87b1075b30c4de5c59d6c205ca39a066e7449b88791c14b1edc53526a87340b8c3b908a10fd4c38fc3e4861bd273ab1882c05c9119b56acfc4214f
         
     | 
    
        data/examples/advanced.rb
    CHANGED
    
    | 
         @@ -1,6 +1,6 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            require 'micromachine'
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
     | 
    
         
            -
            # This example can be run with ruby -I lib/  
     | 
| 
      
 3 
     | 
    
         
            +
            # This example can be run with ruby -I lib/ ./examples/advanced.rb
         
     | 
| 
       4 
4 
     | 
    
         | 
| 
       5 
5 
     | 
    
         
             
            fsm = MicroMachine.new(:pending)
         
     | 
| 
       6 
6 
     | 
    
         | 
| 
         @@ -21,3 +21,12 @@ fsm.trigger(:ignore) 
     | 
|
| 
       21 
21 
     | 
    
         
             
            fsm.trigger(:reset)
         
     | 
| 
       22 
22 
     | 
    
         | 
| 
       23 
23 
     | 
    
         
             
            fsm.trigger(:ignore)
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
            puts "Should print all states: pending, confirmed, ignored"
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
            puts fsm.states.join ", "
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
            puts "Should print all events: confirm, ignore, reset"
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
            puts fsm.events.join ", "
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
    
        data/examples/basic.rb
    CHANGED
    
    
    
        data/examples/callbacks.rb
    CHANGED
    
    
    
        data/lib/micromachine.rb
    CHANGED
    
    | 
         @@ -2,8 +2,8 @@ class MicroMachine 
     | 
|
| 
       2 
2 
     | 
    
         
             
              InvalidEvent = Class.new(NoMethodError)
         
     | 
| 
       3 
3 
     | 
    
         
             
              InvalidState = Class.new(ArgumentError)
         
     | 
| 
       4 
4 
     | 
    
         | 
| 
       5 
     | 
    
         
            -
               
     | 
| 
       6 
     | 
    
         
            -
               
     | 
| 
      
 5 
     | 
    
         
            +
              attr_reader :transitions_for
         
     | 
| 
      
 6 
     | 
    
         
            +
              attr_reader :state
         
     | 
| 
       7 
7 
     | 
    
         | 
| 
       8 
8 
     | 
    
         
             
              def initialize(initial_state)
         
     | 
| 
       9 
9 
     | 
    
         
             
                @state = initial_state
         
     | 
| 
         @@ -20,27 +20,17 @@ class MicroMachine 
     | 
|
| 
       20 
20 
     | 
    
         
             
              end
         
     | 
| 
       21 
21 
     | 
    
         | 
| 
       22 
22 
     | 
    
         
             
              def trigger(event)
         
     | 
| 
       23 
     | 
    
         
            -
                 
     | 
| 
       24 
     | 
    
         
            -
                  @state = transitions_for[event][@state]
         
     | 
| 
       25 
     | 
    
         
            -
                  callbacks = @callbacks[@state] + @callbacks[:any]
         
     | 
| 
       26 
     | 
    
         
            -
                  callbacks.each { |callback| callback.call(event) }
         
     | 
| 
       27 
     | 
    
         
            -
                  true
         
     | 
| 
       28 
     | 
    
         
            -
                else
         
     | 
| 
       29 
     | 
    
         
            -
                  false
         
     | 
| 
       30 
     | 
    
         
            -
                end
         
     | 
| 
      
 23 
     | 
    
         
            +
                trigger?(event) and change(event)
         
     | 
| 
       31 
24 
     | 
    
         
             
              end
         
     | 
| 
       32 
25 
     | 
    
         | 
| 
       33 
26 
     | 
    
         
             
              def trigger!(event)
         
     | 
| 
       34 
     | 
    
         
            -
                 
     | 
| 
       35 
     | 
    
         
            -
                  true
         
     | 
| 
       36 
     | 
    
         
            -
                else
         
     | 
| 
      
 27 
     | 
    
         
            +
                trigger(event) or
         
     | 
| 
       37 
28 
     | 
    
         
             
                  raise InvalidState.new("Event '#{event}' not valid from state '#{@state}'")
         
     | 
| 
       38 
     | 
    
         
            -
                end
         
     | 
| 
       39 
29 
     | 
    
         
             
              end
         
     | 
| 
       40 
30 
     | 
    
         | 
| 
       41 
31 
     | 
    
         
             
              def trigger?(event)
         
     | 
| 
       42 
32 
     | 
    
         
             
                raise InvalidEvent unless transitions_for.has_key?(event)
         
     | 
| 
       43 
     | 
    
         
            -
                transitions_for[event] 
     | 
| 
      
 33 
     | 
    
         
            +
                transitions_for[event].has_key?(state)
         
     | 
| 
       44 
34 
     | 
    
         
             
              end
         
     | 
| 
       45 
35 
     | 
    
         | 
| 
       46 
36 
     | 
    
         
             
              def events
         
     | 
| 
         @@ -48,10 +38,15 @@ class MicroMachine 
     | 
|
| 
       48 
38 
     | 
    
         
             
              end
         
     | 
| 
       49 
39 
     | 
    
         | 
| 
       50 
40 
     | 
    
         
             
              def states
         
     | 
| 
       51 
     | 
    
         
            -
                 
     | 
| 
      
 41 
     | 
    
         
            +
                transitions_for.values.map(&:to_a).flatten.uniq
         
     | 
| 
       52 
42 
     | 
    
         
             
              end
         
     | 
| 
       53 
43 
     | 
    
         | 
| 
       54 
     | 
    
         
            -
             
     | 
| 
       55 
     | 
    
         
            -
             
     | 
| 
      
 44 
     | 
    
         
            +
            private
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
              def change(event)
         
     | 
| 
      
 47 
     | 
    
         
            +
                @state = transitions_for[event][@state]
         
     | 
| 
      
 48 
     | 
    
         
            +
                callbacks = @callbacks[@state] + @callbacks[:any]
         
     | 
| 
      
 49 
     | 
    
         
            +
                callbacks.each { |callback| callback.call(event) }
         
     | 
| 
      
 50 
     | 
    
         
            +
                true
         
     | 
| 
       56 
51 
     | 
    
         
             
              end
         
     | 
| 
       57 
52 
     | 
    
         
             
            end
         
     | 
    
        data/micromachine.gemspec
    CHANGED
    
    | 
         @@ -1,6 +1,6 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            Gem::Specification.new do |s|
         
     | 
| 
       2 
2 
     | 
    
         
             
              s.name = "micromachine"
         
     | 
| 
       3 
     | 
    
         
            -
              s.version = " 
     | 
| 
      
 3 
     | 
    
         
            +
              s.version = "2.0.0"
         
     | 
| 
       4 
4 
     | 
    
         
             
              s.summary = %{Minimal Finite State Machine.}
         
     | 
| 
       5 
5 
     | 
    
         
             
              s.description = %Q{There are many finite state machine implementations for Ruby, and they all provide a nice DSL for declaring events, exceptions, callbacks, and all kinds of niceties in general.\n\nBut if all you want is a finite state machine, look no further: this has less than 50 lines of code and provides everything a finite state machine must have, and nothing more.}
         
     | 
| 
       6 
6 
     | 
    
         
             
              s.author = ["Michel Martens"]
         
     | 
    
        data/test/introspection.rb
    CHANGED
    
    | 
         @@ -17,11 +17,3 @@ end 
     | 
|
| 
       17 
17 
     | 
    
         
             
            test "returns an array with the defined states" do |machine|
         
     | 
| 
       18 
18 
     | 
    
         
             
              assert_equal [:pending, :confirmed, :ignored], machine.states
         
     | 
| 
       19 
19 
     | 
    
         
             
            end
         
     | 
| 
       20 
     | 
    
         
            -
             
     | 
| 
       21 
     | 
    
         
            -
            test "returns false if compared state is not equal to current" do |machine|
         
     | 
| 
       22 
     | 
    
         
            -
              assert !(machine == :confirmed)
         
     | 
| 
       23 
     | 
    
         
            -
            end
         
     | 
| 
       24 
     | 
    
         
            -
             
     | 
| 
       25 
     | 
    
         
            -
            test "returns true if compared state is equal to current" do |machine|
         
     | 
| 
       26 
     | 
    
         
            -
              assert machine == :pending
         
     | 
| 
       27 
     | 
    
         
            -
            end
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: micromachine
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version:  
     | 
| 
      
 4 
     | 
    
         
            +
              version: 2.0.0
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Michel Martens
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2015- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2015-11-03 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: cutest
         
     |