safe-enum 0.1.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 8b46454a6c48cc776ce859c3a4ff8c6913b78e3d
4
- data.tar.gz: aa98b2d7f30253991c5d5ad4d2f9a9e550fbb648
2
+ SHA256:
3
+ metadata.gz: 670e321a5a7e1961b6c4c940f720887a7b6dca7725f890cc27777aff7f0f2e17
4
+ data.tar.gz: 739fc2f8c14ccf50bb0265f6c748be615971ddb3e20128886e7c5562c31bd4b3
5
5
  SHA512:
6
- metadata.gz: 32c878b5aeaa9e3443f8c490b21d0d34fe80d4213eee4d8e2f62b6d033886b0a8d46ad1f4a6210e3eca80c3edad2e96ecba7b519f61ac4acfd0d672ad1c8c21e
7
- data.tar.gz: 20c18d7366e5732cf8e4fd64886bbe0efa17d0060863011c43c6aecb5cf53cdf5528e83041c7b455214a6e91b866680d12854ec0e1cc3fdd08f8358b5a779e4c
6
+ metadata.gz: cebfd3861390ba72e8740dc40b300a8b93920f3258832d08ba1c54074caa1ea93c9f4a3b68fada7a427cec76290e37212971483765a920b8ebe0f22f458997e6
7
+ data.tar.gz: 1938ac3742948d2936e951c8a66191ba06406e83ce2e351ac0c8cf29a6c08303f088f94103d77625c520249b80005b9eaf7b4d8f8049949e03cd560754127d36
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ *.gem
data/README.md CHANGED
@@ -1,4 +1,3 @@
1
- ![Travis](https://travis-ci.org/mezuka/enum.svg)
2
1
  # Enum
3
2
 
4
3
  This is a very basic implementation of enums in Ruby. The cornerstone of the library is **safety**.
@@ -8,7 +7,7 @@ This is a very basic implementation of enums in Ruby. The cornerstone of the lib
8
7
  Add this line to your application's Gemfile:
9
8
 
10
9
  ```ruby
11
- gem 'enum', require: 'enum'
10
+ gem 'safe-enum'
12
11
  ```
13
12
 
14
13
  And then execute:
@@ -17,7 +16,7 @@ And then execute:
17
16
 
18
17
  Or install it yourself as:
19
18
 
20
- $ gem install enum
19
+ $ gem install safe-enum
21
20
 
22
21
  ## Usage
23
22
 
@@ -28,13 +27,27 @@ class Side < Enum::Base
28
27
  end
29
28
  ```
30
29
 
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 returing `nil` this one will raise the exception **always**. Example of usage:
30
+ 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
31
 
33
32
  ```ruby
34
- Side.take(:left) # => "left"
35
- Side.take('left') # => "left"
36
- Side.take(:invalid) # => Enum::TokenNotFoundError: token 'invalid'' not found in the enum Side
37
- Side.take('invalid') # => Enum::TokenNotFoundError: token 'invalid'' not found in the enum Side
33
+ Side.enum(:left) # => "left"
34
+ Side.enum('left') # => "left"
35
+ Side.enum(:invalid) # => Enum::TokenNotFoundError: token 'invalid'' not found in the enum Side
36
+ Side.enum('invalid') # => Enum::TokenNotFoundError: token 'invalid'' not found in the enum Side
37
+ ```
38
+
39
+ Get all defined enum values with the `all` method:
40
+
41
+ ```ruby
42
+ Side.all # => ['left', 'rigth', 'whole']
43
+ ```
44
+
45
+ > Order or the returned values in the same as their definition. It's guaranteed.
46
+
47
+ In order to get array of defined enums safely use `enums` method:
48
+
49
+ ```ruby
50
+ Side.enums(:left, :right) # => ['left', 'right']
38
51
  ```
39
52
 
40
53
  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:
@@ -59,6 +72,47 @@ Side.name(:invalid) # => Enum::TokenNotFoundError: token 'invalid'' not found in
59
72
 
60
73
  > If you don't have installed `I18n` in your project `NameError` exception will be raised on the `name` method call.
61
74
 
75
+ 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:
76
+
77
+ ```ruby
78
+ class Table
79
+ extend Enum::Predicates
80
+
81
+ attr_accessor :side
82
+
83
+ enumerize :side, Side
84
+ end
85
+
86
+ @table = Table.new
87
+ @table.side_is?(:left) # => false
88
+ @table.side_is?(nil) # => false
89
+
90
+ @table.side = Side.enum(:left)
91
+ @table.side_is?(:left) # => true
92
+ @table.side_is?(:right) # => false
93
+ @table.side_is?(nil) # => false
94
+ @table.side_is?(:invalid) # => Enum::TokenNotFoundError: token 'invalid'' not found in the enum Side
95
+
96
+ @table.side = 'invalid'
97
+ @table.side_is?(nil) # => false
98
+ @table.side_is?(:left) # => Enum::TokenNotFoundError: token 'invalid'' not found in the enum Side
99
+ @table.side_any?(:left, :right) # => true
100
+ @table.side_any?(:right) # => false
101
+ @table.side_any?(:invalid, :left) # => Enum::TokenNotFoundError: token 'invalid'' not found in the enum Side
102
+ ```
103
+ > 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?`.
104
+
105
+ It's possible to get index of an enum value with `index` method. It can be convenient in some circumstances:
106
+
107
+ ```ruby
108
+ class WeekDay < Enum::Base
109
+ values :sunday, :monday, :tuesday, :wednesday, :thusday, :friday, :saturday
110
+ end
111
+ WeekDay.index(:sunday) == Date.new(2015, 9, 13).wday # => true
112
+ WeekDay.index(:monday) # => 1
113
+ WeekDay.indexes # => [0, 1, 2, 3, 4, 5, 6]
114
+ ```
115
+
62
116
  ## Development
63
117
 
64
118
  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/base.rb CHANGED
@@ -14,10 +14,22 @@ module Enum
14
14
  end
15
15
 
16
16
  def all
17
- store.to_a
17
+ history
18
18
  end
19
19
 
20
- def take(t)
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,7 +38,11 @@ module Enum
26
38
  end
27
39
 
28
40
  def name(t)
29
- translate(take(t))
41
+ translate(enum(t))
42
+ end
43
+
44
+ def index(token)
45
+ history.index(enum(token))
30
46
  end
31
47
 
32
48
  protected
@@ -39,10 +55,18 @@ module Enum
39
55
  @store = set
40
56
  end
41
57
 
58
+ def history
59
+ @history ||= Array.new
60
+ end
61
+
62
+ def history=(ary)
63
+ @history = ary
64
+ end
65
+
42
66
  def translate(token, options = {})
43
67
  I18n.t(token, scope: "enum.#{self}", exception_handler: proc do
44
68
  if superclass == Enum::Base
45
- I18n.t(token, options.merge(scope: "enum.#{self}"))
69
+ I18n.t(token, **options.merge(scope: "enum.#{self}"))
46
70
  else
47
71
  superclass.translate(token, exception_handler: proc do
48
72
  I18n.t(token, scope: "enum.#{self}")
@@ -55,10 +79,12 @@ module Enum
55
79
 
56
80
  def add_value(val)
57
81
  store.add(val)
82
+ history.push(val)
58
83
  end
59
84
 
60
85
  def init_child_class(child)
61
86
  child.store = self.store.clone
87
+ child.history = self.history.clone
62
88
  end
63
89
 
64
90
  end
@@ -0,0 +1,18 @@
1
+ module Enum
2
+ module Predicates
3
+ def enumerize(field, enum)
4
+ define_method("#{field}_is?") do |other|
5
+ if (field_value = public_send(field)) && other
6
+ enum.enum(field_value) == enum.enum(other)
7
+ else
8
+ false
9
+ end
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
16
+ end
17
+ end
18
+ end
data/lib/enum/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Enum
2
- VERSION = "0.1.0"
2
+ VERSION = '0.3.1'
3
3
  end
data/lib/enum.rb CHANGED
@@ -1,3 +1,12 @@
1
1
  require 'enum/version'
2
2
  require 'enum/token_not_found_error'
3
3
  require 'enum/base'
4
+ require 'enum/predicates'
5
+
6
+ module Enum
7
+ def self.[](*ary)
8
+ Class.new(Base) do
9
+ values(*ary)
10
+ end
11
+ end
12
+ end
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.1.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Koleshko
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-09-14 00:00:00.000000000 Z
11
+ date: 2022-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,13 +86,15 @@ 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
92
+ - lib/safe-enum.rb
91
93
  homepage: https://github.com/mezuka/enum
92
94
  licenses:
93
95
  - MIT
94
96
  metadata: {}
95
- post_install_message:
97
+ post_install_message:
96
98
  rdoc_options: []
97
99
  require_paths:
98
100
  - lib
@@ -107,9 +109,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
109
  - !ruby/object:Gem::Version
108
110
  version: '0'
109
111
  requirements: []
110
- rubyforge_project:
111
- rubygems_version: 2.4.5.1
112
- signing_key:
112
+ rubygems_version: 3.1.2
113
+ signing_key:
113
114
  specification_version: 4
114
115
  summary: Enum implementation
115
116
  test_files: []