Module: PowerEnum::Enumerated::EnumClassMethods
- Defined in:
- lib/power_enum/enumerated.rb
Overview
These are class level methods which are patched into classes that act as enumerated
Instance Attribute Summary collapse
-
#enumeration_model_updates_permitted ⇒ Object
Returns the value of attribute enumeration_model_updates_permitted.
Instance Method Summary collapse
-
#[](*args, &block) ⇒ Object
Enum lookup by Symbol, String, or id.
-
#active ⇒ Object
Returns all the active enum values.
-
#acts_as_enumerated? ⇒ Boolean
Returns true for ActiveRecord models that act as enumerated.
-
#all ⇒ Object
Returns all the enum values.
-
#all_except(*excluded) ⇒ Object
Returns all except for the given list.
-
#contains?(arg) ⇒ Boolean
Returns true if the given Symbol, String or id has a member instance in the enumeration, false otherwise.
-
#enumerations_model_updating? ⇒ Boolean
Returns true if the enumerations model is in the middle of an update_enumerations_model block, false otherwise.
-
#inactive ⇒ Object
Returns all the inactive enum values.
-
#include?(arg) ⇒ Boolean
Returns true if the enum lookup by the given Symbol, String or id would have returned a value, false otherwise.
-
#insert_new_record(arg, description: nil) ⇒ Object
Insert a new record if
argis a Symbol or a String, and flush the enumerations cache. -
#lookup_id(arg) ⇒ Object
Enum lookup by id.
-
#lookup_name(arg) ⇒ Object
Enum lookup by String.
-
#name_column ⇒ Object
Returns the name of the column this enum uses as the basic underlying value.
-
#names ⇒ Object
Returns the names of all the enum values as an array of symbols.
-
#purge_enumerations_cache ⇒ Object
NOTE: purging the cache is sort of pointless because of the per-process rails model.
-
#update_enumerations_model(&block) ⇒ Object
The preferred method to update an enumerations model.
Instance Attribute Details
#enumeration_model_updates_permitted ⇒ Object
Returns the value of attribute enumeration_model_updates_permitted.
174 175 176 |
# File 'lib/power_enum/enumerated.rb', line 174 def enumeration_model_updates_permitted @enumeration_model_updates_permitted end |
Instance Method Details
#[](*args, &block) ⇒ Object
Enum lookup by Symbol, String, or id. Returns arg if arg is an enum instance. Passing in a list of arguments returns a list of enums. When called with no arguments, returns nil.
If the given arg is not in the enumerations cache, the lookup failure handler is set to :call_block and an optional block of the form { |klass, arg| } is passed to this method, the block will be invoked with the first argument being this instance and the second the given argument.
228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/power_enum/enumerated.rb', line 228 def [](*args, &block) case args.size when 0 nil when 1 arg = args.first Array === arg ? self[*arg, &block] : (lookup_enum_by_type(arg) || handle_lookup_failure(arg, &block)) else args.map{ |item| self[item, &block] }.uniq end end |
#active ⇒ Object
Returns all the active enum values. See the 'active?' instance method.
200 201 202 203 |
# File 'lib/power_enum/enumerated.rb', line 200 def active return @all_active if @all_active @all_active = all.find_all{ |enum| enum.active? }.freeze end |
#acts_as_enumerated? ⇒ Boolean
Returns true for ActiveRecord models that act as enumerated.
177 178 179 |
# File 'lib/power_enum/enumerated.rb', line 177 def acts_as_enumerated? true end |
#all ⇒ Object
Returns all the enum values. Caches results after the first time this method is run.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/power_enum/enumerated.rb', line 182 def all return @all if @all freeze_handler = if (handler = self.acts_enumerated_freeze_members).nil? -> { Rails.env.production? } else case handler when Proc handler else -> { handler } end end @all = load_all.collect{ |val| !!freeze_handler.call ? val.freeze : val }.freeze end |
#all_except(*excluded) ⇒ Object
Returns all except for the given list
217 218 219 |
# File 'lib/power_enum/enumerated.rb', line 217 def all_except(*excluded) all.find_all { |item| !(item === excluded) } end |
#contains?(arg) ⇒ Boolean
Returns true if the given Symbol, String or id has a member instance in the enumeration, false otherwise. Returns true if the argument is an enum instance, returns false if the argument is nil or any other value.
244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/power_enum/enumerated.rb', line 244 def contains?(arg) case arg when Symbol !!lookup_name(arg.id2name) when String !!lookup_name(arg) when Integer !!lookup_id(arg) when self true else false end end |
#enumerations_model_updating? ⇒ Boolean
Returns true if the enumerations model is in the middle of an update_enumerations_model block, false otherwise.
329 330 331 |
# File 'lib/power_enum/enumerated.rb', line 329 def enumerations_model_updating? !!@enumerations_model_updating end |
#inactive ⇒ Object
Returns all the inactive enum values. See the 'inactive?' instance method.
206 207 208 209 |
# File 'lib/power_enum/enumerated.rb', line 206 def inactive return @all_inactive if @all_inactive @all_inactive = all.find_all{ |enum| !enum.active? }.freeze end |
#include?(arg) ⇒ Boolean
Returns true if the enum lookup by the given Symbol, String or id would have returned a value, false otherwise.
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/power_enum/enumerated.rb', line 270 def include?(arg) case arg when Symbol !lookup_name(arg.id2name).nil? when String !lookup_name(arg).nil? when Integer !lookup_id(arg).nil? when self possible_match = lookup_id(arg.id) !possible_match.nil? && possible_match == arg else false end end |
#insert_new_record(arg, description: nil) ⇒ Object
Insert a new record if arg is a Symbol or a String, and flush the enumerations cache.
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'lib/power_enum/enumerated.rb', line 429 def insert_new_record(arg, description: nil) if Symbol === arg || String === arg value = arg.to_s upsert_attributes = if column_names.include?("description") { acts_enumerated_name_column => value, description: description || value.capitalize.gsub("_", " ") } else { acts_enumerated_name_column => value } end update_enumerations_model do upsert(upsert_attributes, update_only: [], unique_by: acts_enumerated_name_column) end self[arg] else nil end end |
#lookup_id(arg) ⇒ Object
Enum lookup by id
260 261 262 |
# File 'lib/power_enum/enumerated.rb', line 260 def lookup_id(arg) all_by_id[arg] end |
#lookup_name(arg) ⇒ Object
Enum lookup by String
265 266 267 |
# File 'lib/power_enum/enumerated.rb', line 265 def lookup_name(arg) all_by_name[arg] end |
#name_column ⇒ Object
Returns the name of the column this enum uses as the basic underlying value.
334 335 336 |
# File 'lib/power_enum/enumerated.rb', line 334 def name_column @name_column ||= self.acts_enumerated_name_column end |
#names ⇒ Object
Returns the names of all the enum values as an array of symbols.
212 213 214 |
# File 'lib/power_enum/enumerated.rb', line 212 def names all.map { |item| item.name_sym } end |
#purge_enumerations_cache ⇒ Object
NOTE: purging the cache is sort of pointless because of the per-process rails model. By default this blows up noisily just in case you try to be more clever than rails allows. For those times (like in Migrations) when you really do want to alter the records you can silence the carping by setting enumeration_model_updates_permitted to true.
293 294 295 296 297 298 |
# File 'lib/power_enum/enumerated.rb', line 293 def purge_enumerations_cache unless self.enumeration_model_updates_permitted raise "#{self.name}: cache purging disabled for your protection" end @all = @all_by_name = @all_by_id = @all_active = nil end |
#update_enumerations_model(&block) ⇒ Object
The preferred method to update an enumerations model. The same warnings as 'purge_enumerations_cache' and 'enumerations_model_update_permitted' apply. Pass a block to this method where you perform your updates. Cache will be flushed automatically. If your block takes an argument, will pass in the model class. The argument is optional.
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/power_enum/enumerated.rb', line 306 def update_enumerations_model(&block) if block_given? begin self.enumeration_model_updates_permitted = true purge_enumerations_cache @all = load_all @enumerations_model_updating = true case block.arity when 0 yield else yield self end ensure purge_enumerations_cache @enumerations_model_updating = false self.enumeration_model_updates_permitted = false end end end |