safe-enum 0.1.0 → 0.2.0

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
2
  SHA1:
3
- metadata.gz: 8b46454a6c48cc776ce859c3a4ff8c6913b78e3d
4
- data.tar.gz: aa98b2d7f30253991c5d5ad4d2f9a9e550fbb648
3
+ metadata.gz: 00f1140cec43431cbc879909a1b626628e041a6a
4
+ data.tar.gz: 08542bb0baba436e8eb2e5b79db64d5ecdabed00
5
5
  SHA512:
6
- metadata.gz: 32c878b5aeaa9e3443f8c490b21d0d34fe80d4213eee4d8e2f62b6d033886b0a8d46ad1f4a6210e3eca80c3edad2e96ecba7b519f61ac4acfd0d672ad1c8c21e
7
- data.tar.gz: 20c18d7366e5732cf8e4fd64886bbe0efa17d0060863011c43c6aecb5cf53cdf5528e83041c7b455214a6e91b866680d12854ec0e1cc3fdd08f8358b5a779e4c
6
+ metadata.gz: 30e94b5da5fc242107ab922e6ec0f52da85e82092f32ae5dcb047258aefad7cfb1d74936abdcd76cb1f3ab6dc18dd08a09c3b9af7131031ee1738f8fe8254d77
7
+ data.tar.gz: 2e14ca8f1d0e9b2858332b44d4f37327ffaf043e85f91887104041bffacf08d29980583dff7f15957cbf3f39254379a0262adaf516748926d1da3f6565966253
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ *.gem
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 returing `nil` this one will raise the exception **always**. Example of usage:
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.
@@ -1,3 +1,4 @@
1
1
  require 'enum/version'
2
2
  require 'enum/token_not_found_error'
3
3
  require 'enum/base'
4
+ require 'enum/predicates'
@@ -14,7 +14,7 @@ module Enum
14
14
  end
15
15
 
16
16
  def all
17
- store.to_a
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
@@ -0,0 +1,13 @@
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.take(field_value) == enum.take(other)
7
+ else
8
+ false
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module Enum
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.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