enumeration 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +25 -0
- data/README.rdoc +49 -1
- data/lib/enumeration.rb +8 -3
- data/lib/enumeration/collection.rb +20 -5
- data/lib/enumeration/version.rb +1 -1
- data/test/collection_test.rb +24 -3
- data/test/list_enum_test.rb +9 -2
- data/test/map_enum_test.rb +9 -4
- metadata +5 -4
data/Gemfile.lock
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
enumeration (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
json (1.5.1)
|
10
|
+
kelredd-useful (0.4.1)
|
11
|
+
json
|
12
|
+
leftright (0.9.1)
|
13
|
+
shoulda (2.11.3)
|
14
|
+
test-belt (0.2.1)
|
15
|
+
kelredd-useful (~> 0.4.0)
|
16
|
+
leftright (~> 0.9.0)
|
17
|
+
shoulda (~> 2.11)
|
18
|
+
|
19
|
+
PLATFORMS
|
20
|
+
ruby
|
21
|
+
|
22
|
+
DEPENDENCIES
|
23
|
+
bundler (~> 1.0)
|
24
|
+
enumeration!
|
25
|
+
test-belt (= 0.2.1)
|
data/README.rdoc
CHANGED
@@ -8,10 +8,58 @@ add enumerated value attributes to your ruby classes
|
|
8
8
|
|
9
9
|
gem install enumeration
|
10
10
|
|
11
|
-
== Usage
|
11
|
+
== Basic Example and Usage
|
12
12
|
|
13
13
|
require 'enumeration'
|
14
14
|
|
15
|
+
class Thing
|
16
|
+
include Enumeration
|
17
|
+
|
18
|
+
# list enumeration
|
19
|
+
enum :word, %w{aye bee see}
|
20
|
+
|
21
|
+
# map enumeration
|
22
|
+
enum :stuff, {
|
23
|
+
:a => "aye",
|
24
|
+
:b => "bee",
|
25
|
+
:c => "see"
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
# get the enum sets
|
30
|
+
Thing.word_set # => ['aye', 'bee', 'see']
|
31
|
+
Thing.stuff_set # => [:a, :b, :c]
|
32
|
+
|
33
|
+
# lookup mapped enum values
|
34
|
+
Thing.stuff(:a) # => "aye"
|
35
|
+
|
36
|
+
|
37
|
+
t = Thing.new
|
38
|
+
|
39
|
+
# write list enum values
|
40
|
+
t.word # => nil
|
41
|
+
t.word = "aye"
|
42
|
+
t.word # => "aye"
|
43
|
+
t.word_key # => "aye" (a list's 'keys' are it's values, vice-versa)
|
44
|
+
t.word = "dee"
|
45
|
+
t.word # => nil (won't write non enum values)
|
46
|
+
t.word_key # => nil
|
47
|
+
|
48
|
+
# write mapped enum value
|
49
|
+
t.stuff # => nil
|
50
|
+
t.stuff = :b # (write using key)
|
51
|
+
t.stuff # => "bee"
|
52
|
+
t.stuff_key # => :b
|
53
|
+
t.stuff = "see" # (write using value)
|
54
|
+
t.stuff # => "see"
|
55
|
+
t.stuff_key # => :c
|
56
|
+
t.stuff = :d
|
57
|
+
t.stuff # => nil (won't write non enum keys)
|
58
|
+
t.stuff_key # => nil
|
59
|
+
t.stuff = "dee"
|
60
|
+
t.stuff # => nil (won't write non enum values)
|
61
|
+
t.stuff_key # => nil
|
62
|
+
|
15
63
|
== License
|
16
64
|
|
17
65
|
Copyright (c) 2011 Kelly D. Redding
|
data/lib/enumeration.rb
CHANGED
@@ -11,8 +11,9 @@ module Enumeration
|
|
11
11
|
# define an anonymous Module to extend on
|
12
12
|
# defining a class level map reader
|
13
13
|
class_methods = Module.new do
|
14
|
-
define_method(name) {
|
14
|
+
define_method(name.to_s+'_collection') { c.data }
|
15
15
|
define_method(name.to_s+'_set') { c.set }
|
16
|
+
define_method(name) {|k| class_variable_get("@@#{name}")[k]} if c.map?
|
16
17
|
end
|
17
18
|
|
18
19
|
# set a class variable to store the enum map (used by above reader)
|
@@ -30,8 +31,12 @@ module Enumeration
|
|
30
31
|
end
|
31
32
|
|
32
33
|
# instance reader for the enum value
|
33
|
-
define_method(name)
|
34
|
-
|
34
|
+
define_method(name) { instance_variable_get("@#{name}") }
|
35
|
+
|
36
|
+
# instance reader for the enum key
|
37
|
+
define_method(name.to_s+'_key') do
|
38
|
+
c = self.class.send(:class_variable_get, "@@#{name}")
|
39
|
+
c.key(instance_variable_get("@#{name}"))
|
35
40
|
end
|
36
41
|
end
|
37
42
|
end
|
@@ -1,6 +1,8 @@
|
|
1
1
|
module Enumeration
|
2
2
|
class Collection
|
3
3
|
|
4
|
+
attr_reader :data
|
5
|
+
|
4
6
|
def initialize(map_or_list)
|
5
7
|
unless map_or_list.kind_of?(::Hash) || map_or_list.kind_of?(::Array)
|
6
8
|
raise ArgumentError, "please specify the enum collection as a Hash or Array"
|
@@ -8,11 +10,24 @@ module Enumeration
|
|
8
10
|
@data = map_or_list
|
9
11
|
end
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
# lookup collection value by a key
|
14
|
+
def [](key)
|
15
|
+
if self.map? && @data.has_key?(key)
|
16
|
+
@data[key]
|
17
|
+
elsif (self.map? && @data.has_value?(key)) ||
|
18
|
+
(self.list? && @data.include?(key))
|
19
|
+
key
|
20
|
+
else
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# lookup collection key by a value
|
26
|
+
def key(value)
|
27
|
+
if self.map? && @data.has_value?(value)
|
28
|
+
@data.index(value)
|
29
|
+
elsif (self.map? && @data.has_key?(value)) ||
|
30
|
+
(self.list? && @data.include?(value))
|
16
31
|
value
|
17
32
|
else
|
18
33
|
nil
|
data/lib/enumeration/version.rb
CHANGED
data/test/collection_test.rb
CHANGED
@@ -5,6 +5,7 @@ class CollectionAPITest < Test::Unit::TestCase
|
|
5
5
|
context "Enumeration collection" do
|
6
6
|
subject { Enumeration::Collection.new([]) }
|
7
7
|
|
8
|
+
should_have_reader :data
|
8
9
|
should_have_instance_methods :set, :map?, :list?, :[]
|
9
10
|
|
10
11
|
should "only be created from Arrays or Hashes" do
|
@@ -29,14 +30,22 @@ class ListCollectionTest < Test::Unit::TestCase
|
|
29
30
|
assert_equal false, subject.map?
|
30
31
|
end
|
31
32
|
|
33
|
+
should "know it's data" do
|
34
|
+
assert_equal ['one', 'two', 'three'], subject.data
|
35
|
+
end
|
36
|
+
|
32
37
|
should "know it's set" do
|
33
38
|
assert_equal ['one', 'two', 'three'], subject.set
|
34
39
|
end
|
35
40
|
|
36
|
-
should "lookup by
|
41
|
+
should "lookup value by key" do
|
37
42
|
assert_equal 'two', subject['two']
|
38
43
|
end
|
39
44
|
|
45
|
+
should "lookup key by value" do
|
46
|
+
assert_equal 'three', subject.key('three')
|
47
|
+
end
|
48
|
+
|
40
49
|
end
|
41
50
|
end
|
42
51
|
|
@@ -49,17 +58,29 @@ class MapCollectionTest < Test::Unit::TestCase
|
|
49
58
|
assert_equal false, subject.list?
|
50
59
|
end
|
51
60
|
|
61
|
+
should "know it's data" do
|
62
|
+
assert_equal({:one=>1,:two=>2,:three=>3}, subject.data)
|
63
|
+
end
|
64
|
+
|
52
65
|
should "know it's set" do
|
53
66
|
[:one, :two, :three].each{|n| assert subject.set.include?(n)}
|
54
67
|
end
|
55
68
|
|
56
|
-
should "lookup by key" do
|
69
|
+
should "lookup value by key" do
|
57
70
|
assert_equal 2, subject[:two]
|
58
71
|
end
|
59
72
|
|
60
|
-
should "lookup by value" do
|
73
|
+
should "lookup value by value" do
|
61
74
|
assert_equal 2, subject[2]
|
62
75
|
end
|
63
76
|
|
77
|
+
should "lookup key by value" do
|
78
|
+
assert_equal :two, subject.key(2)
|
79
|
+
end
|
80
|
+
|
81
|
+
should "lookup key by key" do
|
82
|
+
assert_equal :two, subject.key(:two)
|
83
|
+
end
|
84
|
+
|
64
85
|
end
|
65
86
|
end
|
data/test/list_enum_test.rb
CHANGED
@@ -9,7 +9,7 @@ class ListEnumTest < Test::Unit::TestCase
|
|
9
9
|
Thing.send(:enum, :word, %w{aye bee see})
|
10
10
|
end
|
11
11
|
|
12
|
-
|
12
|
+
should_have_class_methods :word_set, :word_collection
|
13
13
|
should_have_accessor :word
|
14
14
|
|
15
15
|
should "not have a class level lookup method" do
|
@@ -25,16 +25,23 @@ class ListEnumTest < Test::Unit::TestCase
|
|
25
25
|
assert !words.empty?
|
26
26
|
assert_equal 3, words.size
|
27
27
|
assert_equal ['aye', 'bee', 'see'], Thing.word_set
|
28
|
+
assert_equal Thing.word_set, Thing.word_collection
|
28
29
|
end
|
29
30
|
|
30
|
-
should "write
|
31
|
+
should "write a value and read the value" do
|
31
32
|
subject.word = "see"
|
32
33
|
assert_equal "see", subject.word
|
33
34
|
end
|
34
35
|
|
36
|
+
should "write a value and read the key" do
|
37
|
+
subject.word = "see"
|
38
|
+
assert_equal "see", subject.word_key
|
39
|
+
end
|
40
|
+
|
35
41
|
should "write nil for values that aren't in the enum" do
|
36
42
|
subject.word = "bady-bad"
|
37
43
|
assert_equal nil, subject.word
|
44
|
+
assert_equal nil, subject.word_key
|
38
45
|
end
|
39
46
|
|
40
47
|
end
|
data/test/map_enum_test.rb
CHANGED
@@ -13,7 +13,7 @@ class MapEnumTest < Test::Unit::TestCase
|
|
13
13
|
})
|
14
14
|
end
|
15
15
|
|
16
|
-
should_have_class_methods :stuff, :stuff_set
|
16
|
+
should_have_class_methods :stuff, :stuff_set, :stuff_collection
|
17
17
|
should_have_accessor :stuff
|
18
18
|
|
19
19
|
should "provide class level access to the enum set" do
|
@@ -23,23 +23,26 @@ class MapEnumTest < Test::Unit::TestCase
|
|
23
23
|
assert !stuffs.empty?
|
24
24
|
assert_equal 3, stuffs.size
|
25
25
|
[:a, :b, :c].each{|t| assert Thing.stuff_set.include?(t)}
|
26
|
+
assert_equal({:a=>'aye',:b=>'bee',:c=>'see'}, Thing.stuff_collection)
|
26
27
|
end
|
27
28
|
|
28
29
|
should "provide class level lookup of the enum" do
|
29
30
|
assert_equal "aye", Thing.stuff(:a)
|
30
31
|
end
|
31
32
|
|
32
|
-
should "write
|
33
|
+
should "write a key and read the value and key" do
|
33
34
|
subject.stuff = :b
|
34
35
|
assert_equal "bee", subject.stuff
|
36
|
+
assert_equal :b, subject.stuff_key
|
35
37
|
end
|
36
38
|
|
37
|
-
should "write
|
39
|
+
should "write a value and read the value" do
|
38
40
|
subject.stuff = "see"
|
39
41
|
assert_equal "see", subject.stuff
|
42
|
+
assert_equal :c, subject.stuff_key
|
40
43
|
end
|
41
44
|
|
42
|
-
should "not read
|
45
|
+
should "not read keys like you would values" do
|
43
46
|
subject.stuff = :c
|
44
47
|
assert_not_equal :c, subject.stuff
|
45
48
|
end
|
@@ -47,11 +50,13 @@ class MapEnumTest < Test::Unit::TestCase
|
|
47
50
|
should "write nil for keys that aren't in the enum" do
|
48
51
|
subject.stuff = :bad
|
49
52
|
assert_equal nil, subject.stuff
|
53
|
+
assert_equal nil, subject.stuff_key
|
50
54
|
end
|
51
55
|
|
52
56
|
should "write nil for values that aren't in the enum" do
|
53
57
|
subject.stuff = "bady-bad"
|
54
58
|
assert_equal nil, subject.stuff
|
59
|
+
assert_equal nil, subject.stuff_key
|
55
60
|
end
|
56
61
|
|
57
62
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumeration
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 1.0.0
|
10
|
+
version: 1.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kelly D. Redding
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-04-
|
18
|
+
date: 2011-04-06 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -61,6 +61,7 @@ extra_rdoc_files: []
|
|
61
61
|
files:
|
62
62
|
- .gitignore
|
63
63
|
- Gemfile
|
64
|
+
- Gemfile.lock
|
64
65
|
- README.rdoc
|
65
66
|
- Rakefile
|
66
67
|
- enumeration.gemspec
|