literal_enums 1.0.0 → 1.1.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/Gemfile.lock +1 -1
- data/README.md +15 -15
- data/lib/enum.rb +2 -2
- data/lib/literal_enums/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5966057536538f9691058988d2ead560f4c0827325d5334db07326f3d61ce73a
|
4
|
+
data.tar.gz: 76be2563d5184035fffb3df734c3578a140daf222e1c53f39d581f25db7dcfb1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 570244eab14a91efd59a10267cbd05d0b41c2fbb5709486476a483e9877844e73658a99813b20e41f0912d91f4964e93bbac4832f810fd8fc9fd4e560c9495da
|
7
|
+
data.tar.gz: c2cecc679d71e18fcc14443ded0d082b9fc6ef051f9d325a0e7e89fdf149d554c7bc3e34cec9dfc20e363d89b3996ea34012a138d92e75e527fb9ae9c4681d6e
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -90,50 +90,50 @@ It is also possible to define a more complex state machine by defining `transiti
|
|
90
90
|
|
91
91
|
```ruby
|
92
92
|
class State < Enum
|
93
|
-
|
93
|
+
Pending do
|
94
94
|
def transitions_to
|
95
|
-
[
|
95
|
+
[Approved, Rejected]
|
96
96
|
end
|
97
97
|
end
|
98
98
|
|
99
|
-
|
99
|
+
Approved do
|
100
100
|
def transitions_to
|
101
|
-
|
101
|
+
Published
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
105
|
-
|
105
|
+
Rejected do
|
106
106
|
def transitions_to
|
107
|
-
|
107
|
+
Deleted
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
111
|
-
|
112
|
-
|
111
|
+
Deleted()
|
112
|
+
Published()
|
113
113
|
end
|
114
114
|
```
|
115
115
|
|
116
116
|
Given the above definition, we can transition from one state to another by calling `transition_to` with the newly desired state. This will raise a `LiteralEnums::TransitionError` if the transition is invalid.
|
117
117
|
|
118
118
|
```ruby
|
119
|
-
State::
|
120
|
-
State::
|
119
|
+
State::Pending.transition_to(State::Approved) # returns State::Approved.
|
120
|
+
State::Pending.transition_to(State::Published) # raises a LiteralEnums::TransitionError.
|
121
121
|
```
|
122
122
|
|
123
123
|
Alternatively, we can call the new state as a method on the old state.
|
124
124
|
|
125
125
|
```ruby
|
126
|
-
State::
|
126
|
+
State::Pending.approved # returns State::Approved.
|
127
127
|
```
|
128
128
|
|
129
|
-
An invalid transition
|
129
|
+
An invalid transition would return a `NoMethodErorr` in this case as the method is not defined.
|
130
130
|
|
131
131
|
The advantage of using method calls to transition from one state to anotehr is the method calls can be chained.
|
132
132
|
|
133
133
|
```ruby
|
134
|
-
State::
|
135
|
-
|
136
|
-
|
134
|
+
State::Pending.approved.published # returns State::Published, since it's valid to
|
135
|
+
# move to State::Published from State::Approved and it's
|
136
|
+
# valid to move to State::Approved from State::Pending.
|
137
137
|
```
|
138
138
|
|
139
139
|
## Installation
|
data/lib/enum.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
class Enum
|
2
|
+
extend Enumerable
|
2
3
|
include LiteralEnums::Transitions
|
3
4
|
|
4
5
|
attr_reader :name, :value
|
5
6
|
|
6
|
-
alias_method :to_s, :value
|
7
7
|
alias_method :inspect, :name
|
8
8
|
|
9
9
|
def initialize(name, value)
|
@@ -22,7 +22,7 @@ class Enum
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def values
|
25
|
-
|
25
|
+
map(&:value).to_set
|
26
26
|
end
|
27
27
|
|
28
28
|
def each(&block)
|