state_machines 0.1.3 → 0.1.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f4408b8e84e9ed5e15d4c26a98acd7190a6e5a66
4
- data.tar.gz: 47f30c2fda4189ab81c1c1dca43774a2e0d56965
3
+ metadata.gz: 3935f97b4e0314e076a6a35afc73f4f187058293
4
+ data.tar.gz: f2e5c9374a4f1b05729035f14f4909dc7f218518
5
5
  SHA512:
6
- metadata.gz: ca98c4bbd71a2a351ba3f3b6f70951a70ac0148dfa019eae76cb99083d2f4a02afe967f0464ec8bceaf92defb59f5512681f1ae95a177dd6707742b68cf055f0
7
- data.tar.gz: ddf52356a4ef5738bd7a27dfd6d61f95f3e72f38f3ee60a70202706eb2a3bdcbf526954682addb9f42dec6fa57b75ba406521cf49c25a77937e18cca8999a39f
6
+ metadata.gz: 48312c212788cc3b03356ea37dbfa1cd5b73ea881b2f61f6367d3d8ad3eece0c145b79bc47b7c895b222651f887af31ca3695028c2c8f87d43f4e7a166944d42
7
+ data.tar.gz: 4e05b6813f79d0374d9f21be4f91567fa09ed5c18fb9dd99ae108be5e1d813f149fbd757b3ee11ff1f9988d0819a3fa81907967cbcdbb0858d8a12c3de27514a
data/Changelog.md CHANGED
@@ -0,0 +1,12 @@
1
+ * Pass `static: false` in case you don't want initial states to be forced. e.g.
2
+
3
+ ```ruby
4
+ # will set the initial machine state
5
+ @machines.initialize_states(@object)
6
+
7
+ # optionally you can pass the attributes to have that as the initial state
8
+ @machines.initialize_states(@object, {}, { state: 'finished' })
9
+
10
+ # or pass set `static` to false if you want to keep the `object.state` current value
11
+ @machines.initialize_states(@object, { static: false })
12
+ ```
data/Contributors.md ADDED
@@ -0,0 +1,39 @@
1
+ - Aaron Gibralter
2
+ - Aaron Pfeifer
3
+ - Abdelkader Boudih
4
+ - Akira Matsuda
5
+ - Andrea Longhi
6
+ - Brad Heller
7
+ - Brandon Dimcheff
8
+ - Casey Howard
9
+ - Chinasaur
10
+ - Daniel Huckstep
11
+ - Durran Jordan
12
+ - Gareth Adams
13
+ - Jahangir Zinedine
14
+ - Jeremy Wells
15
+ - Joe Lind
16
+ - Jon Evans
17
+ - Markus Schirp
18
+ - Michael Klishin
19
+ - Mikhail Shirkov
20
+ - Mohamed Alouane
21
+ - Nate Murray
22
+ - Nathan Long
23
+ - Nicolas Blanco
24
+ - Pawel Pierzchala
25
+ - Pete Forde
26
+ - Peter Lampesberger
27
+ - Rin Raeuber
28
+ - Robert Poor
29
+ - Rustam Zagirov
30
+ - Sandro Turriate and Tim Pope
31
+ - Sean O'Brien
32
+ - Stefan Penner
33
+ - Steve Richert
34
+ - Wojciech Wnętrzak
35
+ - @chris
36
+ - @gmitrev
37
+ - @nblumoe
38
+ - @reiner
39
+ - @sanemat
@@ -1,8 +1,6 @@
1
1
  module StateMachines
2
2
  # Represents a collection of state machines for a class
3
3
  class MachineCollection < Hash
4
-
5
-
6
4
  # Initializes the state of each machine in the given object. This can allow
7
5
  # states to be initialized in two groups: static and dynamic. For example:
8
6
  #
@@ -13,22 +11,25 @@ module StateMachines
13
11
  # If no block is provided, then all states will still be initialized.
14
12
  #
15
13
  # Valid configuration options:
16
- # * <tt>:static</tt> - Whether to initialize static states. If set to
17
- # :force, the state will be initialized regardless of its current value.
18
- # Default is :force.
14
+ # * <tt>:static</tt> - Whether to initialize static states. Unless set to
15
+ # false, the state will be initialized regardless of its current value.
16
+ # Default is true.
19
17
  # * <tt>:dynamic</tt> - Whether to initialize dynamic states. If set to
20
18
  # :force, the state will be initialized regardless of its current value.
21
19
  # Default is true.
22
20
  # * <tt>:to</tt> - A hash to write the initialized state to instead of
23
21
  # writing to the object. Default is to write directly to the object.
24
- def initialize_states(object, options = {})
22
+ def initialize_states(object, options = {}, attributes = {})
25
23
  options.assert_valid_keys( :static, :dynamic, :to)
26
24
  options = {:static => true, :dynamic => true}.merge(options)
27
25
 
28
26
  result = yield if block_given?
29
27
 
30
28
  each_value do |machine|
31
- machine.initialize_state(object, :force => options[:static] == :force, :to => options[:to]) unless machine.dynamic_initial_state?
29
+ unless machine.dynamic_initial_state?
30
+ force = options[:static] == :force || !attributes.keys.include?(machine.attribute)
31
+ machine.initialize_state(object, force: force, :to => options[:to])
32
+ end
32
33
  end if options[:static]
33
34
 
34
35
  each_value do |machine|
@@ -1,3 +1,3 @@
1
1
  module StateMachines
2
- VERSION = '0.1.3'
2
+ VERSION = '0.1.4'
3
3
  end
@@ -29,18 +29,7 @@ class MachineWithStaticInitialStateTest < StateMachinesTest
29
29
  assert_equal 'parked', @klass.new.state
30
30
  end
31
31
 
32
- def test_not_set_initial_state_even_if_not_empty
33
- @klass.class_eval do
34
- def initialize(_attributes = {})
35
- self.state = 'idling'
36
- super()
37
- end
38
- end
39
- object = @klass.new
40
- assert_equal 'idling', object.state
41
- end
42
-
43
- def test_should_set_initial_state_prior_to_initialization
32
+ def test_should_not_initial_state_prior_to_initialization
44
33
  base = Class.new do
45
34
  attr_accessor :state_on_init
46
35
 
@@ -51,7 +40,7 @@ class MachineWithStaticInitialStateTest < StateMachinesTest
51
40
  klass = Class.new(base)
52
41
  StateMachines::Machine.new(klass, initial: :parked)
53
42
 
54
- assert_equal 'parked', klass.new.state_on_init
43
+ assert_nil klass.new.state_on_init
55
44
  end
56
45
 
57
46
  def test_should_be_included_in_known_states
@@ -7,8 +7,8 @@ class MachineCollectionStateInitializationTest < StateMachinesTest
7
7
  @klass = Class.new
8
8
 
9
9
  @machines[:state] = StateMachines::Machine.new(@klass, :state, initial: :parked)
10
- @machines[:alarm_state] = StateMachines::Machine.new(@klass, :alarm_state, initial: lambda { |_object| :active })
11
- @machines[:alarm_state].state :active, value: lambda { 'active' }
10
+ @machines[:alarm_state] = StateMachines::Machine.new(@klass, :alarm_state, initial: ->(_object) { :active })
11
+ @machines[:alarm_state].state :active, value: -> { 'active' }
12
12
 
13
13
  # Prevent the auto-initialization hook from firing
14
14
  @klass.class_eval do
@@ -25,17 +25,17 @@ class MachineCollectionStateInitializationTest < StateMachinesTest
25
25
  assert_raises(ArgumentError) { @machines.initialize_states(@object, invalid: true) }
26
26
  end
27
27
 
28
- def test_should_only_initialize_static_states_prior_to_block
28
+ def test_should_initialize_static_states_after_block
29
29
  @machines.initialize_states(@object) do
30
30
  @state_in_block = @object.state
31
31
  @alarm_state_in_block = @object.alarm_state
32
32
  end
33
33
 
34
- assert_equal 'parked', @state_in_block
34
+ assert_nil @state_in_block
35
35
  assert_nil @alarm_state_in_block
36
36
  end
37
37
 
38
- def test_should_only_initialize_dynamic_states_after_block
38
+ def test_should_initialize_dynamic_states_after_block
39
39
  @machines.initialize_states(@object) do
40
40
  @alarm_state_in_block = @object.alarm_state
41
41
  end
@@ -57,10 +57,10 @@ class MachineCollectionStateInitializationTest < StateMachinesTest
57
57
  assert_equal 'active', @object.alarm_state
58
58
  end
59
59
 
60
- def test_should_not_initialize_existing_static_states_by_default
60
+ def test_should_initialize_existing_static_states_by_default
61
61
  @object.state = 'idling'
62
62
  @machines.initialize_states(@object)
63
- assert_equal 'idling', @object.state
63
+ assert_equal 'parked', @object.state
64
64
  end
65
65
 
66
66
  def test_should_initialize_existing_static_states_if_forced
@@ -69,10 +69,10 @@ class MachineCollectionStateInitializationTest < StateMachinesTest
69
69
  assert_equal 'parked', @object.state
70
70
  end
71
71
 
72
- def test_should_not_initialize_existing_static_states_if_not_forced
72
+ def test_should_initialize_existing_static_states_if_not_forced
73
73
  @object.state = 'idling'
74
74
  @machines.initialize_states(@object, static: true)
75
- assert_equal 'idling', @object.state
75
+ assert_equal 'parked', @object.state
76
76
  end
77
77
 
78
78
  def test_should_skip_dynamic_states_if_disabled
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_machines
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-01-21 00:00:00.000000000 Z
12
+ date: 2015-01-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -65,6 +65,7 @@ files:
65
65
  - ".rspec"
66
66
  - ".travis.yml"
67
67
  - Changelog.md
68
+ - Contributors.md
68
69
  - Gemfile
69
70
  - LICENSE.txt
70
71
  - README.md