enumeration 1.2.1 → 1.3.1
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.lock +8 -4
- data/enumeration.gemspec +1 -1
- data/lib/enumeration/assert_macros.rb +57 -0
- data/lib/enumeration/collection.rb +37 -38
- data/lib/enumeration/version.rb +1 -1
- data/test/assert_macros_test.rb +33 -0
- metadata +15 -13
data/Gemfile.lock
CHANGED
@@ -1,21 +1,25 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
enumeration (1.
|
4
|
+
enumeration (1.3.1)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: http://rubygems.org/
|
8
8
|
specs:
|
9
|
-
ansi (1.
|
10
|
-
assert (0.
|
9
|
+
ansi (1.4.1)
|
10
|
+
assert (0.7.3)
|
11
|
+
assert-view (~> 0.5)
|
12
|
+
assert-view (0.5.0)
|
11
13
|
ansi (~> 1.3)
|
14
|
+
undies (~> 2.0)
|
12
15
|
rake (0.9.2)
|
16
|
+
undies (2.0.0)
|
13
17
|
|
14
18
|
PLATFORMS
|
15
19
|
ruby
|
16
20
|
|
17
21
|
DEPENDENCIES
|
18
|
-
assert (~> 0.
|
22
|
+
assert (~> 0.7)
|
19
23
|
bundler (~> 1.0)
|
20
24
|
enumeration!
|
21
25
|
rake (~> 0.9.2)
|
data/enumeration.gemspec
CHANGED
@@ -0,0 +1,57 @@
|
|
1
|
+
module Enumeration; end
|
2
|
+
module Enumeration::AssertMacros
|
3
|
+
|
4
|
+
# a set of Assert macros to help write enum definition and
|
5
|
+
# regression tests in Assert (https://github.com/teaminsight/assert)
|
6
|
+
|
7
|
+
def self.included(receiver)
|
8
|
+
receiver.class_eval do
|
9
|
+
extend MacroMethods
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module MacroMethods
|
14
|
+
|
15
|
+
def have_enum(name, *args)
|
16
|
+
values = [*args].flatten
|
17
|
+
type = nil
|
18
|
+
if values.first.kind_of?(::Hash)
|
19
|
+
values = values.first
|
20
|
+
type = 'map'
|
21
|
+
elsif !values.empty?
|
22
|
+
type = 'list'
|
23
|
+
end
|
24
|
+
|
25
|
+
called_from = caller.first
|
26
|
+
macro_name = "have the"
|
27
|
+
macro_name += " #{type}" if type
|
28
|
+
macro_name += " enum '#{name}'"
|
29
|
+
macro_name += " with #{values.inspect} values" if !values.empty?
|
30
|
+
|
31
|
+
Assert::Macro.new(macro_name) do
|
32
|
+
should have_accessor name, [called_from]
|
33
|
+
|
34
|
+
should have_class_method "#{name}_collection", [called_from]
|
35
|
+
|
36
|
+
if type == 'map'
|
37
|
+
should have_class_method name, [called_from]
|
38
|
+
end
|
39
|
+
|
40
|
+
if !values.empty?
|
41
|
+
should "know its '#{name}' enum values", called_from do
|
42
|
+
assert_equal values, subject.class.send("#{name}_collection")
|
43
|
+
end
|
44
|
+
|
45
|
+
if type == 'map'
|
46
|
+
should "map the '#{name}' enum values at the class level", called_from do
|
47
|
+
values.each {|k,v| assert_equal v, subject.class.send(name, k)}
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
@@ -1,50 +1,49 @@
|
|
1
|
-
module Enumeration
|
2
|
-
|
1
|
+
module Enumeration; end
|
2
|
+
class Enumeration::Collection
|
3
3
|
|
4
|
-
|
4
|
+
attr_reader :data
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
10
|
-
@data = map_or_list
|
11
|
-
end
|
12
|
-
|
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
|
6
|
+
def initialize(map_or_list)
|
7
|
+
unless map_or_list.kind_of?(::Hash) || map_or_list.kind_of?(::Array)
|
8
|
+
raise ArgumentError, "please specify the enum collection as a Hash or Array"
|
23
9
|
end
|
10
|
+
@data = map_or_list
|
11
|
+
end
|
24
12
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
end
|
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
|
35
22
|
end
|
23
|
+
end
|
36
24
|
|
37
|
-
|
38
|
-
|
25
|
+
# lookup collection key by a value
|
26
|
+
def key(value)
|
27
|
+
if self.map? && @data.has_value?(value)
|
28
|
+
@data.invert[value]
|
29
|
+
elsif (self.map? && @data.has_key?(value)) ||
|
30
|
+
(self.list? && @data.include?(value))
|
31
|
+
value
|
32
|
+
else
|
33
|
+
nil
|
39
34
|
end
|
35
|
+
end
|
40
36
|
|
41
|
-
|
42
|
-
|
43
|
-
|
37
|
+
def list?
|
38
|
+
@data.kind_of?(::Array)
|
39
|
+
end
|
44
40
|
|
45
|
-
|
46
|
-
|
47
|
-
|
41
|
+
def map?
|
42
|
+
@data.kind_of?(::Hash)
|
43
|
+
end
|
48
44
|
|
45
|
+
def set
|
46
|
+
self.map? ? @data.keys : @data
|
49
47
|
end
|
48
|
+
|
50
49
|
end
|
data/lib/enumeration/version.rb
CHANGED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'assert'
|
2
|
+
|
3
|
+
require 'enumeration'
|
4
|
+
require 'enumeration/assert_macros'
|
5
|
+
|
6
|
+
module Enumeration::AssertMacros
|
7
|
+
|
8
|
+
class BaseTests < Assert::Context
|
9
|
+
desc "Enumeration::AssertMacros"
|
10
|
+
include Enumeration::AssertMacros
|
11
|
+
|
12
|
+
before do
|
13
|
+
Thing.send :include, Enumeration
|
14
|
+
Thing.send(:enum, :list, %w{aye bee see})
|
15
|
+
Thing.send(:enum, :map, {
|
16
|
+
:a => "aye",
|
17
|
+
:b => "bee",
|
18
|
+
:c => "see"
|
19
|
+
})
|
20
|
+
@a_thing = Thing.new
|
21
|
+
end
|
22
|
+
subject { @a_thing }
|
23
|
+
|
24
|
+
should have_enum :list
|
25
|
+
should have_enum :list, ['aye', 'bee', 'see']
|
26
|
+
should have_enum :list, 'aye', 'bee', 'see'
|
27
|
+
|
28
|
+
should have_enum :map
|
29
|
+
should have_enum :map, :a => "aye", :b => "bee", :c => "see"
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
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: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
8
|
+
- 3
|
9
9
|
- 1
|
10
|
-
version: 1.
|
10
|
+
version: 1.3.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kelly D. Redding
|
@@ -15,11 +15,11 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2012-01-10 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
|
-
prerelease: false
|
22
21
|
type: :development
|
22
|
+
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
24
|
none: false
|
25
25
|
requirements:
|
@@ -30,24 +30,23 @@ dependencies:
|
|
30
30
|
- 1
|
31
31
|
- 0
|
32
32
|
version: "1.0"
|
33
|
-
name: bundler
|
34
33
|
version_requirements: *id001
|
34
|
+
name: bundler
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
|
-
prerelease: false
|
37
36
|
type: :development
|
37
|
+
prerelease: false
|
38
38
|
requirement: &id002 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
hash:
|
43
|
+
hash: 5
|
44
44
|
segments:
|
45
45
|
- 0
|
46
|
-
-
|
47
|
-
|
48
|
-
version: 0.2.0
|
49
|
-
name: assert
|
46
|
+
- 7
|
47
|
+
version: "0.7"
|
50
48
|
version_requirements: *id002
|
49
|
+
name: assert
|
51
50
|
description: add enumerated value attributes to your ruby classes
|
52
51
|
email:
|
53
52
|
- kelly@kelredd.com
|
@@ -65,9 +64,11 @@ files:
|
|
65
64
|
- Rakefile
|
66
65
|
- enumeration.gemspec
|
67
66
|
- lib/enumeration.rb
|
67
|
+
- lib/enumeration/assert_macros.rb
|
68
68
|
- lib/enumeration/collection.rb
|
69
69
|
- lib/enumeration/version.rb
|
70
70
|
- test/api_test.rb
|
71
|
+
- test/assert_macros_test.rb
|
71
72
|
- test/collection_test.rb
|
72
73
|
- test/helper.rb
|
73
74
|
- test/irb.rb
|
@@ -103,12 +104,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
104
|
requirements: []
|
104
105
|
|
105
106
|
rubyforge_project:
|
106
|
-
rubygems_version: 1.8.
|
107
|
+
rubygems_version: 1.8.11
|
107
108
|
signing_key:
|
108
109
|
specification_version: 3
|
109
110
|
summary: add enumerated value attributes to your ruby classes
|
110
111
|
test_files:
|
111
112
|
- test/api_test.rb
|
113
|
+
- test/assert_macros_test.rb
|
112
114
|
- test/collection_test.rb
|
113
115
|
- test/helper.rb
|
114
116
|
- test/irb.rb
|