safe-enum 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/.gitignore +1 -0
- data/README.md +40 -3
- data/lib/enum.rb +1 -0
- data/lib/enum/base.rb +15 -1
- data/lib/enum/predicates.rb +13 -0
- data/lib/enum/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 00f1140cec43431cbc879909a1b626628e041a6a
|
4
|
+
data.tar.gz: 08542bb0baba436e8eb2e5b79db64d5ecdabed00
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 30e94b5da5fc242107ab922e6ec0f52da85e82092f32ae5dcb047258aefad7cfb1d74936abdcd76cb1f3ab6dc18dd08a09c3b9af7131031ee1738f8fe8254d77
|
7
|
+
data.tar.gz: 2e14ca8f1d0e9b2858332b44d4f37327ffaf043e85f91887104041bffacf08d29980583dff7f15957cbf3f39254379a0262adaf516748926d1da3f6565966253
|
data/.gitignore
CHANGED
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 'enum', require: 'enum'
|
11
|
+
gem 'safe-enum', require: 'enum'
|
12
12
|
```
|
13
13
|
|
14
14
|
And then execute:
|
@@ -17,7 +17,7 @@ And then execute:
|
|
17
17
|
|
18
18
|
Or install it yourself as:
|
19
19
|
|
20
|
-
$ gem install enum
|
20
|
+
$ gem install safe-enum
|
21
21
|
|
22
22
|
## Usage
|
23
23
|
|
@@ -28,7 +28,7 @@ class Side < Enum::Base
|
|
28
28
|
end
|
29
29
|
```
|
30
30
|
|
31
|
-
Now `take` 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
|
31
|
+
Now `take` 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
34
|
Side.take(:left) # => "left"
|
@@ -59,6 +59,43 @@ Side.name(:invalid) # => Enum::TokenNotFoundError: token 'invalid'' not found in
|
|
59
59
|
|
60
60
|
> If you don't have installed `I18n` in your project `NameError` exception will be raised on the `name` method call.
|
61
61
|
|
62
|
+
Consider the case when we have an object with a field with only enum values. Extend the class of this object by `Enum::Predicates` and use `enumerize` method to generate predicates. This is a more convenient way matching current value of the field with an enum value. Usage the predicate methods is **safe** also. It means that you can't pass to the method invalid enum value neither can have an invalid value in the field:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
class Table
|
66
|
+
extend Enum::Predicates
|
67
|
+
|
68
|
+
attr_accessor :side
|
69
|
+
|
70
|
+
enumerize :side, Side
|
71
|
+
end
|
72
|
+
|
73
|
+
@table = Table.new
|
74
|
+
@table.side_is?(:left) # => false
|
75
|
+
@table.side_is?(nil) # => false
|
76
|
+
|
77
|
+
@table.side = Side.take(:left)
|
78
|
+
@table.side_is?(:left) # => true
|
79
|
+
@table.side_is?(:right) # => false
|
80
|
+
@table.side_is?(nil) # => false
|
81
|
+
@table.side_is?(:invalid) # => Enum::TokenNotFoundError: token 'invalid'' not found in the enum Side
|
82
|
+
|
83
|
+
@table.side = 'invalid'
|
84
|
+
@table.side_is?(nil) # => false
|
85
|
+
@table.side_is?(:left) # => Enum::TokenNotFoundError: token 'invalid'' not found in the enum Side
|
86
|
+
```
|
87
|
+
> 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
|
+
|
89
|
+
It's possible to get index of an enum value with `index` method. It can be convenient in some circumstances:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
class WeekDay < Enum::Base
|
93
|
+
values :sunday, :monday, :tuesday, :wednesday, :thusday, :friday, :saturday
|
94
|
+
end
|
95
|
+
WeekDay.index(:sunday) == Date.new(2015, 9, 13).wday # => true
|
96
|
+
WeekDay.index(:monday) # => 1
|
97
|
+
```
|
98
|
+
|
62
99
|
## Development
|
63
100
|
|
64
101
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/enum.rb
CHANGED
data/lib/enum/base.rb
CHANGED
@@ -14,7 +14,7 @@ module Enum
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def all
|
17
|
-
|
17
|
+
history
|
18
18
|
end
|
19
19
|
|
20
20
|
def take(t)
|
@@ -29,6 +29,10 @@ module Enum
|
|
29
29
|
translate(take(t))
|
30
30
|
end
|
31
31
|
|
32
|
+
def index(token)
|
33
|
+
history.index(take(token))
|
34
|
+
end
|
35
|
+
|
32
36
|
protected
|
33
37
|
|
34
38
|
def store
|
@@ -39,6 +43,14 @@ module Enum
|
|
39
43
|
@store = set
|
40
44
|
end
|
41
45
|
|
46
|
+
def history
|
47
|
+
@history ||= Array.new
|
48
|
+
end
|
49
|
+
|
50
|
+
def history=(ary)
|
51
|
+
@history = ary
|
52
|
+
end
|
53
|
+
|
42
54
|
def translate(token, options = {})
|
43
55
|
I18n.t(token, scope: "enum.#{self}", exception_handler: proc do
|
44
56
|
if superclass == Enum::Base
|
@@ -55,10 +67,12 @@ module Enum
|
|
55
67
|
|
56
68
|
def add_value(val)
|
57
69
|
store.add(val)
|
70
|
+
history.push(val)
|
58
71
|
end
|
59
72
|
|
60
73
|
def init_child_class(child)
|
61
74
|
child.store = self.store.clone
|
75
|
+
child.history = self.history.clone
|
62
76
|
end
|
63
77
|
|
64
78
|
end
|
data/lib/enum/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: safe-enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey Koleshko
|
@@ -86,6 +86,7 @@ files:
|
|
86
86
|
- enum.gemspec
|
87
87
|
- lib/enum.rb
|
88
88
|
- lib/enum/base.rb
|
89
|
+
- lib/enum/predicates.rb
|
89
90
|
- lib/enum/token_not_found_error.rb
|
90
91
|
- lib/enum/version.rb
|
91
92
|
homepage: https://github.com/mezuka/enum
|