enumerated_type 0.1.1 → 0.1.2
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.
- data/Gemfile +0 -2
- data/README.md +4 -4
- data/enumerated_type.gemspec +2 -2
- data/lib/enumerated_type.rb +8 -0
- data/lib/enumerated_type/version.rb +1 -1
- data/test/enumerated_type_spec.rb +18 -1
- metadata +12 -12
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -6,7 +6,7 @@ Dead simple enumerated types for Ruby.
|
|
6
6
|
|
7
7
|
This gem implements the familiar notion of enumerated types in Ruby.
|
8
8
|
|
9
|
-
"But this is Ruby," you say, "where we haven't any use for such things." Yep. In Ruby, you can get a long way without any formalized concept of enumerated types by
|
9
|
+
"But this is Ruby," you say, "where we haven't any use for such things." Yep. In Ruby, you can get a long way without any formalized concept of enumerated types by using symbols. Let's take the fairly typical example of a "status" field on the `Job` class:
|
10
10
|
|
11
11
|
```ruby
|
12
12
|
class Job
|
@@ -27,7 +27,7 @@ class Job
|
|
27
27
|
end
|
28
28
|
```
|
29
29
|
|
30
|
-
At first pass this seems fine. Any code that needs to act based on a job's status has to have magic symbols (i.e. `job.status == :success`), but maybe that's ok for a little while. Later, though, we might want to add a little logic
|
30
|
+
At first pass this seems fine. Any code that needs to act based on a job's status has to have magic symbols (i.e. `job.status == :success`), but maybe that's ok for a little while. Later, though, we might want to add a little logic around the `Job`'s status, something like:
|
31
31
|
|
32
32
|
```ruby
|
33
33
|
# In a job notifier class or something
|
@@ -104,7 +104,7 @@ Get an instance of an enumerated type:
|
|
104
104
|
|
105
105
|
# Or via symbol...
|
106
106
|
@status = JobStatus[:pending]
|
107
|
-
@status = JobStatus[:
|
107
|
+
@status = JobStatus[:wrong] #=> raises an ArgumentError
|
108
108
|
```
|
109
109
|
|
110
110
|
All instances have predicate methods defined automatically:
|
@@ -166,4 +166,4 @@ JobStatus::SUCCESS.message # => "Your job has completed"
|
|
166
166
|
|
167
167
|
To run the tests (assuming you have already run `gem install bundler`):
|
168
168
|
|
169
|
-
bundle install && rake
|
169
|
+
bundle install && bundle exec rake
|
data/enumerated_type.gemspec
CHANGED
@@ -19,6 +19,6 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
-
spec.add_development_dependency "
|
23
|
-
spec.add_development_dependency "
|
22
|
+
spec.add_development_dependency "minitest", "~> 5.1.0"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.1.0"
|
24
24
|
end
|
data/lib/enumerated_type.rb
CHANGED
@@ -22,6 +22,10 @@ module EnumeratedType
|
|
22
22
|
name.to_s
|
23
23
|
end
|
24
24
|
|
25
|
+
def to_json
|
26
|
+
name.to_s
|
27
|
+
end
|
28
|
+
|
25
29
|
private
|
26
30
|
|
27
31
|
def initialize(name, properties)
|
@@ -64,6 +68,10 @@ module EnumeratedType
|
|
64
68
|
end
|
65
69
|
|
66
70
|
options.keys.each do |property|
|
71
|
+
if property.to_s == "name"
|
72
|
+
raise ArgumentError, "Property name 'name' is not allowed (conflicts with default EnumeratedType#name)"
|
73
|
+
end
|
74
|
+
|
67
75
|
unless instance_methods.include?(:"#{property}")
|
68
76
|
attr_reader(:"#{property}")
|
69
77
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require "bundler/setup"
|
2
2
|
|
3
|
-
require "minitest"
|
3
|
+
require "minitest/autorun"
|
4
4
|
require "minitest/pride"
|
5
5
|
|
6
6
|
require "enumerated_type"
|
@@ -87,6 +87,17 @@ describe EnumeratedType do
|
|
87
87
|
it "does not expose public setters for properties" do
|
88
88
|
Gender::MALE.respond_to?(:planet=).must_equal false
|
89
89
|
end
|
90
|
+
|
91
|
+
it "does not allow the property name 'name'" do
|
92
|
+
name_property_definition = lambda do
|
93
|
+
Class.new do
|
94
|
+
include EnumeratedType
|
95
|
+
declare :test, :name => "test"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
name_property_definition.must_raise(ArgumentError, "Property name 'name' is not allowed")
|
100
|
+
end
|
90
101
|
end
|
91
102
|
|
92
103
|
describe ".[]" do
|
@@ -121,4 +132,10 @@ describe EnumeratedType do
|
|
121
132
|
Gender::MALE.to_s.must_equal "male"
|
122
133
|
end
|
123
134
|
end
|
135
|
+
|
136
|
+
describe "#to_json" do
|
137
|
+
it "is the name (as a string)" do
|
138
|
+
Gender::MALE.to_json.must_equal "male"
|
139
|
+
end
|
140
|
+
end
|
124
141
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumerated_type
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-12-
|
12
|
+
date: 2013-12-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -28,29 +28,29 @@ dependencies:
|
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '1.3'
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
|
-
name:
|
31
|
+
name: minitest
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
33
33
|
none: false
|
34
34
|
requirements:
|
35
|
-
- -
|
35
|
+
- - ~>
|
36
36
|
- !ruby/object:Gem::Version
|
37
|
-
version:
|
37
|
+
version: 5.1.0
|
38
38
|
type: :development
|
39
39
|
prerelease: false
|
40
40
|
version_requirements: !ruby/object:Gem::Requirement
|
41
41
|
none: false
|
42
42
|
requirements:
|
43
|
-
- -
|
43
|
+
- - ~>
|
44
44
|
- !ruby/object:Gem::Version
|
45
|
-
version:
|
45
|
+
version: 5.1.0
|
46
46
|
- !ruby/object:Gem::Dependency
|
47
|
-
name:
|
47
|
+
name: rake
|
48
48
|
requirement: !ruby/object:Gem::Requirement
|
49
49
|
none: false
|
50
50
|
requirements:
|
51
51
|
- - ~>
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version:
|
53
|
+
version: 10.1.0
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -58,7 +58,7 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - ~>
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 10.1.0
|
62
62
|
description: Simple enumerated types
|
63
63
|
email:
|
64
64
|
- rafer@ralua.com
|
@@ -90,7 +90,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
90
90
|
version: '0'
|
91
91
|
segments:
|
92
92
|
- 0
|
93
|
-
hash: -
|
93
|
+
hash: -1744150559039667923
|
94
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
95
|
none: false
|
96
96
|
requirements:
|
@@ -99,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
99
99
|
version: '0'
|
100
100
|
segments:
|
101
101
|
- 0
|
102
|
-
hash: -
|
102
|
+
hash: -1744150559039667923
|
103
103
|
requirements: []
|
104
104
|
rubyforge_project:
|
105
105
|
rubygems_version: 1.8.26
|