finity 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -48,7 +48,7 @@ class Readfile
48
48
 
49
49
  event :read do
50
50
  transitions :from => [:opened, :reading], :to => :reading,
51
- :if => proc { not @file.eof? }
51
+ :if => proc { not @file.eof? },
52
52
  :do => proc { log 'Reading next line of file' }
53
53
 
54
54
  transitions :from => [:opened, :reading], :to => :reading,
@@ -97,10 +97,10 @@ object. Many other state machine implementations define one method for each even
97
97
  however, **Finity** tries to be as minimally invasive as possible:
98
98
 
99
99
  ```
100
- object = SomeClassIncludingFinity.new
101
- if object.state? :some_state
102
- object.event! :some_event
103
- end
100
+ object = SomeClassIncludingFinity.new
101
+ if object.state? :some_state
102
+ object.event! :some_event
103
+ end
104
104
  ```
105
105
 
106
106
  [transitions]: https://github.com/troessner/transitions
@@ -35,12 +35,6 @@ module Finity
35
35
  module ClassMethods
36
36
  attr_accessor :machine
37
37
 
38
- # When inheriting this module, pass the registered machines to the
39
- # inheriting class after executing potential parent inheritance logic.
40
- def inherited klass
41
- super and klass.machine = machine
42
- end
43
-
44
38
  # Instantiate a new state machine for the including class by accepting a
45
39
  # block with state and event (and subsequent transition) definitions.
46
40
  def finity options = {}, &block
@@ -27,7 +27,7 @@ module Finity
27
27
  # Initialize a new state machine within the provided class and define
28
28
  # methods for querying the current state and initiating transitions.
29
29
  def initialize klass, options = {}, &block
30
- @klass, @states, @events, @init = klass, {}, {}, options.delete(:init)
30
+ @klass, @states, @events, @inital = klass, {}, {}, options.delete(:inital)
31
31
  @klass.send :define_method, :event! do |*args|
32
32
  klass.machine.update self, *args
33
33
  end
@@ -35,11 +35,12 @@ module Finity
35
35
  klass.machine.current.name.eql? *args
36
36
  end
37
37
  instance_eval &block if block_given?
38
+ @current = inital
38
39
  end
39
40
 
40
41
  # Return the name of the initial state.
41
- def init
42
- @init ||= @states.keys.first unless @states.first.nil?
42
+ def inital
43
+ @inital ||= @states.keys.first unless @states.first.nil?
43
44
  end
44
45
 
45
46
  # Register a state.
@@ -55,14 +56,14 @@ module Finity
55
56
  # An event occured, so update the state machine by evaluating the
56
57
  # transition functions and notify the left and entered state.
57
58
  def update object, event
58
- @current ||= @states[init]
59
- if (state = @events[event].handle object, @current)
59
+ current ||= @states[@current || inital]
60
+ if state = @events[event].handle(object, current)
60
61
  if @states[state].nil?
61
62
  raise InvalidState, "Invalid state '#{state}'"
62
63
  end
63
- @current.leave object
64
- @current = @states[state]
65
- @current.enter object
64
+ current.leave object
65
+ current = @states[@current = state]
66
+ current.enter object
66
67
  end
67
68
  end
68
69
  end
@@ -1,3 +1,3 @@
1
1
  module Finity
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: finity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-22 00:00:00.000000000 Z
12
+ date: 2012-06-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler