enumeration 0.0.1 → 1.0.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/lib/enumeration.rb +43 -1
- data/lib/enumeration/collection.rb +35 -0
- data/lib/enumeration/version.rb +1 -1
- data/test/api__test.rb +14 -0
- data/test/collection_test.rb +65 -0
- data/test/helper.rb +1 -2
- data/test/list_enum_test.rb +42 -0
- data/test/map_enum_test.rb +58 -0
- data/test/thing.rb +3 -0
- metadata +14 -5
- data/test/enumeration_test.rb +0 -13
data/lib/enumeration.rb
CHANGED
@@ -1,3 +1,45 @@
|
|
1
|
+
module Enumeration; end
|
2
|
+
require 'enumeration/collection'
|
3
|
+
|
1
4
|
module Enumeration
|
2
|
-
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def enum(name, map_or_list)
|
8
|
+
# TODO: validate name
|
9
|
+
c = Collection.new(map_or_list)
|
10
|
+
|
11
|
+
# define an anonymous Module to extend on
|
12
|
+
# defining a class level map reader
|
13
|
+
class_methods = Module.new do
|
14
|
+
define_method(name) {|k| class_variable_get("@@#{name}")[k]} if c.map?
|
15
|
+
define_method(name.to_s+'_set') { c.set }
|
16
|
+
end
|
17
|
+
|
18
|
+
# set a class variable to store the enum map (used by above reader)
|
19
|
+
# extend the anonymous module to get tne above class
|
20
|
+
# level reader for the map
|
21
|
+
class_eval do
|
22
|
+
class_variable_set("@@#{name}", c)
|
23
|
+
extend class_methods
|
24
|
+
end
|
25
|
+
|
26
|
+
# instance writer for the enum value
|
27
|
+
define_method("#{name}=") do |value|
|
28
|
+
c = self.class.send(:class_variable_get, "@@#{name}")
|
29
|
+
instance_variable_set("@#{name}", c[value])
|
30
|
+
end
|
31
|
+
|
32
|
+
# instance reader for the enum value
|
33
|
+
define_method(name) do
|
34
|
+
instance_variable_get("@#{name}")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class << self
|
40
|
+
def included(receiver)
|
41
|
+
receiver.send :extend, ClassMethods
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
3
45
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Enumeration
|
2
|
+
class Collection
|
3
|
+
|
4
|
+
def initialize(map_or_list)
|
5
|
+
unless map_or_list.kind_of?(::Hash) || map_or_list.kind_of?(::Array)
|
6
|
+
raise ArgumentError, "please specify the enum collection as a Hash or Array"
|
7
|
+
end
|
8
|
+
@data = map_or_list
|
9
|
+
end
|
10
|
+
|
11
|
+
def [](value)
|
12
|
+
if self.map? && @data.has_key?(value)
|
13
|
+
@data[value]
|
14
|
+
elsif (self.map? && @data.has_value?(value)) ||
|
15
|
+
(@data.include?(value))
|
16
|
+
value
|
17
|
+
else
|
18
|
+
nil
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def list?
|
23
|
+
@data.kind_of?(::Array)
|
24
|
+
end
|
25
|
+
|
26
|
+
def map?
|
27
|
+
@data.kind_of?(::Hash)
|
28
|
+
end
|
29
|
+
|
30
|
+
def set
|
31
|
+
self.map? ? @data.keys : @data
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
data/lib/enumeration/version.rb
CHANGED
data/test/api__test.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require "test/helper"
|
2
|
+
require "enumeration/collection"
|
3
|
+
|
4
|
+
class CollectionAPITest < Test::Unit::TestCase
|
5
|
+
context "Enumeration collection" do
|
6
|
+
subject { Enumeration::Collection.new([]) }
|
7
|
+
|
8
|
+
should_have_instance_methods :set, :map?, :list?, :[]
|
9
|
+
|
10
|
+
should "only be created from Arrays or Hashes" do
|
11
|
+
assert_raises ArgumentError do
|
12
|
+
Enumeration::Collection.new('stuff')
|
13
|
+
end
|
14
|
+
assert_nothing_raised do
|
15
|
+
Enumeration::Collection.new([])
|
16
|
+
Enumeration::Collection.new({})
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class ListCollectionTest < Test::Unit::TestCase
|
24
|
+
context "List collection" do
|
25
|
+
subject { Enumeration::Collection.new(['one', 'two', 'three']) }
|
26
|
+
|
27
|
+
should "be a list" do
|
28
|
+
assert_equal true, subject.list?
|
29
|
+
assert_equal false, subject.map?
|
30
|
+
end
|
31
|
+
|
32
|
+
should "know it's set" do
|
33
|
+
assert_equal ['one', 'two', 'three'], subject.set
|
34
|
+
end
|
35
|
+
|
36
|
+
should "lookup by value" do
|
37
|
+
assert_equal 'two', subject['two']
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class MapCollectionTest < Test::Unit::TestCase
|
44
|
+
context "Map collection" do
|
45
|
+
subject { Enumeration::Collection.new({ :one => 1, :two => 2, :three => 3}) }
|
46
|
+
|
47
|
+
should "be a map" do
|
48
|
+
assert_equal true, subject.map?
|
49
|
+
assert_equal false, subject.list?
|
50
|
+
end
|
51
|
+
|
52
|
+
should "know it's set" do
|
53
|
+
[:one, :two, :three].each{|n| assert subject.set.include?(n)}
|
54
|
+
end
|
55
|
+
|
56
|
+
should "lookup by key" do
|
57
|
+
assert_equal 2, subject[:two]
|
58
|
+
end
|
59
|
+
|
60
|
+
should "lookup by value" do
|
61
|
+
assert_equal 2, subject[2]
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
data/test/helper.rb
CHANGED
@@ -0,0 +1,42 @@
|
|
1
|
+
require "test/helper"
|
2
|
+
|
3
|
+
class ListEnumTest < Test::Unit::TestCase
|
4
|
+
context "instance" do
|
5
|
+
|
6
|
+
subject { Thing.new }
|
7
|
+
before do
|
8
|
+
Thing.send :include, Enumeration
|
9
|
+
Thing.send(:enum, :word, %w{aye bee see})
|
10
|
+
end
|
11
|
+
|
12
|
+
should_have_class_method :word_set
|
13
|
+
should_have_accessor :word
|
14
|
+
|
15
|
+
should "not have a class level lookup method" do
|
16
|
+
assert_raises NoMethodError do
|
17
|
+
Thing.word
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
should "provide class level access to the enum set" do
|
22
|
+
words = Thing.word_set
|
23
|
+
assert words
|
24
|
+
assert_kind_of ::Array, words
|
25
|
+
assert !words.empty?
|
26
|
+
assert_equal 3, words.size
|
27
|
+
assert_equal ['aye', 'bee', 'see'], Thing.word_set
|
28
|
+
end
|
29
|
+
|
30
|
+
should "write by value and read by value" do
|
31
|
+
subject.word = "see"
|
32
|
+
assert_equal "see", subject.word
|
33
|
+
end
|
34
|
+
|
35
|
+
should "write nil for values that aren't in the enum" do
|
36
|
+
subject.word = "bady-bad"
|
37
|
+
assert_equal nil, subject.word
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require "test/helper"
|
2
|
+
|
3
|
+
class MapEnumTest < Test::Unit::TestCase
|
4
|
+
context "instance" do
|
5
|
+
|
6
|
+
subject { Thing.new }
|
7
|
+
before do
|
8
|
+
Thing.send :include, Enumeration
|
9
|
+
Thing.send(:enum, :stuff, {
|
10
|
+
:a => "aye",
|
11
|
+
:b => "bee",
|
12
|
+
:c => "see"
|
13
|
+
})
|
14
|
+
end
|
15
|
+
|
16
|
+
should_have_class_methods :stuff, :stuff_set
|
17
|
+
should_have_accessor :stuff
|
18
|
+
|
19
|
+
should "provide class level access to the enum set" do
|
20
|
+
stuffs = Thing.stuff_set
|
21
|
+
assert stuffs
|
22
|
+
assert_kind_of ::Array, stuffs
|
23
|
+
assert !stuffs.empty?
|
24
|
+
assert_equal 3, stuffs.size
|
25
|
+
[:a, :b, :c].each{|t| assert Thing.stuff_set.include?(t)}
|
26
|
+
end
|
27
|
+
|
28
|
+
should "provide class level lookup of the enum" do
|
29
|
+
assert_equal "aye", Thing.stuff(:a)
|
30
|
+
end
|
31
|
+
|
32
|
+
should "write by key and read by value" do
|
33
|
+
subject.stuff = :b
|
34
|
+
assert_equal "bee", subject.stuff
|
35
|
+
end
|
36
|
+
|
37
|
+
should "write by value and read by value" do
|
38
|
+
subject.stuff = "see"
|
39
|
+
assert_equal "see", subject.stuff
|
40
|
+
end
|
41
|
+
|
42
|
+
should "not read by key" do
|
43
|
+
subject.stuff = :c
|
44
|
+
assert_not_equal :c, subject.stuff
|
45
|
+
end
|
46
|
+
|
47
|
+
should "write nil for keys that aren't in the enum" do
|
48
|
+
subject.stuff = :bad
|
49
|
+
assert_equal nil, subject.stuff
|
50
|
+
end
|
51
|
+
|
52
|
+
should "write nil for values that aren't in the enum" do
|
53
|
+
subject.stuff = "bady-bad"
|
54
|
+
assert_equal nil, subject.stuff
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
data/test/thing.rb
ADDED
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: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
|
+
- 1
|
7
8
|
- 0
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.1
|
10
|
+
version: 1.0.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kelly D. Redding
|
@@ -65,10 +65,15 @@ files:
|
|
65
65
|
- Rakefile
|
66
66
|
- enumeration.gemspec
|
67
67
|
- lib/enumeration.rb
|
68
|
+
- lib/enumeration/collection.rb
|
68
69
|
- lib/enumeration/version.rb
|
69
|
-
- test/
|
70
|
+
- test/api__test.rb
|
71
|
+
- test/collection_test.rb
|
70
72
|
- test/env.rb
|
71
73
|
- test/helper.rb
|
74
|
+
- test/list_enum_test.rb
|
75
|
+
- test/map_enum_test.rb
|
76
|
+
- test/thing.rb
|
72
77
|
has_rdoc: true
|
73
78
|
homepage: http://github.com/kelredd/enumeration
|
74
79
|
licenses: []
|
@@ -104,6 +109,10 @@ signing_key:
|
|
104
109
|
specification_version: 3
|
105
110
|
summary: add enumerated value attributes to your ruby classes
|
106
111
|
test_files:
|
107
|
-
- test/
|
112
|
+
- test/api__test.rb
|
113
|
+
- test/collection_test.rb
|
108
114
|
- test/env.rb
|
109
115
|
- test/helper.rb
|
116
|
+
- test/list_enum_test.rb
|
117
|
+
- test/map_enum_test.rb
|
118
|
+
- test/thing.rb
|