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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 304e864000cd9009e884b5a9722d045453a575a53cc8ad550d3a003934e3cbe7
4
- data.tar.gz: 2a20b309e7588c9bc35c5795eea9f56bd47203227e7e431df2191fea2945c82f
3
+ metadata.gz: '072928a0df967986d036f27aa5b5dd2db382b1f72be26d95b99311db6bb24ace'
4
+ data.tar.gz: 93bdc11f44febe4ec26c656f30116719d019e4550cbfbf02701d71e01072f8f1
5
5
  SHA512:
6
- metadata.gz: e2fae3be5f296d03585737378424d23eabc6aced6f782202378fd6ec1d7031ccf7d9977a12ab7b7eff0ba0ee6d5b8d642662a08075c97b6301c1fd0b3d4e89df
7
- data.tar.gz: 02617f7cc9c0a266a49be008b2b57800540d8864ce7627d2aa5de2efcd2a9a054be114d15f5374d97aee43a16af75c25e2ecd2da8854e361c476b64c2ee61127
6
+ metadata.gz: 0c6256bc0a9f820b3f0a094b8bbf4e5ac487f16da6e7e3277f374e08742d7e5fbc6c556fcbfcdcf62a592c0453a8c3e39e1348ea014510bb69d880c0658a2a04
7
+ data.tar.gz: d2b913cc32cf59188b245a86164874e285102fd9eacdf2506101a0d9715977f95cb13dd72a1830a80df12d6701d803f4ec7f40105dcc0501f0cdb579f70d9ee0
data/README.md CHANGED
@@ -1,14 +1,13 @@
1
- ![Travis](https://travis-ci.org/mezuka/enum.svg)
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 :left, :right
26
+ values({left: 'my_left', right: 'my_right'})
28
27
  end
29
28
  ```
30
29
 
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:
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) # => "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
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 # => ['left', 'rigth', 'whole']
51
+ Side.all # => [:left, :right]
44
52
  ```
45
53
 
46
- > Order or the returned values in the same as their definition. It's guaranteed.
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) # => ['left', 'right']
59
+ Side.enums(:left, :right) # => [{:left => "my_left"}, {:right => "my_right"}]
52
60
  ```
53
61
 
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:
62
+ To get the token by value use `find_value` method :
55
63
 
56
- ```yml
57
- en:
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
- The `name` method usage example:
68
+ To get the index of a token use `index` method :
65
69
 
66
70
  ```ruby
67
- Side.name(:left) # => "Left"
68
- Side.name('left') # => "Left"
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
- > If you don't have installed `I18n` in your project `NameError` exception will be raised on the `name` method call.
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
@@ -40,6 +40,11 @@ module Enum
40
40
  end
41
41
  end
42
42
 
43
+ def value(t)
44
+ e = enum(t)
45
+ e[t]
46
+ end
47
+
43
48
  def find_value(v)
44
49
  key = nil
45
50
  store.each do |e|
data/lib/enum/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Enum
2
- VERSION = '0.4.2'
2
+ VERSION = '0.4.3'
3
3
  end
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.2
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-18 00:00:00.000000000 Z
11
+ date: 2019-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler