solidstate 0.1.0 → 0.2.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/README.md +8 -8
- data/lib/solidstate.rb +10 -1
- data/solidstate.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2440bdd08c03d227ce1343783e7fc863d197db67
|
4
|
+
data.tar.gz: 56ea18976e16dce884b21b2b1bc0a94a7be1b564
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d59cea2fb06b79b7f660cd4dcd8fff0927e843760434f3eba84edafcf87e7671a3577b06632b3eba225520eae0e0104fbb2445db1f5fd6addf493dcf42c4286
|
7
|
+
data.tar.gz: a1f6bb99d36c2ef57ea9914477d6863ed811895cfc817df780bfac72bde5cb082c10b53e1bf19f468af9e01e1900010118ddff87d7ebed846a24dc386ea58f95
|
data/README.md
CHANGED
@@ -50,29 +50,29 @@ Minuscule but solid state machine for Ruby classes. The only dependency is that
|
|
50
50
|
include SolidState
|
51
51
|
|
52
52
|
states :inactive, :active, :unsubscribed, :disabled do
|
53
|
-
transitions :
|
54
|
-
transitions :
|
55
|
-
transitions :
|
53
|
+
transitions from: :inactive, to: :active
|
54
|
+
transitions from: :active, to: [:unsubscribed, :disabled]
|
55
|
+
transitions from: :unsubscribed, to: :active
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
59
|
s = Subscriber.new
|
60
|
-
s.state
|
60
|
+
s.state # => 'inactive'
|
61
61
|
|
62
62
|
# since we declared transitions, we can now call #{state}! which
|
63
63
|
# checks whether the instance can transition to that state and
|
64
64
|
# if so, sets the new state and optionally saves the record.
|
65
65
|
|
66
|
-
s.active!
|
66
|
+
s.active! # => true
|
67
67
|
|
68
68
|
s.inactive! # => raises InvalidTransitionError
|
69
69
|
|
70
70
|
# this also works outside transition methods, of course.
|
71
|
-
s.reload
|
72
|
-
s.active?
|
71
|
+
s.reload # => true
|
72
|
+
s.active? # => true
|
73
73
|
|
74
74
|
s.state = 'inactive'
|
75
|
-
s.valid?
|
75
|
+
s.valid? # => false
|
76
76
|
|
77
77
|
# the last trick this library does is that it optionally lets you
|
78
78
|
# declare callback methods that are called whenever a transition
|
data/lib/solidstate.rb
CHANGED
@@ -8,8 +8,8 @@ module SolidState
|
|
8
8
|
module ClassMethods
|
9
9
|
|
10
10
|
def states(*list, &block)
|
11
|
-
raise "This is not a list of names" unless list.first.is_a?(String)
|
12
11
|
list = list.collect(&:to_s)
|
12
|
+
raise "This is not a list of names" unless list.first.respond_to?(:downcase)
|
13
13
|
|
14
14
|
@@states = list
|
15
15
|
@@state_transitions = {}
|
@@ -62,6 +62,7 @@ module SolidState
|
|
62
62
|
|
63
63
|
if !respond_to?(:valid?) or (valid? && save)
|
64
64
|
send("once_#{dest}", from) if respond_to?("once_#{dest}")
|
65
|
+
send("once_not_#{from}", dest) if respond_to?("once_not_#{from}")
|
65
66
|
true
|
66
67
|
else
|
67
68
|
false
|
@@ -71,11 +72,19 @@ module SolidState
|
|
71
72
|
end
|
72
73
|
end
|
73
74
|
|
75
|
+
def once_not_subscribed
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
def after
|
80
|
+
|
74
81
|
def set_state(new_state)
|
75
82
|
return false unless can_transition_to?(new_state)
|
76
83
|
self.state = new_state
|
77
84
|
end
|
78
85
|
|
86
|
+
private
|
87
|
+
|
79
88
|
def ensure_valid_transition
|
80
89
|
if send("#{STATE_ATTRIBUTE}_changed?") and !can_transition_to?(state)
|
81
90
|
errors.add(STATE_ATTRIBUTE, "can't transition from current state to #{state}")
|
data/solidstate.gemspec
CHANGED