finity 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,9 +8,8 @@ Gem::Specification.new do |s|
8
8
  s.authors = ['Martin Donath']
9
9
  s.email = 'md@struct.cc'
10
10
  s.homepage = 'http://github.com/squidfunk/finity'
11
- s.summary = 'Slim and more readable state machine for Ruby'
12
- s.description = 'Extremly lightweight state machine implementation with an easily readable syntax, ' +
13
- 'which is essential if you have tens or hundreds of transitions.'
11
+ s.summary = 'Slim and readable state machine for Ruby'
12
+ s.description = 'Extremly lightweight state machine implementation with an easily readable syntax'
14
13
 
15
14
  s.required_rubygems_version = '>= 1.3.6'
16
15
  s.add_development_dependency 'bundler', '~> 1'
@@ -45,7 +45,8 @@ module Finity
45
45
  @transitions[state.name].find do |transition|
46
46
  name = transition.handle object
47
47
  return name unless name.nil?
48
- end
48
+ end rescue
49
+ raise InvalidState, "No handler for :#{name} from :#{state.name}"
49
50
  end
50
51
  end
51
52
  end
@@ -44,7 +44,7 @@ module Finity
44
44
  )
45
45
  end
46
46
  @klass.send :define_method, :state? do |*args|
47
- klass.finity.current.name.eql? *args
47
+ self.current.eql? *args
48
48
  end
49
49
  end
50
50
 
@@ -53,27 +53,34 @@ module Finity
53
53
  @initial ||= @states.keys.first unless @states.first.nil?
54
54
  end
55
55
 
56
- # Register a state.
56
+ # Register a state, or a set of states.
57
57
  def state name, options = {}
58
- @states[name] = State.new name, options
58
+ name.is_a?(Array) ? name.each { |value| state value, options } :
59
+ @states[name] = State.new(name, options)
59
60
  end
60
61
 
61
62
  # Register an event and evaluate the block for transitions.
62
63
  def event name, options = {}, &block
63
- @events[name] = Event.new name, options, &block
64
+ name.is_a?(Array) ? name.each { |value| event value, options, &block } :
65
+ @events[name] = Event.new(name, options, &block)
64
66
  end
65
67
 
66
68
  # An event occured, so update the state machine by evaluating the
67
- # transition functions and notify the left and entered state.
69
+ # transition functions and notify the left and entered state, but only if
70
+ # the event leads to a transition that triggers a state change.
68
71
  def update object, current, event
69
72
  now ||= @states[current]
70
73
  if state = @events[event].handle(object, now)
71
74
  if @states[state].nil?
72
75
  raise InvalidState, "Invalid state #{state}"
73
76
  end
74
- now.leave object
75
- now = @states[current = state]
76
- now.enter object
77
+ if state.eql? current
78
+ now.cycle object
79
+ else
80
+ now.leave object
81
+ now = @states[current = state]
82
+ now.enter object
83
+ end
77
84
  end
78
85
  current
79
86
  end
@@ -26,7 +26,7 @@ module Finity
26
26
 
27
27
  # Initialize a new state for the state machine with callbacks.
28
28
  def initialize name, options = {}
29
- @name, @enter, @leave = name, *options.values_at(:enter, :leave)
29
+ @name, @enter, @cycle, @leave = name, *options.values_at(:enter, :cycle, :leave)
30
30
  instance_eval &block if block_given?
31
31
  end
32
32
 
@@ -35,6 +35,11 @@ module Finity
35
35
  execute object, @enter unless @enter.nil?
36
36
  end
37
37
 
38
+ # Executed if a state is kept during a cycle.
39
+ def cycle object
40
+ execute object, @cycle unless @cycle.nil?
41
+ end
42
+
38
43
  # Executed when the current state is left.
39
44
  def leave object
40
45
  execute object, @leave unless @leave.nil?
@@ -1,3 +1,3 @@
1
1
  module Finity
2
- VERSION = '0.0.4'
2
+ VERSION = '0.1.0'
3
3
  end
metadata CHANGED
@@ -1,36 +1,39 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: finity
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.4
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
5
6
  platform: ruby
6
- authors:
7
+ authors:
7
8
  - Martin Donath
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
-
12
- date: 2012-07-17 00:00:00 +02:00
13
- default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2012-08-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1'
17
22
  type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
21
27
  - - ~>
22
- - !ruby/object:Gem::Version
23
- version: "1"
24
- version:
25
- description: Extremly lightweight state machine implementation with an easily readable syntax, which is essential if you have tens or hundreds of transitions.
28
+ - !ruby/object:Gem::Version
29
+ version: '1'
30
+ description: Extremly lightweight state machine implementation with an easily readable
31
+ syntax
26
32
  email: md@struct.cc
27
33
  executables: []
28
-
29
34
  extensions: []
30
-
31
35
  extra_rdoc_files: []
32
-
33
- files:
36
+ files:
34
37
  - Gemfile
35
38
  - LICENSE
36
39
  - README.md
@@ -41,33 +44,28 @@ files:
41
44
  - lib/finity/state.rb
42
45
  - lib/finity/transition.rb
43
46
  - lib/finity/version.rb
44
- has_rdoc: true
45
47
  homepage: http://github.com/squidfunk/finity
46
48
  licenses: []
47
-
48
49
  post_install_message:
49
50
  rdoc_options: []
50
-
51
- require_paths:
51
+ require_paths:
52
52
  - lib
53
- required_ruby_version: !ruby/object:Gem::Requirement
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- version: "0"
58
- version:
59
- required_rubygems_version: !ruby/object:Gem::Requirement
60
- requirements:
61
- - - ">="
62
- - !ruby/object:Gem::Version
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
63
64
  version: 1.3.6
64
- version:
65
65
  requirements: []
66
-
67
66
  rubyforge_project:
68
- rubygems_version: 1.3.5
67
+ rubygems_version: 1.8.24
69
68
  signing_key:
70
69
  specification_version: 3
71
- summary: Slim and more readable state machine for Ruby
70
+ summary: Slim and readable state machine for Ruby
72
71
  test_files: []
73
-