power_enum 0.8.5 → 0.8.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -276,6 +276,11 @@ Example:
276
276
  :active => false
277
277
  end
278
278
 
279
+ ##### acts\_as\_enumerated? (since version 0.8.6)
280
+
281
+ Returns `true` for ActiveRecord models that act as enumerated, `false` for others. So
282
+ `BookingStatus.acts_as_enumerated?` would return `true`, while `Booking.acts_as_enumerated?` would return `false`.
283
+
279
284
  #### Instance Methods
280
285
 
281
286
  Each enumeration model gets the following instance methods.
@@ -533,6 +538,18 @@ This is also valid:
533
538
  end
534
539
  end
535
540
 
541
+ #### match\_enum (Since version 0.8.6)
542
+
543
+ Tests if an enum instance matches the given value, which may be a symbol, id, string, or enum instance:
544
+
545
+ describe Booking do
546
+ it "status should be 'received' for a new booking" do
547
+ Booking.new.status.should match_enum(:received)
548
+ end
549
+ end
550
+
551
+ Of course `Booking.new.status.should === :received` still works, but is liable to produce false positives.
552
+
536
553
  ## How to run tests
537
554
 
538
555
  Go to the 'dummy' project:
@@ -9,6 +9,11 @@ module ActiveRecord
9
9
 
10
10
  module ClassMethods
11
11
 
12
+ # Returns false for ActiveRecord models that do not act as enumerated.
13
+ def acts_as_enumerated?
14
+ false
15
+ end
16
+
12
17
  # Declares the model as enumerated. See the README for detailed usage instructions.
13
18
  #
14
19
  # === Supported options
@@ -101,6 +106,11 @@ module ActiveRecord
101
106
  module EnumClassMethods
102
107
  attr_accessor :enumeration_model_updates_permitted
103
108
 
109
+ # Returns true for ActiveRecord models that act as enumerated.
110
+ def acts_as_enumerated?
111
+ true
112
+ end
113
+
104
114
  # Returns all the enum values. Caches results after the first time this method is run.
105
115
  def all
106
116
  return @all if @all
data/lib/testing/rspec.rb CHANGED
@@ -42,9 +42,8 @@ if defined? RSpec
42
42
  match do |enum|
43
43
  enum_class = get_enum_class(enum)
44
44
 
45
- if enum_class.respond_to?(:[]) &&
46
- enum_class.respond_to?(:enumeration_model_updates_permitted) &&
47
- enum_class.respond_to?(:purge_enumerations_cache)
45
+ if enum_class.respond_to?(:acts_as_enumerated?) &&
46
+ enum_class.acts_as_enumerated?
48
47
 
49
48
  if @items
50
49
  begin
@@ -143,4 +142,39 @@ if defined? RSpec
143
142
  end
144
143
  end
145
144
 
145
+ # Tests if an enum instance matches the given value, which may be a symbol,
146
+ # id, string, or enum instance:
147
+ #
148
+ # describe Booking do
149
+ # it "status should be 'received' for a new booking" do
150
+ # Booking.new.status.should match_enum(:received)
151
+ # end
152
+ # end
153
+ RSpec::Matchers.define :match_enum do |attribute|
154
+ match do |item|
155
+ if item.class.respond_to?(:acts_as_enumerated?) &&
156
+ item.class.acts_as_enumerated?
157
+ begin
158
+ item.class[attribute] == item
159
+ rescue Exception
160
+ false
161
+ end
162
+ else
163
+ false
164
+ end
165
+ end
166
+
167
+ failure_message_for_should do
168
+ "expected #{attribute} to match the enum"
169
+ end
170
+
171
+ failure_message_for_should_not do
172
+ "expected #{attribute} to not match the enum"
173
+ end
174
+
175
+ description do
176
+ "match enum value of #{attribute}"
177
+ end
178
+ end
179
+
146
180
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: power_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.5
4
+ version: 0.8.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2012-08-28 00:00:00.000000000 Z
15
+ date: 2012-08-29 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: rails
@@ -143,7 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
143
  version: '0'
144
144
  segments:
145
145
  - 0
146
- hash: -1049060871827763452
146
+ hash: -1167487358875782212
147
147
  required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  none: false
149
149
  requirements: