enumerate_it 0.7.10 → 0.7.11

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- # Specify your gem's dependencies in eblood_compomat.gemspec
3
+ # Specify your gem's dependencies in enumerate_it.gemspec
4
4
  gemspec
@@ -70,6 +70,14 @@ This will create some nice stuff:
70
70
 
71
71
  RelationshipStatus.values_for %w(MARRIED SINGLE) # [2, 1]
72
72
 
73
+ * You can retrieve the value for a specific enumeration constant:
74
+
75
+ RelationshipStatus.value_for("MARRIED") # 2
76
+
77
+ * You can retrieve the symbol used to declare a specific enumeration value:
78
+
79
+ RelationshipStatus.key_for(RelationshioStatus::MARRIED) # :married
80
+
73
81
  * You can manipulate the hash used to create the enumeration:
74
82
 
75
83
  RelationshipStatus.enumeration # returns the exact hash used to define the enumeration
@@ -225,6 +225,14 @@ module EnumerateIt
225
225
  values.map { |v| self.const_get(v.to_sym) }
226
226
  end
227
227
 
228
+ def self.value_for(value)
229
+ self.const_get(value.to_sym)
230
+ end
231
+
232
+ def self.key_for(value)
233
+ enumeration.map {|e| e[0] if e[1][0] == value }.compact.first
234
+ end
235
+
228
236
  def self.to_range
229
237
  (list.min..list.max)
230
238
  end
@@ -243,7 +251,6 @@ module EnumerateIt
243
251
  values_hash[key] = [value, key]
244
252
  end
245
253
  end
246
- values_hash
247
254
  end
248
255
 
249
256
  def self.register_enumeration(values_hash)
@@ -1,3 +1,3 @@
1
1
  module EnumerateIt
2
- VERSION = "0.7.10"
2
+ VERSION = "0.7.11"
3
3
  end
@@ -251,6 +251,22 @@ describe EnumerateIt::Base do
251
251
  end
252
252
  end
253
253
 
254
+ describe "#value_for" do
255
+ it "returns the enumeration's value" do
256
+ TestEnumeration.value_for("VALUE_1").should == TestEnumeration::VALUE_1
257
+ end
258
+ end
259
+
260
+ describe "#key_for" do
261
+ it "returns the key for the given value inside the enumeration" do
262
+ TestEnumeration.key_for(TestEnumeration::VALUE_1).should == :value_1
263
+ end
264
+
265
+ it "returns nil if the enumeration does not have the given value" do
266
+ TestEnumeration.key_for("foo").should be_nil
267
+ end
268
+ end
269
+
254
270
  context 'associate values with a list' do
255
271
  it "creates constants for each enumeration value" do
256
272
  TestEnumerationWithList::FIRST.should == "first"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumerate_it
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.10
4
+ version: 0.7.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-14 00:00:00.000000000 Z
12
+ date: 2012-02-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &2169076300 !ruby/object:Gem::Requirement
16
+ requirement: &2172840600 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.3.2
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2169076300
24
+ version_requirements: *2172840600
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &2169075800 !ruby/object:Gem::Requirement
27
+ requirement: &2172840100 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 2.5.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2169075800
35
+ version_requirements: *2172840100
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activerecord
38
- requirement: &2169075340 !ruby/object:Gem::Requirement
38
+ requirement: &2172839640 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: 3.0.5
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2169075340
46
+ version_requirements: *2172839640
47
47
  description: Have a legacy database and need some enumerations in your models to match
48
48
  those stupid '4 rows/2 columns' tables with foreign keys and stop doing joins just
49
49
  to fetch a simple description? Or maybe use some integers instead of strings as