ruby_enum 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +9 -0
- data/Gemfile.lock +1 -1
- data/README.md +30 -3
- data/lib/ruby_enum/rspec.rb +25 -0
- data/lib/ruby_enum/version.rb +1 -1
- data/lib/ruby_enum.rb +31 -7
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 657f62ea2a5ad4d1de5127994577fed1718f6818
|
4
|
+
data.tar.gz: b2e13247e031052801b3a655b56f66384e1163e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61772d63201ff017e9fa501edf8072a1e54db44deff03a0dfac12894dff8ee1fbc3a6e821d2810b5fd87cdbe41073fbb6951109036e0299497981df2c988589c
|
7
|
+
data.tar.gz: ce4300b106bb90bdbcfa257a7dad0684b2551b08168aaf81d1046975941a292175fb07dc318bb2af708eaf0708b1284d9ee9ea07bdfdc33adee955b17e3b6712
|
data/.gitignore
ADDED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# RubyEnum
|
2
2
|
|
3
|
-
A simple enumeration type for ruby
|
3
|
+
A simple enumeration type for ruby.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -20,7 +20,8 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
To create an enumeration type simply include the `RubyEnum` module
|
23
|
+
To create an enumeration type simply include the `RubyEnum` module in your
|
24
|
+
enumeration class and provide the enumeration's values as follows:
|
24
25
|
```ruby
|
25
26
|
class Coordinates
|
26
27
|
include RubyEnum
|
@@ -31,7 +32,33 @@ class Coordinates
|
|
31
32
|
enum :east
|
32
33
|
```
|
33
34
|
|
34
|
-
Including `RubyEnum` in any class will make it a singleton. This means that you
|
35
|
+
Including `RubyEnum` in any class will make it a singleton. This means that you
|
36
|
+
can't create new instances of your class using `new`, `allocate`, `clone` or
|
37
|
+
`dup`. You can access an enumeration's value instances in three different ways:
|
38
|
+
|
39
|
+
1. Using a class method that matches the provided name of the enumeration
|
40
|
+
value:
|
41
|
+
```ruby
|
42
|
+
north = Coordinates.north
|
43
|
+
```
|
44
|
+
|
45
|
+
2. Using a constant:
|
46
|
+
```ruby
|
47
|
+
north = Coordinates::NORTH
|
48
|
+
```
|
49
|
+
|
50
|
+
3. Treating your enumeration class as a dictionary:
|
51
|
+
```ruby
|
52
|
+
north = Coordinates[:north]
|
53
|
+
```
|
54
|
+
|
55
|
+
All three approaches are equivalent and subject to personal style.
|
56
|
+
|
57
|
+
To retrieve all values of an enumeration, simply invoke `all` method on your
|
58
|
+
enumeration class:
|
59
|
+
```ruby
|
60
|
+
coordinates = Coordinates.all
|
61
|
+
```
|
35
62
|
|
36
63
|
## Development
|
37
64
|
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rspec/expectations'
|
2
|
+
|
3
|
+
RSpec::Matchers.define :be_an_enumeration do
|
4
|
+
match do |actual|
|
5
|
+
actual.ancestors.include? RubyEnum
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
RSpec::Matchers.define :define_enumeration_value do |name, value|
|
10
|
+
match do |actual|
|
11
|
+
enum_value = actual.send name
|
12
|
+
if value
|
13
|
+
enum_value.name == name && enum_value.value == value
|
14
|
+
else
|
15
|
+
enum_value.name == name
|
16
|
+
end
|
17
|
+
end
|
18
|
+
description do
|
19
|
+
if value
|
20
|
+
"define enumeration #{name} with value #{value}"
|
21
|
+
else
|
22
|
+
"define enumeration #{name}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/ruby_enum/version.rb
CHANGED
data/lib/ruby_enum.rb
CHANGED
@@ -11,11 +11,32 @@ module RubyEnum
|
|
11
11
|
|
12
12
|
module InstanceMethods
|
13
13
|
|
14
|
+
def method_missing(method, *args, &block)
|
15
|
+
if method.to_s =~ /^(.*)\?/
|
16
|
+
enum_names = self.class.all.map(&:name)
|
17
|
+
expected_name = $1.to_sym
|
18
|
+
if enum_names.include?(expected_name)
|
19
|
+
return name == expected_name
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def respond_to?(method)
|
25
|
+
if method.to_s =~ /^(.*)\?/
|
26
|
+
enum_names = self.class.all.map(&:name)
|
27
|
+
return enum_names.include? $1.to_sym
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
14
31
|
# the value associated with the enumeration instance
|
15
|
-
def
|
32
|
+
def value
|
16
33
|
@_value
|
17
34
|
end
|
18
35
|
|
36
|
+
def name
|
37
|
+
@_name
|
38
|
+
end
|
39
|
+
|
19
40
|
def clone
|
20
41
|
raise TypeError, "can't clone instance of enum #{self.class}"
|
21
42
|
end
|
@@ -26,7 +47,8 @@ module RubyEnum
|
|
26
47
|
|
27
48
|
private
|
28
49
|
|
29
|
-
def initialize(value)
|
50
|
+
def initialize(name, value)
|
51
|
+
@_name = name
|
30
52
|
@_value = value
|
31
53
|
end
|
32
54
|
end
|
@@ -40,8 +62,6 @@ module RubyEnum
|
|
40
62
|
|
41
63
|
# defines enumeration values
|
42
64
|
def enum(name, value = nil)
|
43
|
-
@_instances ||= {}
|
44
|
-
|
45
65
|
normalized_name = normalize name
|
46
66
|
if self[normalized_name]
|
47
67
|
raise ArgumentError, "An enumeration value for #{normalized_name} has " \
|
@@ -49,20 +69,24 @@ module RubyEnum
|
|
49
69
|
else
|
50
70
|
value = normalized_name.to_s unless value
|
51
71
|
define_instance_accessor_for normalized_name
|
52
|
-
|
72
|
+
enumeration_values[normalized_name] = new(normalized_name, value)
|
53
73
|
end
|
54
74
|
end
|
55
75
|
|
56
76
|
def [](name)
|
57
|
-
|
77
|
+
enumeration_values[normalize(name)]
|
58
78
|
end
|
59
79
|
|
60
80
|
def all
|
61
|
-
|
81
|
+
enumeration_values.map { |_, instance| instance }
|
62
82
|
end
|
63
83
|
|
64
84
|
private
|
65
85
|
|
86
|
+
def enumeration_values
|
87
|
+
@_instances ||= {}
|
88
|
+
end
|
89
|
+
|
66
90
|
def define_instance_accessor_for(name)
|
67
91
|
self.class.instance_eval do
|
68
92
|
define_method(name) { return self[name] }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lefteris Laskaridis
|
@@ -59,6 +59,7 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
+
- ".gitignore"
|
62
63
|
- Gemfile
|
63
64
|
- Gemfile.lock
|
64
65
|
- LICENSE.txt
|
@@ -67,6 +68,7 @@ files:
|
|
67
68
|
- bin/console
|
68
69
|
- bin/setup
|
69
70
|
- lib/ruby_enum.rb
|
71
|
+
- lib/ruby_enum/rspec.rb
|
70
72
|
- lib/ruby_enum/version.rb
|
71
73
|
- ruby_enum.gemspec
|
72
74
|
- tags
|