enum_class 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +50 -34
- data/enum_class.gemspec +9 -0
- data/lib/enum_class/version.rb +1 -1
- metadata +8 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 05d16b831418f03c47d59dca4a82f327bdcf9703
|
4
|
+
data.tar.gz: 89ff063b0113f4756c7dcd42a489b66da34dd8d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c36cc19f57bc8169a2969b63ab2e903c2509097734077a89b9e3fb4943bf2c702b84c6cff76735d447aa25e6808212c37a2d6ee32d12dfd1e3be9c639a39f03
|
7
|
+
data.tar.gz: 41277b7d42e6b99c1f0b4b61607a683226f65f8aa229eb23bba69155c78f30034bf54227d5e377dd2650db4337cf0f7dd44a5489af67d6854b2b8e33061793ee
|
data/README.md
CHANGED
@@ -1,13 +1,18 @@
|
|
1
1
|
# Enum
|
2
2
|
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/enum_class.svg)](https://
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/enum_class.svg)](https://rubygems.org/gems/enum_class)
|
4
4
|
[![Build Status](https://travis-ci.org/LIQIDTechnology/enum_class.svg?branch=master)](https://travis-ci.org/LIQIDTechnology/enum_class)
|
5
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
6
|
[![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/gems/enum_class/)
|
7
7
|
[![Documentation coverage](https://inch-ci.org/github/LIQIDTechnology/enum_class.svg?branch=master)](https://inch-ci.org/github/LIQIDTechnology/enum_class)
|
8
8
|
|
9
|
-
This gem is an attempt to provide a lightweight (< 300 LOC) solution for __object-oriented enumerations in Ruby__.
|
10
|
-
|
9
|
+
This gem is an attempt to provide a lightweight (< 300 LOC, no dependencies) solution for __object-oriented enumerations in Ruby__. Ruby does not provide out of the box an enumerated type and the typical way of handling enumerated values is by using lists of symbols or strings. Although this is fine for simple cases, in more involved scenarios it leaves to the programmer the burden to store, validate and associate meaning and behaviour to the possible values.
|
10
|
+
|
11
|
+
Other languages, such as Java, have [native support for enum types](https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html), providing a way of encapsulating the meaning and behaviour of the values in an object-oriented way.
|
12
|
+
|
13
|
+
This library attempts to bring a similar functionality to Ruby. It consists of a single class (`Enum`), that can be subclassed to create enumerated types. Each class has a set of predefined instances, that represent the enumerated values.
|
14
|
+
|
15
|
+
A few [similar](https://github.com/capnregex/enum) [gems](https://github.com/dblock/ruby-enum) have been built before, inspired by a similar design, but they missed features like support for inheritance and declaring instance methods on enumerated values, so this gem attempts to bridge that gap.
|
11
16
|
|
12
17
|
## Installation
|
13
18
|
|
@@ -30,59 +35,70 @@ Or install it yourself as:
|
|
30
35
|
### Basic usage
|
31
36
|
|
32
37
|
```ruby
|
33
|
-
class
|
34
|
-
value :
|
35
|
-
value :
|
38
|
+
class Weekday < Enum
|
39
|
+
value :MONDAY
|
40
|
+
value :TUESDAY
|
41
|
+
value :WEDNESDAY
|
42
|
+
value :THURSDAY
|
43
|
+
value :FRIDAY
|
44
|
+
value :SATURDAY
|
45
|
+
value :SUNDAY
|
36
46
|
end
|
37
47
|
|
38
|
-
|
48
|
+
Weekday.inspect # => Weekday(MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, ...)
|
49
|
+
|
50
|
+
# Instances are accessible as constants or as class methods
|
51
|
+
Weekday::MONDAY.class # => Weekday
|
52
|
+
Weekday::TUESDAY.class # => Weekday
|
39
53
|
|
40
|
-
#
|
41
|
-
|
42
|
-
|
43
|
-
|
54
|
+
Weekday.monday # => Weekday::MONDAY
|
55
|
+
Weekday.tuesday # => Weekday::TUESDAY
|
56
|
+
|
57
|
+
Weekday::FOO # => raises NameError
|
58
|
+
Weekday.foo # => raises NameError
|
44
59
|
|
45
60
|
# Enumerated values are implicitly ordered, depending on order of definition
|
46
|
-
|
47
|
-
|
61
|
+
Weekday::MONDAY == Weekday::TUESDAY # => false
|
62
|
+
Weekday::MONDAY < Weekday::TUESDAY # => true
|
48
63
|
|
49
|
-
# Instances can be retrieved by key
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
64
|
+
# Instances can be retrieved by key; keys are case-insensitive
|
65
|
+
Weekday[:monday] # => Weekday::MONDAY
|
66
|
+
Weekday['monday'] # => Weekday::MONDAY
|
67
|
+
Weekday.for('Monday') # => Weekday::MONDAY
|
68
|
+
Weekday.for('X') # => raises KeyError
|
54
69
|
|
55
|
-
|
70
|
+
Weekday.new # => raises an error
|
56
71
|
|
57
|
-
#
|
58
|
-
|
59
|
-
|
72
|
+
# Inspection methods
|
73
|
+
Weekday::MONDAY.monday? # => true
|
74
|
+
Weekday::TUESDAY.monday? # => false
|
60
75
|
|
61
|
-
|
62
|
-
|
76
|
+
# Enum classes implement all methods of Enumerable
|
77
|
+
Weekday.map { |weekday| "#{weekday.key}!" } # => ['MONDAY!', 'TUESDAY!', 'WEDNESDAY!', ...]
|
63
78
|
```
|
64
79
|
|
65
80
|
### Creating enums with a shortcut
|
66
81
|
|
67
82
|
```ruby
|
68
|
-
|
83
|
+
Grade = Enum.create(:a, :b, :c, :d, :e, :f) # => Grade(A, B, C, D, E, F)
|
69
84
|
```
|
70
85
|
|
71
|
-
### Custom initializer
|
86
|
+
### Custom initializer and instance methods
|
72
87
|
```ruby
|
73
|
-
class
|
74
|
-
def initialize(
|
75
|
-
@
|
88
|
+
class Color < Enum
|
89
|
+
def initialize(css_code)
|
90
|
+
@css_code = css_code
|
76
91
|
end
|
77
92
|
|
78
|
-
attr_reader :
|
93
|
+
attr_reader :css_code
|
79
94
|
|
80
|
-
value :
|
81
|
-
value :
|
95
|
+
value :RED, '#ff0000'
|
96
|
+
value :GREEN, '#00ff00'
|
97
|
+
value :BLUE, '#0000ff'
|
82
98
|
end
|
83
99
|
|
84
|
-
|
85
|
-
|
100
|
+
Color::RED.css_code # => '#ff0000'
|
101
|
+
Color::GREEN.css_code # => '#00ff00'
|
86
102
|
```
|
87
103
|
|
88
104
|
### Inheritance
|
data/enum_class.gemspec
CHANGED
@@ -10,6 +10,13 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["michele@liqid.de"]
|
11
11
|
|
12
12
|
spec.summary = 'Object-oriented enumerations for Ruby'
|
13
|
+
spec.description = <<-EOF
|
14
|
+
Provides a lightweight solution for object-oriented enumerations in Ruby in a similar ways as other languages, like Java, do.
|
15
|
+
It consists of a single class (Enum), that can be subclassed to create enumerated types.
|
16
|
+
Each class has a set of predefined instances, that represent the enumerated values.
|
17
|
+
Fatures like support for inheritance and declaring instance methods on enumerated values are supported as well.
|
18
|
+
EOF
|
19
|
+
|
13
20
|
spec.homepage = "https://github.com/LIQIDTechnology/enum_class"
|
14
21
|
spec.license = "MIT"
|
15
22
|
|
@@ -18,6 +25,8 @@ Gem::Specification.new do |spec|
|
|
18
25
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
26
|
spec.require_paths = ["lib"]
|
20
27
|
|
28
|
+
spec.required_ruby_version = '~> 2.0'
|
29
|
+
|
21
30
|
spec.add_development_dependency "bundler", "~> 1.12"
|
22
31
|
spec.add_development_dependency "rake", "~> 10.0"
|
23
32
|
spec.add_development_dependency "rspec", "~> 3.0"
|
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michele Piccirillo
|
@@ -80,7 +80,11 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
-
description:
|
83
|
+
description: |2
|
84
|
+
Provides a lightweight solution for object-oriented enumerations in Ruby in a similar ways as other languages, like Java, do.
|
85
|
+
It consists of a single class (Enum), that can be subclassed to create enumerated types.
|
86
|
+
Each class has a set of predefined instances, that represent the enumerated values.
|
87
|
+
Fatures like support for inheritance and declaring instance methods on enumerated values are supported as well.
|
84
88
|
email:
|
85
89
|
- michele@liqid.de
|
86
90
|
executables: []
|
@@ -112,9 +116,9 @@ require_paths:
|
|
112
116
|
- lib
|
113
117
|
required_ruby_version: !ruby/object:Gem::Requirement
|
114
118
|
requirements:
|
115
|
-
- - "
|
119
|
+
- - "~>"
|
116
120
|
- !ruby/object:Gem::Version
|
117
|
-
version: '0'
|
121
|
+
version: '2.0'
|
118
122
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
123
|
requirements:
|
120
124
|
- - ">="
|