meta_enum 2.0.1 → 2.1.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
  SHA256:
3
- metadata.gz: c864943910fad7b9ca83fbcfa8a8e514c5a9675e669e3f45ec5bc32e924d85c3
4
- data.tar.gz: 64be15e7024853f90efb66fff368d3afb8b0093957ee9d010dbc31bd5ec7bc0e
3
+ metadata.gz: d2713c2ae0ded30fe7aacea8cf0365c70ef1e7ad2455b29f240ec041396a2037
4
+ data.tar.gz: c80391638b6e58a29c9f666e6f00b5899f030e3e5cf5bec6061b1a8da4e59082
5
5
  SHA512:
6
- metadata.gz: b350f51a27a0f119d2258a67de7fe29baf2f7a23fc64c0c4c43101e95cfcb27e21f7e3a49751d3365630f76dccd7379548705c573b826486f24baca2ce7a5448
7
- data.tar.gz: 4ab4a6437a9490ba25bbaf9c607c500c4b935278f2991c41836a26a7d4fea0c5193b502bed7837a0a1f7656469b26c6a82594230b9483c7a1f26b7f70ca8e894
6
+ metadata.gz: a0800fec13293bcdd73457f90820b6d60ecd20e949ba8558d5871fdf287b8e16c17847ffc3a59548fd2c750123eda3d97f1c5e4f581933153725d8c7b4670848
7
+ data.tar.gz: c79c507c8da924ab42851c580d8f8053ce6b97feb6564759162d9396c9b6bf18742c1c4241f04d38e230ae91feae5c0799dc54c77ed05455084d9ac4338c4003
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.1.0 (May 31, 2018)
4
+
5
+ * Add element_class optional argument to MetaEnum::Type.new to customize element class.
6
+
3
7
  ## 2.0.1 (February 8, 2018)
4
8
 
5
9
  * Fix MetaEnum::Type#inspect
@@ -7,7 +11,7 @@
7
11
 
8
12
  ## 2.0.0 (January 27, 2018)
9
13
 
10
- Support non-integer values.
14
+ * Support non-integer values.
11
15
 
12
16
  This entailed some breaking changes:
13
17
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- meta_enum (2.0.1)
4
+ meta_enum (2.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -81,7 +81,29 @@ Non-integer values can be enabled by passing a value_normalizer to `MetaEnum::Ty
81
81
  CardType = MetaEnum::Type.new({visa: "VS", mastercard: "MC", discover: "DS"}, value_normalizer: method(:String))
82
82
  CardType[:visa] # => #<MetaEnum::Element: visa: "VS", data: nil>
83
83
  CardType["VS"] # => #<MetaEnum::Element: visa: "VS", data: nil>
84
- pry(main)> CardType["VS"].value # => "VS"
84
+ CardType["VS"].value # => "VS"
85
+ ```
86
+
87
+ The class used to create the elements can be customized with the `element_class` argument.
88
+
89
+ ```ruby
90
+ AgeType = MetaEnum::Type.new(
91
+ {
92
+ child: [0, {description: "Less than 18", range: 0...18}],
93
+ adult: [1, {description: "At least 18", range: 18...Float::INFINITY}]
94
+ },
95
+ element_class: Class.new(MetaEnum::Element) {
96
+ def description
97
+ data[:description]
98
+ end
99
+
100
+ def range
101
+ data[:range]
102
+ end
103
+ }
104
+ )
105
+ AgeType[:child].description # => "Less than 18"
106
+ AgeType[:child].range # => 0...18
85
107
  ```
86
108
 
87
109
  ## Development
@@ -31,9 +31,13 @@ module MetaEnum
31
31
  #
32
32
  # value_normalizer is a callable object that normalizes values. The default
33
33
  # converts all values to integers. To allow string values use method(:String).
34
+ #
35
+ # element_class is the class with which to create elements. It should be a
36
+ # sub-class of MetaEnum::Element (or otherwise match it's behavior).
34
37
  def initialize(
35
38
  elements,
36
- value_normalizer: method(:Integer)
39
+ value_normalizer: method(:Integer),
40
+ element_class: MetaEnum::Element
37
41
  )
38
42
  @value_normalizer = value_normalizer
39
43
  @elements_by_value = {}
@@ -42,7 +46,7 @@ module MetaEnum
42
46
 
43
47
  elements.each do |name, value_and_data|
44
48
  value_and_data = Array(value_and_data)
45
- v = Element.new normalize_value(value_and_data[0]), name, value_and_data[1], self
49
+ v = element_class.new normalize_value(value_and_data[0]), name, value_and_data[1], self
46
50
  raise ArgumentError, "duplicate value: #{v.value}" if @elements_by_value.key? v.value
47
51
  raise ArgumentError, "duplicate name: #{v.name}" if @elements_by_name.key? v.name
48
52
  @elements_by_value[v.value] = v
@@ -1,3 +1,3 @@
1
1
  module MetaEnum
2
- VERSION = "2.0.1"
2
+ VERSION = "2.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: meta_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jack Christensen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-08 00:00:00.000000000 Z
11
+ date: 2018-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -109,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
109
  version: '0'
110
110
  requirements: []
111
111
  rubyforge_project:
112
- rubygems_version: 2.7.3
112
+ rubygems_version: 2.7.6
113
113
  signing_key:
114
114
  specification_version: 4
115
115
  summary: Friendly enum type system