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) ⇒ 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.
-
#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.
171 172 173 |
# File 'lib/power_enum/enumerated.rb', line 171 def enumeration_model_updates_permitted @enumeration_model_updates_permitted end |
Instance Method Details
#[](*args) ⇒ 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.
221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/power_enum/enumerated.rb', line 221 def [](*args) case args.size when 0 nil when 1 arg = args.first Array === arg ? self[*arg] : (lookup_enum_by_type(arg) || handle_lookup_failure(arg)) else args.map{ |item| self[item] }.uniq end end |
#active ⇒ Object
Returns all the active enum values. See the 'active?' instance method.
197 198 199 200 |
# File 'lib/power_enum/enumerated.rb', line 197 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.
174 175 176 |
# File 'lib/power_enum/enumerated.rb', line 174 def acts_as_enumerated? true end |
#all ⇒ Object
Returns all the enum values. Caches results after the first time this method is run.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/power_enum/enumerated.rb', line 179 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
214 215 216 |
# File 'lib/power_enum/enumerated.rb', line 214 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.
237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/power_enum/enumerated.rb', line 237 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.
322 323 324 |
# File 'lib/power_enum/enumerated.rb', line 322 def enumerations_model_updating? !!@enumerations_model_updating end |
#inactive ⇒ Object
Returns all the inactive enum values. See the 'inactive?' instance method.
203 204 205 206 |
# File 'lib/power_enum/enumerated.rb', line 203 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.
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/power_enum/enumerated.rb', line 263 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 |
#lookup_id(arg) ⇒ Object
Enum lookup by id
253 254 255 |
# File 'lib/power_enum/enumerated.rb', line 253 def lookup_id(arg) all_by_id[arg] end |
#lookup_name(arg) ⇒ Object
Enum lookup by String
258 259 260 |
# File 'lib/power_enum/enumerated.rb', line 258 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.
327 328 329 |
# File 'lib/power_enum/enumerated.rb', line 327 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.
209 210 211 |
# File 'lib/power_enum/enumerated.rb', line 209 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.
286 287 288 289 290 291 |
# File 'lib/power_enum/enumerated.rb', line 286 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.
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/power_enum/enumerated.rb', line 299 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 |