state_attr 0.1.12 → 0.1.13
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/state_attr/state.rb +14 -4
- data/state_attr.gemspec +2 -2
- data/test/unit/state_attr_test.rb +24 -0
- metadata +5 -5
data/lib/state_attr/state.rb
CHANGED
@@ -5,11 +5,20 @@ module StateAttr
|
|
5
5
|
def initialize(parent, field, machine, logger, options)
|
6
6
|
@parent = parent
|
7
7
|
@field = field
|
8
|
-
|
8
|
+
@options = options
|
9
|
+
groups = @options[:groups] || {}
|
9
10
|
@machine = {}
|
10
|
-
|
11
|
+
#convert different types of input to array of symbols
|
12
|
+
machine.each { |key, value|
|
13
|
+
@machine[key] = if value.nil?
|
14
|
+
[nil]
|
15
|
+
elsif groups.keys.include? value
|
16
|
+
Array(groups[value]).map(&:to_sym)
|
17
|
+
else
|
18
|
+
Array(value).map(&:to_sym)
|
19
|
+
end
|
20
|
+
}
|
11
21
|
@logger = logger
|
12
|
-
@options = options
|
13
22
|
@callback = "on_#{@field}_change".to_sym
|
14
23
|
end
|
15
24
|
|
@@ -34,7 +43,8 @@ module StateAttr
|
|
34
43
|
|
35
44
|
# array of allowed stated
|
36
45
|
def allowed
|
37
|
-
@machine[read_state] || []
|
46
|
+
result = @machine[read_state] || []
|
47
|
+
result
|
38
48
|
end
|
39
49
|
|
40
50
|
#validate if can switch to given state
|
data/state_attr.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "state_attr"
|
3
|
-
s.version = "0.1.
|
4
|
-
s.date = "2011-
|
3
|
+
s.version = "0.1.13"
|
4
|
+
s.date = "2011-06-29"
|
5
5
|
s.summary = "Minimalistic state machine approach allowing multiple state attributes at the same time."
|
6
6
|
s.email = "mpapis@gmail.com"
|
7
7
|
s.homepage = "http://github.com/mpapis/state_attr/tree/master"
|
@@ -17,6 +17,18 @@ class FakeModel < ActiveRecord::Base
|
|
17
17
|
nil => :g,
|
18
18
|
:g => :h,
|
19
19
|
}
|
20
|
+
|
21
|
+
state_attr :group_state, {
|
22
|
+
nil => :a,
|
23
|
+
:a => :all,
|
24
|
+
}, :groups => {
|
25
|
+
:all => %w( a b c d)
|
26
|
+
}
|
27
|
+
|
28
|
+
# state_attr :state_initial, {
|
29
|
+
# :a => [:b, :c],
|
30
|
+
# :b => :c,
|
31
|
+
# }, :initial => :a
|
20
32
|
end
|
21
33
|
|
22
34
|
class StateAttrTest < Test::Unit::TestCase
|
@@ -76,4 +88,16 @@ class StateAttrTest < Test::Unit::TestCase
|
|
76
88
|
assert model.state2.allowed?(:d)
|
77
89
|
assert model.state2.allowed?(:e)
|
78
90
|
end
|
91
|
+
def test_group
|
92
|
+
model = FakeModel.new
|
93
|
+
assert_equal [:a], model.group_state.allowed
|
94
|
+
model.group_state.switch(:a)
|
95
|
+
assert_equal [:a,:b,:c,:d], model.group_state.allowed
|
96
|
+
end
|
97
|
+
# def test_state_initial
|
98
|
+
# model = FakeModel.new
|
99
|
+
# assert_equal :a, model.state_initial.value
|
100
|
+
# model.state_initial.switch(:b)
|
101
|
+
# assert_equal :b, model.state_initial.value
|
102
|
+
# end
|
79
103
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: state_attr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 1
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 13
|
10
|
+
version: 0.1.13
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Michal Papis
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-06-29 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -66,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
66
|
requirements: []
|
67
67
|
|
68
68
|
rubyforge_project:
|
69
|
-
rubygems_version: 1.5.
|
69
|
+
rubygems_version: 1.5.3
|
70
70
|
signing_key:
|
71
71
|
specification_version: 3
|
72
72
|
summary: Minimalistic state machine approach allowing multiple state attributes at the same time.
|