safe-enum 0.2.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 00f1140cec43431cbc879909a1b626628e041a6a
4
- data.tar.gz: 08542bb0baba436e8eb2e5b79db64d5ecdabed00
3
+ metadata.gz: b01a24ce4b8682ef047734ceda2a6d42f7f20654
4
+ data.tar.gz: 344c9fbee4e130f694c287e4a8a2a5e29e063126
5
5
  SHA512:
6
- metadata.gz: 30e94b5da5fc242107ab922e6ec0f52da85e82092f32ae5dcb047258aefad7cfb1d74936abdcd76cb1f3ab6dc18dd08a09c3b9af7131031ee1738f8fe8254d77
7
- data.tar.gz: 2e14ca8f1d0e9b2858332b44d4f37327ffaf043e85f91887104041bffacf08d29980583dff7f15957cbf3f39254379a0262adaf516748926d1da3f6565966253
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', require: '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 `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:
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.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
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.take(:left)
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
@@ -17,7 +17,19 @@ module Enum
17
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,11 +38,11 @@ module Enum
26
38
  end
27
39
 
28
40
  def name(t)
29
- translate(take(t))
41
+ translate(enum(t))
30
42
  end
31
43
 
32
44
  def index(token)
33
- history.index(take(token))
45
+ history.index(enum(token))
34
46
  end
35
47
 
36
48
  protected
@@ -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.take(field_value) == enum.take(other)
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
@@ -1,3 +1,3 @@
1
1
  module Enum
2
- VERSION = "0.2.0"
2
+ VERSION = '0.3.0'
3
3
  end
@@ -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.2.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: 2015-09-14 00:00:00.000000000 Z
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.4.5.1
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: