power_enum 0.9.3 → 0.9.4

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.
Files changed (2) hide show
  1. data/lib/power_enum/enumerated.rb +20 -3
  2. metadata +3 -3
@@ -125,10 +125,15 @@ module PowerEnum::Enumerated
125
125
  # Returns all the enum values. Caches results after the first time this method is run.
126
126
  def all
127
127
  return @all if @all
128
+ @all = load_all.collect{|val| val.freeze}.freeze
129
+ end
130
+
131
+ def load_all
128
132
  conditions = self.acts_enumerated_conditions
129
133
  order = self.acts_enumerated_order
130
- @all = where(conditions).order(order).collect{|val| val.freeze}.freeze
134
+ where(conditions).order(order)
131
135
  end
136
+ private :load_all
132
137
 
133
138
  # Returns all the active enum values. See the 'active?' instance method.
134
139
  def active
@@ -242,6 +247,9 @@ module PowerEnum::Enumerated
242
247
  if block_given?
243
248
  begin
244
249
  self.enumeration_model_updates_permitted = true
250
+ purge_enumerations_cache
251
+ @all = load_all
252
+ @enumerations_model_updating = true
245
253
  case block.arity
246
254
  when 0
247
255
  yield
@@ -250,11 +258,18 @@ module PowerEnum::Enumerated
250
258
  end
251
259
  ensure
252
260
  purge_enumerations_cache
261
+ @enumerations_model_updating = false
253
262
  self.enumeration_model_updates_permitted = false
254
263
  end
255
264
  end
256
265
  end
257
266
 
267
+ # Returns true if the enumerations model is in the middle of an
268
+ # update_enumerations_model block, false otherwise.
269
+ def enumerations_model_updating?
270
+ !!@enumerations_model_updating
271
+ end
272
+
258
273
  # Returns the name of the column this enum uses as the basic underlying value.
259
274
  def name_column
260
275
  @name_column ||= self.acts_enumerated_name_column
@@ -282,10 +297,12 @@ module PowerEnum::Enumerated
282
297
  private :all_by_name
283
298
 
284
299
  def all_by_attribute(attr)
285
- all.inject({}) { |memo, item|
300
+ aba = all.inject({}) { |memo, item|
286
301
  memo[item.send(attr)] = item
287
302
  memo
288
- }.freeze
303
+ }
304
+ aba.freeze unless enumerations_model_updating?
305
+ aba
289
306
  end
290
307
  private :all_by_attribute
291
308
 
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.9.3
4
+ version: 0.9.4
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-09-24 00:00:00.000000000 Z
15
+ date: 2012-11-17 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: rails
@@ -145,7 +145,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
145
145
  version: '0'
146
146
  segments:
147
147
  - 0
148
- hash: -2052291500201023565
148
+ hash: 2627295504409005826
149
149
  required_rubygems_version: !ruby/object:Gem::Requirement
150
150
  none: false
151
151
  requirements: