power_enum 0.5.5 → 0.6.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 +55 -0
- data/lib/power_enum.rb +1 -0
- data/lib/testing/rspec.rb +146 -0
- metadata +105 -77
data/README.md
CHANGED
@@ -47,6 +47,7 @@ This package adds:
|
|
47
47
|
- Two mixins and a helper to ActiveRecord
|
48
48
|
- Methods to migrations to simplify the creation of backing tables
|
49
49
|
- A generator to make generating enums easy
|
50
|
+
- Custom RSpec matchers to streamline the testing of enums and enumerated attributes (Since version 0.6.0)
|
50
51
|
|
51
52
|
`acts_as_enumerated` provides capabilities to treat your model and its records as an enumeration.
|
52
53
|
At a minimum, the database table for an acts\_as\_enumerated must contain an 'id' column and a column
|
@@ -433,6 +434,60 @@ accordingly.
|
|
433
434
|
|
434
435
|
See virtual\_enumerations\_sample.rb in the examples directory of this gem for a full description.
|
435
436
|
|
437
|
+
### Testing (Since version 0.6.0)
|
438
|
+
|
439
|
+
A pair of custom RSpec matchers are included to streamline testing of enums and enumerated attributes.
|
440
|
+
|
441
|
+
#### act\_as\_enumerated
|
442
|
+
|
443
|
+
This is used to test that a model acts as enumerated. Example:
|
444
|
+
|
445
|
+
describe BookingStatus do
|
446
|
+
it { should act_as_enumerated }
|
447
|
+
end
|
448
|
+
|
449
|
+
This also works:
|
450
|
+
|
451
|
+
describe BookingStatus do
|
452
|
+
it "should act as enumerated" do
|
453
|
+
BookingStatus.should act_as_enumerated
|
454
|
+
end
|
455
|
+
end
|
456
|
+
|
457
|
+
You can use the `with_items` chained matcher to test that each enum is properly seeded:
|
458
|
+
|
459
|
+
describe BookingStatus do
|
460
|
+
it {
|
461
|
+
should act_as_enumerated.with_items(:confirmed, :received, :rejected)
|
462
|
+
}
|
463
|
+
end
|
464
|
+
|
465
|
+
You can also pass in hashes if you want to be thorough and test out all the attributes of each enum. If
|
466
|
+
you do this, you must pass in the `:name` attribute in each hash
|
467
|
+
|
468
|
+
describe BookingStatus do
|
469
|
+
it {
|
470
|
+
should act_as_enumerated.with_items({ :name => 'confirmed', :description => "Processed and confirmed" },
|
471
|
+
{ :name => 'received', :description => "Pending confirmation" },
|
472
|
+
{ :name => 'rejected', :description => "Rejected due to internal rules" })
|
473
|
+
}
|
474
|
+
end
|
475
|
+
|
476
|
+
#### have\_enumerated
|
477
|
+
|
478
|
+
This is used to test that a model has enumerated the given attribute:
|
479
|
+
|
480
|
+
describe Booking do
|
481
|
+
it { should have_enumerated(:status) }
|
482
|
+
end
|
483
|
+
|
484
|
+
This is also valid:
|
485
|
+
|
486
|
+
describe Booking do
|
487
|
+
it "Should have enumerated the status attribute" do
|
488
|
+
Booking.should have_enumerated(:status)
|
489
|
+
end
|
490
|
+
end
|
436
491
|
|
437
492
|
## How to run tests
|
438
493
|
|
data/lib/power_enum.rb
CHANGED
@@ -0,0 +1,146 @@
|
|
1
|
+
if defined? RSpec
|
2
|
+
require 'rspec/expectations'
|
3
|
+
|
4
|
+
# This is used to test that a model acts as enumerated. Example:
|
5
|
+
#
|
6
|
+
# describe BookingStatus do
|
7
|
+
# it { should act_as_enumerated }
|
8
|
+
# end
|
9
|
+
#
|
10
|
+
# This also works:
|
11
|
+
#
|
12
|
+
# describe BookingStatus do
|
13
|
+
# it "should act as enumerated" do
|
14
|
+
# BookingStatus.should act_as_enumerated
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# You can use the `with_items` chained matcher to test that each enum is properly seeded:
|
19
|
+
#
|
20
|
+
# describe BookingStatus do
|
21
|
+
# it {
|
22
|
+
# should act_as_enumerated.with_items(:confirmed, :received, :rejected)
|
23
|
+
# }
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# You can also pass in hashes if you want to be thorough and test out all the attributes of each enum. If
|
27
|
+
# you do this, you must pass in the `:name` attribute in each hash
|
28
|
+
#
|
29
|
+
# describe BookingStatus do
|
30
|
+
# it {
|
31
|
+
# should act_as_enumerated.with_items({ :name => 'confirmed', :description => "Processed and confirmed" },
|
32
|
+
# { :name => 'received', :description => "Pending confirmation" },
|
33
|
+
# { :name => 'rejected', :description => "Rejected due to internal rules" })
|
34
|
+
# }
|
35
|
+
# end
|
36
|
+
RSpec::Matchers.define :act_as_enumerated do
|
37
|
+
|
38
|
+
chain :with_items do |*args|
|
39
|
+
@items = args
|
40
|
+
end
|
41
|
+
|
42
|
+
match do |enum|
|
43
|
+
enum_class = get_enum_class(enum)
|
44
|
+
|
45
|
+
if enum_class.respond_to?(:[]) &&
|
46
|
+
enum_class.respond_to?(:enumeration_model_updates_permitted) &&
|
47
|
+
enum_class.respond_to?(:purge_enumerations_cache)
|
48
|
+
|
49
|
+
if @items
|
50
|
+
begin
|
51
|
+
@items.all? { |item| validate_enum(enum_class, item) }
|
52
|
+
rescue Exception
|
53
|
+
false
|
54
|
+
end
|
55
|
+
else
|
56
|
+
true
|
57
|
+
end
|
58
|
+
else
|
59
|
+
false
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def get_enum_class(enum)
|
64
|
+
if enum.is_a?(Class)
|
65
|
+
enum
|
66
|
+
else
|
67
|
+
enum.class
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def validate_enum(enum_class, item)
|
72
|
+
case item
|
73
|
+
when String, Symbol, Fixnum
|
74
|
+
enum_class[item].present?
|
75
|
+
when Hash
|
76
|
+
name = item[:name]
|
77
|
+
if (e = enum_class[name]).present?
|
78
|
+
item.all?{ |attribute, value|
|
79
|
+
e.send(attribute) == value
|
80
|
+
}
|
81
|
+
else
|
82
|
+
false
|
83
|
+
end
|
84
|
+
else
|
85
|
+
false
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
failure_message_for_should do
|
90
|
+
message = "should act as enumerated"
|
91
|
+
if @items
|
92
|
+
message << " and have members #{@items.inspect}"
|
93
|
+
end
|
94
|
+
message
|
95
|
+
end
|
96
|
+
|
97
|
+
failure_message_for_should_not do
|
98
|
+
message = "should not act as enumerated"
|
99
|
+
if @items
|
100
|
+
message << " and not have members #{@items.inspect}"
|
101
|
+
end
|
102
|
+
message
|
103
|
+
end
|
104
|
+
|
105
|
+
description do
|
106
|
+
"act as enumerated"
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
# This is used to test that a model has enumerated the given attribute:
|
111
|
+
#
|
112
|
+
# describe Booking do
|
113
|
+
# it { should have_enumerated(:status) }
|
114
|
+
# end
|
115
|
+
#
|
116
|
+
# This is also valid:
|
117
|
+
#
|
118
|
+
# describe Booking do
|
119
|
+
# it "Should have enumerated the status attribute" do
|
120
|
+
# Booking.should have_enumerated(:status)
|
121
|
+
# end
|
122
|
+
# end
|
123
|
+
RSpec::Matchers.define :have_enumerated do |attribute|
|
124
|
+
match do |model|
|
125
|
+
model_class = if model.is_a?(Class)
|
126
|
+
model
|
127
|
+
else
|
128
|
+
model.class
|
129
|
+
end
|
130
|
+
model_class.has_enumerated?(attribute)
|
131
|
+
end
|
132
|
+
|
133
|
+
failure_message_for_should do
|
134
|
+
"expected #{attribute} to be an enumerated attribute"
|
135
|
+
end
|
136
|
+
|
137
|
+
failure_message_for_should_not do
|
138
|
+
"expected #{attribute} to not be an enumerated attribute"
|
139
|
+
end
|
140
|
+
|
141
|
+
description do
|
142
|
+
"have enumerated #{attribute}"
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
metadata
CHANGED
@@ -1,10 +1,15 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: power_enum
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 7
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 6
|
9
|
+
- 0
|
10
|
+
version: 0.6.0
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Trevor Squires
|
9
14
|
- Pivotal Labs
|
10
15
|
- Arthur Shagall
|
@@ -12,82 +17,98 @@ authors:
|
|
12
17
|
autorequire:
|
13
18
|
bindir: bin
|
14
19
|
cert_chain: []
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
requirement: &
|
20
|
+
|
21
|
+
date: 2012-02-20 00:00:00 Z
|
22
|
+
dependencies:
|
23
|
+
- !ruby/object:Gem::Dependency
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
25
|
none: false
|
21
|
-
requirements:
|
22
|
-
- -
|
23
|
-
- !ruby/object:Gem::Version
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 3
|
32
|
+
- 0
|
33
|
+
- 0
|
24
34
|
version: 3.0.0
|
25
|
-
|
35
|
+
version_requirements: *id001
|
36
|
+
name: rails
|
26
37
|
prerelease: false
|
27
|
-
|
28
|
-
- !ruby/object:Gem::Dependency
|
29
|
-
|
30
|
-
requirement: &15442580 !ruby/object:Gem::Requirement
|
38
|
+
type: :runtime
|
39
|
+
- !ruby/object:Gem::Dependency
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
41
|
none: false
|
32
|
-
requirements:
|
33
|
-
- -
|
34
|
-
- !ruby/object:Gem::Version
|
35
|
-
|
36
|
-
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
version_requirements: *id002
|
50
|
+
name: jeweler
|
37
51
|
prerelease: false
|
38
|
-
version_requirements: *15442580
|
39
|
-
- !ruby/object:Gem::Dependency
|
40
|
-
name: rspec
|
41
|
-
requirement: &15440860 !ruby/object:Gem::Requirement
|
42
|
-
none: false
|
43
|
-
requirements:
|
44
|
-
- - ! '>='
|
45
|
-
- !ruby/object:Gem::Version
|
46
|
-
version: '0'
|
47
52
|
type: :development
|
48
|
-
|
49
|
-
|
50
|
-
- !ruby/object:Gem::Dependency
|
51
|
-
name: sqlite3
|
52
|
-
requirement: &15439280 !ruby/object:Gem::Requirement
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
55
|
none: false
|
54
|
-
requirements:
|
55
|
-
- -
|
56
|
-
- !ruby/object:Gem::Version
|
57
|
-
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
version_requirements: *id003
|
64
|
+
name: rspec
|
65
|
+
prerelease: false
|
58
66
|
type: :development
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 3
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
version_requirements: *id004
|
78
|
+
name: sqlite3
|
59
79
|
prerelease: false
|
60
|
-
|
61
|
-
- !ruby/object:Gem::Dependency
|
62
|
-
|
63
|
-
requirement: &15438200 !ruby/object:Gem::Requirement
|
80
|
+
type: :development
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
64
83
|
none: false
|
65
|
-
requirements:
|
66
|
-
- - =
|
67
|
-
- !ruby/object:Gem::Version
|
84
|
+
requirements:
|
85
|
+
- - "="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
hash: 21
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
- 2
|
91
|
+
- 1
|
68
92
|
version: 0.2.1
|
69
|
-
|
93
|
+
version_requirements: *id005
|
94
|
+
name: genspec
|
70
95
|
prerelease: false
|
71
|
-
|
72
|
-
description:
|
73
|
-
as though they were an enumeration of values.
|
74
|
-
|
75
|
-
It
|
76
|
-
|
77
|
-
|
78
|
-
It is particularly suitable for scenarios where your Rails application is not the
|
79
|
-
only user of the database, such as
|
80
|
-
|
81
|
-
when it''s used for analytics or reporting.
|
96
|
+
type: :development
|
97
|
+
description: |
|
98
|
+
Power Enum allows you to treat instances of your ActiveRecord models as though they were an enumeration of values.
|
99
|
+
It allows you to cleanly solve many of the problems that the traditional Rails alternatives handle poorly if at all.
|
100
|
+
It is particularly suitable for scenarios where your Rails application is not the only user of the database, such as
|
101
|
+
when it's used for analytics or reporting.
|
82
102
|
|
83
|
-
'
|
84
103
|
email: arthur.shagall@gmail.com
|
85
104
|
executables: []
|
105
|
+
|
86
106
|
extensions: []
|
87
|
-
|
107
|
+
|
108
|
+
extra_rdoc_files:
|
88
109
|
- LICENSE
|
89
110
|
- README.md
|
90
|
-
files:
|
111
|
+
files:
|
91
112
|
- examples/virtual_enumerations_sample.rb
|
92
113
|
- lib/active_record/acts/enumerated.rb
|
93
114
|
- lib/active_record/aggregations/has_enumerated.rb
|
@@ -101,34 +122,41 @@ files:
|
|
101
122
|
- lib/power_enum/migration/command_recorder.rb
|
102
123
|
- lib/power_enum/reflection.rb
|
103
124
|
- lib/power_enum/schema/schema_statements.rb
|
125
|
+
- lib/testing/rspec.rb
|
104
126
|
- LICENSE
|
105
127
|
- README.md
|
106
128
|
homepage: http://github.com/albertosaurus/enumerations_mixin
|
107
129
|
licenses: []
|
130
|
+
|
108
131
|
post_install_message:
|
109
132
|
rdoc_options: []
|
110
|
-
|
133
|
+
|
134
|
+
require_paths:
|
111
135
|
- lib
|
112
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
136
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
137
|
none: false
|
114
|
-
requirements:
|
115
|
-
- -
|
116
|
-
- !ruby/object:Gem::Version
|
117
|
-
|
118
|
-
segments:
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
hash: 3
|
142
|
+
segments:
|
119
143
|
- 0
|
120
|
-
|
121
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
144
|
+
version: "0"
|
145
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
146
|
none: false
|
123
|
-
requirements:
|
124
|
-
- -
|
125
|
-
- !ruby/object:Gem::Version
|
126
|
-
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
hash: 3
|
151
|
+
segments:
|
152
|
+
- 0
|
153
|
+
version: "0"
|
127
154
|
requirements: []
|
155
|
+
|
128
156
|
rubyforge_project:
|
129
157
|
rubygems_version: 1.8.10
|
130
158
|
signing_key:
|
131
159
|
specification_version: 3
|
132
|
-
summary: Allows you to treat instances of your ActiveRecord models as though they
|
133
|
-
were an enumeration of values
|
160
|
+
summary: Allows you to treat instances of your ActiveRecord models as though they were an enumeration of values
|
134
161
|
test_files: []
|
162
|
+
|