safe-enum 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +25 -7
- data/lib/enum/base.rb +15 -3
- data/lib/enum/predicates.rb +6 -1
- data/lib/enum/version.rb +1 -1
- data/lib/safe-enum.rb +1 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b01a24ce4b8682ef047734ceda2a6d42f7f20654
|
4
|
+
data.tar.gz: 344c9fbee4e130f694c287e4a8a2a5e29e063126
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f57bf5d384b5d282486dacd279fd14380acd61dc04f76e871c93d93ecdb5f3bec5e6f6772744c307c2e5b140b73c712e9a0f2eb7d3476609d955aef611884508
|
7
|
+
data.tar.gz: db9c679dcbfbcd4f785d732b903daf73fb8b73f691c212404a9dbd95ac4366879f1c3119e2746bdf3c7dd23c339ac171a21ba80cf8cc0c4523f8f1a0d6bed0ba
|
data/README.md
CHANGED
@@ -8,7 +8,7 @@ This is a very basic implementation of enums in Ruby. The cornerstone of the lib
|
|
8
8
|
Add this line to your application's Gemfile:
|
9
9
|
|
10
10
|
```ruby
|
11
|
-
gem 'safe-enum'
|
11
|
+
gem 'safe-enum'
|
12
12
|
```
|
13
13
|
|
14
14
|
And then execute:
|
@@ -28,13 +28,27 @@ class Side < Enum::Base
|
|
28
28
|
end
|
29
29
|
```
|
30
30
|
|
31
|
-
Now `
|
31
|
+
Now get a value with the `enum` method safely defined values by argument with its `Symbol` or `String` type. If there is no defined such value `Enum::TokenNotFoundError` exception will be raised. And this is the **safety** - you will be noticed about the problem and fix it by introducing a new value or fixing a source of the invalid value. While others implementations of enums in Ruby (that I know) just silently ignore invalid values returning `nil` this one will raise the exception **always**. Example of usage:
|
32
32
|
|
33
33
|
```ruby
|
34
|
-
Side.
|
35
|
-
Side.
|
36
|
-
Side.
|
37
|
-
Side.
|
34
|
+
Side.enum(:left) # => "left"
|
35
|
+
Side.enum('left') # => "left"
|
36
|
+
Side.enum(:invalid) # => Enum::TokenNotFoundError: token 'invalid'' not found in the enum Side
|
37
|
+
Side.enum('invalid') # => Enum::TokenNotFoundError: token 'invalid'' not found in the enum Side
|
38
|
+
```
|
39
|
+
|
40
|
+
Get all defined enum values with the `all` method:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
Side.all # => ['left', 'rigth', 'whole']
|
44
|
+
```
|
45
|
+
|
46
|
+
> Order or the returned values in the same as their definition. It's guaranteed.
|
47
|
+
|
48
|
+
In order to get array of defined enums safely use `enums` method:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
Side.enums(:left, :right) # => ['left', 'right']
|
38
52
|
```
|
39
53
|
|
40
54
|
If you have installed `I18n` in your application feel free to use `name` method to retreive the values' translations. For the given example the possible translation structure in `yml` format is the following:
|
@@ -74,7 +88,7 @@ end
|
|
74
88
|
@table.side_is?(:left) # => false
|
75
89
|
@table.side_is?(nil) # => false
|
76
90
|
|
77
|
-
@table.side = Side.
|
91
|
+
@table.side = Side.enum(:left)
|
78
92
|
@table.side_is?(:left) # => true
|
79
93
|
@table.side_is?(:right) # => false
|
80
94
|
@table.side_is?(nil) # => false
|
@@ -83,6 +97,9 @@ end
|
|
83
97
|
@table.side = 'invalid'
|
84
98
|
@table.side_is?(nil) # => false
|
85
99
|
@table.side_is?(:left) # => Enum::TokenNotFoundError: token 'invalid'' not found in the enum Side
|
100
|
+
@table.side_any?(:left, :right) # => true
|
101
|
+
@table.side_any?(:right) # => false
|
102
|
+
@table.side_any?(:invalid, :left) # => Enum::TokenNotFoundError: token 'invalid'' not found in the enum Side
|
86
103
|
```
|
87
104
|
> If you pass to the predicate `nil` or have `nil` value in the field the result will be always `false`. If you want to check that the field is `nil` just use Ruby's standard method `nil?`.
|
88
105
|
|
@@ -94,6 +111,7 @@ class WeekDay < Enum::Base
|
|
94
111
|
end
|
95
112
|
WeekDay.index(:sunday) == Date.new(2015, 9, 13).wday # => true
|
96
113
|
WeekDay.index(:monday) # => 1
|
114
|
+
WeekDay.indexes # => [0, 1, 2, 3, 4, 5, 6]
|
97
115
|
```
|
98
116
|
|
99
117
|
## Development
|
data/lib/enum/base.rb
CHANGED
@@ -17,7 +17,19 @@ module Enum
|
|
17
17
|
history
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
20
|
+
def indexes
|
21
|
+
(0...store.size).to_a
|
22
|
+
end
|
23
|
+
|
24
|
+
def include?(token)
|
25
|
+
store.include?(token.to_s)
|
26
|
+
end
|
27
|
+
|
28
|
+
def enums(*tokens)
|
29
|
+
tokens.map { |token| enum(token) }
|
30
|
+
end
|
31
|
+
|
32
|
+
def enum(t)
|
21
33
|
ts = t.to_s
|
22
34
|
unless store.include?(ts)
|
23
35
|
raise(TokenNotFoundError, "token '#{t}'' not found in the enum #{self}")
|
@@ -26,11 +38,11 @@ module Enum
|
|
26
38
|
end
|
27
39
|
|
28
40
|
def name(t)
|
29
|
-
translate(
|
41
|
+
translate(enum(t))
|
30
42
|
end
|
31
43
|
|
32
44
|
def index(token)
|
33
|
-
history.index(
|
45
|
+
history.index(enum(token))
|
34
46
|
end
|
35
47
|
|
36
48
|
protected
|
data/lib/enum/predicates.rb
CHANGED
@@ -3,11 +3,16 @@ module Enum
|
|
3
3
|
def enumerize(field, enum)
|
4
4
|
define_method("#{field}_is?") do |other|
|
5
5
|
if (field_value = public_send(field)) && other
|
6
|
-
enum.
|
6
|
+
enum.enum(field_value) == enum.enum(other)
|
7
7
|
else
|
8
8
|
false
|
9
9
|
end
|
10
10
|
end
|
11
|
+
|
12
|
+
define_method("#{field}_any?") do |*others|
|
13
|
+
others.each { |other| enum.enum(other) } # make sure that all others values are valid enums
|
14
|
+
others.any? { |other| public_send("#{field}_is?", other) }
|
15
|
+
end
|
11
16
|
end
|
12
17
|
end
|
13
18
|
end
|
data/lib/enum/version.rb
CHANGED
data/lib/safe-enum.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'enum'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: safe-enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey Koleshko
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-04-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -89,6 +89,7 @@ files:
|
|
89
89
|
- lib/enum/predicates.rb
|
90
90
|
- lib/enum/token_not_found_error.rb
|
91
91
|
- lib/enum/version.rb
|
92
|
+
- lib/safe-enum.rb
|
92
93
|
homepage: https://github.com/mezuka/enum
|
93
94
|
licenses:
|
94
95
|
- MIT
|
@@ -109,8 +110,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
110
|
version: '0'
|
110
111
|
requirements: []
|
111
112
|
rubyforge_project:
|
112
|
-
rubygems_version: 2.
|
113
|
+
rubygems_version: 2.5.1
|
113
114
|
signing_key:
|
114
115
|
specification_version: 4
|
115
116
|
summary: Enum implementation
|
116
117
|
test_files: []
|
118
|
+
has_rdoc:
|