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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fb01aaee4990bae960203ed343c56c3138dafc9d
4
- data.tar.gz: fac9f38332e3d05bdd69346debd68dbac255d173
3
+ metadata.gz: e0ea8d8e6d54a15a3c13af072ea7a9f82556623d
4
+ data.tar.gz: f42b49f63f8c8df27bc9024994bdab0cfab4e0e4
5
5
  SHA512:
6
- metadata.gz: 519b31b4e7485d849ad9fd6c368ee996206d1f8109564bb420d8cf2fec6b8110e80754a746703476df985dba6ede49519d5214b7d0ff30d20d771c0ecb053b64
7
- data.tar.gz: 3c297a574c757079148b82f891f5597e57e9d4b70da6a284a7575a6b2d8ae072029f21b5dc70455e96733f8ab1a9fb7b1af688c5c41665f059bffd7b6f5980a4
6
+ metadata.gz: 26a18e0989467083864440f46e803aee30dc1a21e2da5f6076639bfd73b35d63b6e18ced54a3ec735f58e85af0f427cb27a4f20080940444a891640cd8a20b4b
7
+ data.tar.gz: ec1c235732fa550ebb12a4ca80a90f407ad885ced8785017f7ea394d5242924218222adea318f4e307d10787cdbd4a4f95421e71e11eb515d48a9ab2d61cac5b
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.1
data/.travis.yml CHANGED
@@ -1,5 +1,11 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.3.1
4
+ rvm:
5
+ - 2.0.0-p648
6
+ - 2.1.10
7
+ - 2.2.7
8
+ - 2.3.4
9
+ - 2.4.1
10
+ - jruby-9.1.5.0
5
11
  before_install: gem install bundler -v 1.12.4
data/README.md CHANGED
@@ -1,10 +1,13 @@
1
- # EnumClass
1
+ # Enum
2
2
 
3
- This gem is an attempt to provide a lightweight solution for __object-oriented enumerations in Ruby__. It consists
4
- in a single class (`Enum`), that can be subclassed to create enumerations and comes with no dependencies. It's similar
5
- in design to some [other](https://github.com/capnregex/enum) [gems](https://github.com/dblock/ruby-enum) available, but
6
- it stemmed out from the necessity of handling reliably missing features like inheritance and instance methods of the enumerated
7
- object.
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
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
23
  spec.add_development_dependency "rspec", "~> 3.0"
24
24
  spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "coveralls"
25
26
  end
data/lib/enum.rb CHANGED
@@ -156,7 +156,17 @@ class Enum
156
156
 
157
157
  protected
158
158
 
159
- # Initializes
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&.upcase&.to_sym
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&.ord || -1
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
@@ -1,7 +1,3 @@
1
1
  # frozen_string_literal: true
2
-
3
2
  require 'enum_class/version'
4
3
  require 'enum'
5
-
6
- module EnumClass
7
- end
@@ -1,3 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @!visibility private
1
4
  module EnumClass
2
- VERSION = "0.1.0"
5
+ VERSION = "0.1.1"
3
6
  end
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.0
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.5.1
125
+ rubygems_version: 2.6.11
111
126
  signing_key:
112
127
  specification_version: 4
113
128
  summary: Object-oriented enumerations for Ruby