enum_class 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/.travis.yml +7 -1
- data/README.md +9 -6
- data/enum_class.gemspec +1 -0
- data/lib/enum.rb +19 -7
- data/lib/enum_class.rb +0 -4
- data/lib/enum_class/version.rb +4 -1
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e0ea8d8e6d54a15a3c13af072ea7a9f82556623d
|
4
|
+
data.tar.gz: f42b49f63f8c8df27bc9024994bdab0cfab4e0e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 26a18e0989467083864440f46e803aee30dc1a21e2da5f6076639bfd73b35d63b6e18ced54a3ec735f58e85af0f427cb27a4f20080940444a891640cd8a20b4b
|
7
|
+
data.tar.gz: ec1c235732fa550ebb12a4ca80a90f407ad885ced8785017f7ea394d5242924218222adea318f4e307d10787cdbd4a4f95421e71e11eb515d48a9ab2d61cac5b
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.4.1
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,10 +1,13 @@
|
|
1
|
-
#
|
1
|
+
# Enum
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/enum_class.svg)](https://badge.fury.io/rb/enum_class)
|
4
|
+
[![Build Status](https://travis-ci.org/LIQIDTechnology/enum_class.svg?branch=master)](https://travis-ci.org/LIQIDTechnology/enum_class)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/github/LIQIDTechnology/enum_class/badge.svg?branch=master)](https://coveralls.io/github/LIQIDTechnology/enum_class?branch=master)
|
6
|
+
[![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/gems/enum_class/)
|
7
|
+
[![Documentation coverage](https://inch-ci.org/github/LIQIDTechnology/enum_class.svg?branch=master)](https://inch-ci.org/github/LIQIDTechnology/enum_class)
|
8
|
+
|
9
|
+
This gem is an attempt to provide a lightweight (< 300 LOC) solution for __object-oriented enumerations in Ruby__. It consists in a single class (`Enum`), that can be subclassed to create enumerations and comes with no dependencies. It stemmed out from the necessity of handling reliably features like inheritance and support for instance methods on enumerated
|
10
|
+
objects, features that we couldn't find in other [similar](https://github.com/capnregex/enum) [gems](https://github.com/dblock/ruby-enum).
|
8
11
|
|
9
12
|
## Installation
|
10
13
|
|
data/enum_class.gemspec
CHANGED
data/lib/enum.rb
CHANGED
@@ -156,7 +156,17 @@ class Enum
|
|
156
156
|
|
157
157
|
protected
|
158
158
|
|
159
|
-
#
|
159
|
+
# When an enumeration is subclassed, this method is called as a constructor instead of #{initialize}
|
160
|
+
# for building values in the child class corresponding to those present in the parent class.
|
161
|
+
#
|
162
|
+
# For the inherited values, #{initialize} is skipped and this method is called instead, receiving as
|
163
|
+
# parameter the value from the parent class.
|
164
|
+
#
|
165
|
+
# Subclasses should override this to provide custom behavior; the default implementation copies all the
|
166
|
+
# instance variables from the provided object to the newly instanciated one.
|
167
|
+
#
|
168
|
+
# @param enum_value [Enum] value of the parent enumeration
|
169
|
+
# @return [void]
|
160
170
|
def initialize_from_superclass(enum_value)
|
161
171
|
enum_value.instance_variables.each do |ivar|
|
162
172
|
instance_variable_set(ivar, enum_value.instance_variable_get(ivar))
|
@@ -171,7 +181,7 @@ class Enum
|
|
171
181
|
# @raise [KeyError] if there is no enumerated value with the specified key
|
172
182
|
def for(value)
|
173
183
|
return value if value.is_a? self.class
|
174
|
-
enum_key = value
|
184
|
+
enum_key = value.nil? ? nil : value.upcase.to_sym
|
175
185
|
values.find(proc { raise KeyError, "Enumerated value not found: #{value.inspect}" }) { |v| v.key == enum_key }
|
176
186
|
end
|
177
187
|
|
@@ -184,10 +194,6 @@ class Enum
|
|
184
194
|
|
185
195
|
alias [] for
|
186
196
|
|
187
|
-
def inspect
|
188
|
-
"#{name || 'Enum'}(#{values.map(&:to_s).join(', ')})"
|
189
|
-
end
|
190
|
-
|
191
197
|
def const_missing(sym)
|
192
198
|
self.for(sym)
|
193
199
|
rescue
|
@@ -232,7 +238,7 @@ class Enum
|
|
232
238
|
|
233
239
|
raise DuplicateEnumKey, "#{enum_value.inspect} is already defined" if values.include?(enum_value)
|
234
240
|
|
235
|
-
max_ord = values.max
|
241
|
+
max_ord = values.max ? values.max.ord : -1
|
236
242
|
enum_value.instance_variable_set(:@ord, max_ord + 1)
|
237
243
|
|
238
244
|
values << enum_value
|
@@ -240,6 +246,12 @@ class Enum
|
|
240
246
|
define_method("#{key.downcase}?") { @key == key }
|
241
247
|
define_singleton_method(key.to_s.downcase) { self.for(key) }
|
242
248
|
end
|
249
|
+
|
250
|
+
public
|
251
|
+
|
252
|
+
def inspect
|
253
|
+
"#{name || 'Enum'}(#{values.map(&:to_s).join(', ')})"
|
254
|
+
end
|
243
255
|
end
|
244
256
|
|
245
257
|
private_class_method :new
|
data/lib/enum_class.rb
CHANGED
data/lib/enum_class/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enum_class
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michele Piccirillo
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: coveralls
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
description:
|
70
84
|
email:
|
71
85
|
- michele@liqid.de
|
@@ -75,6 +89,7 @@ extra_rdoc_files: []
|
|
75
89
|
files:
|
76
90
|
- ".gitignore"
|
77
91
|
- ".rspec"
|
92
|
+
- ".ruby-version"
|
78
93
|
- ".travis.yml"
|
79
94
|
- CODE_OF_CONDUCT.md
|
80
95
|
- Gemfile
|
@@ -107,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
122
|
version: '0'
|
108
123
|
requirements: []
|
109
124
|
rubyforge_project:
|
110
|
-
rubygems_version: 2.
|
125
|
+
rubygems_version: 2.6.11
|
111
126
|
signing_key:
|
112
127
|
specification_version: 4
|
113
128
|
summary: Object-oriented enumerations for Ruby
|