safe-hash-enum 0.4.2 → 0.4.3
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 +45 -43
- data/lib/enum/base.rb +5 -0
- data/lib/enum/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '072928a0df967986d036f27aa5b5dd2db382b1f72be26d95b99311db6bb24ace'
|
4
|
+
data.tar.gz: 93bdc11f44febe4ec26c656f30116719d019e4550cbfbf02701d71e01072f8f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0c6256bc0a9f820b3f0a094b8bbf4e5ac487f16da6e7e3277f374e08742d7e5fbc6c556fcbfcdcf62a592c0453a8c3e39e1348ea014510bb69d880c0658a2a04
|
7
|
+
data.tar.gz: d2b913cc32cf59188b245a86164874e285102fd9eacdf2506101a0d9715977f95cb13dd72a1830a80df12d6701d803f4ec7f40105dcc0501f0cdb579f70d9ee0
|
data/README.md
CHANGED
@@ -1,14 +1,13 @@
|
|
1
|
-

|
2
1
|
# Enum
|
3
2
|
|
4
|
-
This is a very basic implementation of enums in Ruby. The cornerstone of the library is **safety**.
|
3
|
+
This is a very basic implementation of enums in Ruby. Forked from [mezuka/enum](https://github.com/mezuka/enum). The cornerstone of the mezuka library is **safety**. This implementation uses **hashes** instead of strings to create enums. It is a work in progress and evolves depending on developers needs.
|
5
4
|
|
6
5
|
## Installation
|
7
6
|
|
8
7
|
Add this line to your application's Gemfile:
|
9
8
|
|
10
9
|
```ruby
|
11
|
-
gem 'safe-enum'
|
10
|
+
gem 'safe-hash-enum'
|
12
11
|
```
|
13
12
|
|
14
13
|
And then execute:
|
@@ -17,62 +16,80 @@ And then execute:
|
|
17
16
|
|
18
17
|
Or install it yourself as:
|
19
18
|
|
20
|
-
$ gem install safe-enum
|
19
|
+
$ gem install safe-hash-enum
|
21
20
|
|
22
21
|
## Usage
|
23
22
|
|
24
23
|
Define set of enums with code like this:
|
25
24
|
```ruby
|
26
25
|
class Side < Enum::Base
|
27
|
-
values
|
26
|
+
values({left: 'my_left', right: 'my_right'})
|
28
27
|
end
|
29
28
|
```
|
30
29
|
|
31
|
-
Now get a
|
30
|
+
Now get a safely defined hash with the `enum` method with its `Symbol` or `String` type as argument. If there is no defined such hash `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 hash or fixing the source of the invalid hash. 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
31
|
|
33
32
|
```ruby
|
34
|
-
Side.enum(:left) # =>
|
35
|
-
Side.enum('left') # =>
|
36
|
-
Side.enum(:invalid) # => Enum::TokenNotFoundError: token 'invalid'' not found in
|
37
|
-
Side.enum('invalid') # => Enum::TokenNotFoundError: token 'invalid'' not found in
|
33
|
+
Side.enum(:left) # => {:left => "my_left"}
|
34
|
+
Side.enum('left') # => {:left => "my_left"}
|
35
|
+
Side.enum(:invalid) # => Enum::TokenNotFoundError: token 'invalid'' not found in Side
|
36
|
+
Side.enum('invalid') # => Enum::TokenNotFoundError: token 'invalid'' not found in Side
|
37
|
+
```
|
38
|
+
|
39
|
+
Get value of hash by token (String or Symbol):
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
Side.value(:left) # => "my_left"
|
43
|
+
Side.value('left') # => "my_left"
|
44
|
+
Side.value(:invalid) # => Enum::TokenNotFoundError: token 'invalid'' not found in Side
|
45
|
+
Side.value('invalid') # => Enum::TokenNotFoundError: token 'invalid'' not found in Side
|
38
46
|
```
|
39
47
|
|
40
48
|
Get all defined enum values with the `all` method:
|
41
49
|
|
42
50
|
```ruby
|
43
|
-
Side.all # => [
|
51
|
+
Side.all # => [:left, :right]
|
44
52
|
```
|
45
53
|
|
46
|
-
> Order
|
54
|
+
> Order of the returned values in the same as their definition. It's guaranteed.
|
47
55
|
|
48
56
|
In order to get array of defined enums safely use `enums` method:
|
49
57
|
|
50
58
|
```ruby
|
51
|
-
Side.enums(:left, :right) # => [
|
59
|
+
Side.enums(:left, :right) # => [{:left => "my_left"}, {:right => "my_right"}]
|
52
60
|
```
|
53
61
|
|
54
|
-
|
62
|
+
To get the token by value use `find_value` method :
|
55
63
|
|
56
|
-
```
|
57
|
-
|
58
|
-
enum:
|
59
|
-
Side:
|
60
|
-
left: 'Left'
|
61
|
-
right: 'Right'
|
64
|
+
```ruby
|
65
|
+
Side.find_value('my_left') # => {:left => "my_left"}
|
62
66
|
```
|
63
67
|
|
64
|
-
|
68
|
+
To get the index of a token use `index` method :
|
65
69
|
|
66
70
|
```ruby
|
67
|
-
Side.
|
68
|
-
Side.
|
69
|
-
Side.name(:right) # => "Right"
|
70
|
-
Side.name('right') # => "Right"
|
71
|
-
Side.name(:invalid) # => Enum::TokenNotFoundError: token 'invalid'' not found in the enum Side
|
71
|
+
Side.index(:left) # => 0
|
72
|
+
Side.index(:right) # => 1
|
72
73
|
```
|
73
74
|
|
74
|
-
|
75
|
+
Note : the predicates feature in mezuka/enum isn't implemented yet with hashes
|
76
|
+
|
77
|
+
## Development
|
78
|
+
|
79
|
+
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.
|
75
80
|
|
81
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
82
|
+
|
83
|
+
## Contributing
|
84
|
+
|
85
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/netika-res/enum. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
86
|
+
|
87
|
+
## License
|
88
|
+
|
89
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
90
|
+
|
91
|
+
---
|
92
|
+
## TODO ADAPT FROM MEZUKA (not yet implemented with hash)
|
76
93
|
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:
|
77
94
|
|
78
95
|
```ruby
|
@@ -113,19 +130,4 @@ WeekDay.index(:sunday) == Date.new(2015, 9, 13).wday # => true
|
|
113
130
|
WeekDay.index(:monday) # => 1
|
114
131
|
WeekDay.indexes # => [0, 1, 2, 3, 4, 5, 6]
|
115
132
|
```
|
116
|
-
|
117
|
-
## Development
|
118
|
-
|
119
|
-
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.
|
120
|
-
|
121
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
122
|
-
|
123
|
-
## Contributing
|
124
|
-
|
125
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/mezuka/enum. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
126
|
-
|
127
|
-
|
128
|
-
## License
|
129
|
-
|
130
|
-
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
131
|
-
|
133
|
+
---
|
data/lib/enum/base.rb
CHANGED
data/lib/enum/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: safe-hash-enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- NETIKA real estate solution s.a.
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-07-
|
11
|
+
date: 2019-07-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|