enumerated_type 0.2.3 → 0.3.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.
data/README.md CHANGED
@@ -154,9 +154,9 @@ Add arbitrary attributes:
154
154
  class JobStatus
155
155
  include EnumeratedType
156
156
 
157
- declare :pending, :message => "Your Job is waiting to be processed"
158
- declare :success, :message => "Your Job has completed"
159
- declare :failure, :message => "Oops, it looks like there was a problem"
157
+ declare :pending, :code => 1, :message => "Your Job is waiting to be processed"
158
+ declare :success, :code => 2, :message => "Your Job has completed"
159
+ declare :failure, :code => 3, :message => "Oops, it looks like there was a problem"
160
160
  end
161
161
 
162
162
  JobStatus::SUCCESS.message # => "Your job has completed"
@@ -175,6 +175,26 @@ JobStatus.coerce(:wrong) # => raises an ArgumentError
175
175
 
176
176
  `.coerce` is particularly useful for scrubbing parameters, allowing you to succinctly assert that arguments are valid for your `EnumeratedType`, while also broadening the range of types that can be used as input. Using `.coerce` at the boundaries of your code allows clients the freedom to pass in full fledged `EnumeratedType` objects, symbols or even strings, and allows you to use the `.coerce`d input with confidence (i.e without any type or validity checking beyond the call to `.coerce`).
177
177
 
178
+ Look up an enum by a property:
179
+
180
+ ```ruby
181
+ JobStatus.by(:code, 2) # => #<JobStatus:success>
182
+ JobStatus.by(:code, 4) # => raises a TypeError
183
+ ```
184
+
185
+ The first parameter specifies the method that will be called on instances of your `EnumeratedType` during lookup. This works for attributes specified with `declare` (such as `:code` above) but also for .
186
+
187
+ ```ruby
188
+ class JobStatus
189
+ def display
190
+ "#{name}-#{code}"
191
+ end
192
+ end
193
+
194
+ JobStatus.by(:display, "pending-1") # => #<JobStatus:pending>
195
+ ```
196
+ If more than one instance of your `EnumeratedType` matches, the first match will be returned in the order the types were `declared`.
197
+
178
198
  ## Development
179
199
 
180
200
  To run the tests (assuming you have already run `gem install bundler`):
@@ -52,9 +52,13 @@ module EnumeratedType
52
52
  @all.each(&block)
53
53
  end
54
54
 
55
+ def by(name, value)
56
+ found = find { |e| e.send(name) == value }
57
+ found || raise(ArgumentError, "Could not find #{self.name} with ##{name} == #{value.inspect}'")
58
+ end
59
+
55
60
  def [](name)
56
- found = find { |e| e.name == name }
57
- found || raise(ArgumentError, "Unrecognized #{self.name} name #{name.inspect}'")
61
+ by(:name, name)
58
62
  end
59
63
 
60
64
  def recognized?(name)
@@ -1,3 +1,3 @@
1
1
  module EnumeratedType
2
- VERSION = "0.2.3"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -83,25 +83,25 @@ describe EnumeratedType do
83
83
  it "freezes String properties" do
84
84
  Gender::MALE.planet.frozen?.must_equal true
85
85
  end
86
-
86
+
87
87
  it "does not freeze other properties" do
88
88
  NonString = Class.new do
89
89
  def frozen?
90
90
  !!@frozen
91
91
  end
92
-
92
+
93
93
  def freeze
94
94
  @frozen = true
95
95
  end
96
96
  end
97
-
97
+
98
98
  non_string = NonString.new
99
-
99
+
100
100
  Class.new do
101
101
  include EnumeratedType
102
102
  declare :test, :other => non_string
103
103
  end
104
-
104
+
105
105
  non_string.frozen?.must_equal false
106
106
  end
107
107
 
@@ -173,6 +173,36 @@ describe EnumeratedType do
173
173
  end
174
174
  end
175
175
 
176
+ describe ".by" do
177
+ class Shapes
178
+ include EnumeratedType
179
+
180
+ declare :triangle, :sides => 3, :pretty_hip => "YEAH"
181
+ declare :rectangle, :sides => 4, :pretty_hip => "NAH"
182
+ declare :pentagon, :sides => 5, :pretty_hip => "YEAH"
183
+
184
+ def sides_squared
185
+ sides * sides
186
+ end
187
+ end
188
+
189
+ it "looks the value up by the specified attribute" do
190
+ Shapes.by(:sides, 4).must_equal(Shapes::RECTANGLE)
191
+ end
192
+
193
+ it "raises an argumetn if there is no match" do
194
+ lambda { Shapes.by(:sides, 6) }.must_raise(ArgumentError)
195
+ end
196
+
197
+ it "returns the first declared value if there is more than one match" do
198
+ Shapes.by(:pretty_hip, "YEAH").must_equal(Shapes::TRIANGLE)
199
+ end
200
+
201
+ it "works with arbitrary methods" do
202
+ Shapes.by(:sides_squared, 16).must_equal(Shapes::RECTANGLE)
203
+ end
204
+ end
205
+
176
206
  describe "#inspect" do
177
207
  it "looks reasonable" do
178
208
  Gender::FEMALE.inspect.must_equal "#<Gender:female>"
@@ -189,17 +219,17 @@ describe EnumeratedType do
189
219
  it "is the name (as a string)" do
190
220
  Gender::MALE.to_json.must_equal '"male"'
191
221
  end
192
-
222
+
193
223
  it "doesn't raise an exception when given options" do
194
224
  Gender::MALE.to_json(:stuff => "here")
195
225
  end
196
226
  end
197
-
227
+
198
228
  describe "#as_json" do
199
229
  it "is the name (as a string)" do
200
230
  Gender::MALE.as_json.must_equal "male"
201
231
  end
202
-
232
+
203
233
  it "doesn't raise an exception when given options" do
204
234
  Gender::MALE.as_json(:stuff => "here")
205
235
  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.2.3
4
+ version: 0.3.0
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: 2014-03-17 00:00:00.000000000 Z
12
+ date: 2014-03-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -90,7 +90,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
90
  version: '0'
91
91
  segments:
92
92
  - 0
93
- hash: -3675634527099412429
93
+ hash: 2282385263752842432
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: -3675634527099412429
102
+ hash: 2282385263752842432
103
103
  requirements: []
104
104
  rubyforge_project:
105
105
  rubygems_version: 1.8.23