Module: EnumerateBy::ClassMethods
- Defined in:
- lib/enumerate_by.rb
Instance Method Summary collapse
-
#enumeration? ⇒ Boolean
Does this class define an enumeration? Always true.
-
#find_all_by_enumerator(enumerators) ⇒ Object
Finds records with the given enumerators.
-
#find_all_by_enumerator!(enumerators) ⇒ Object
Finds records with the given enumerators.
-
#find_by_enumerator(enumerator) ⇒ Object
Finds the record that is associated with the given enumerator.
-
#find_by_enumerator!(enumerator) ⇒ Object
(also: #[])
Finds the record that is associated with the given enumerator.
-
#uncached ⇒ Object
Temporarily disables the enumeration cache (as well as the query cache) within the context of the given block if the enumeration is configured to allow caching.
Instance Method Details
#enumeration? ⇒ Boolean
Does this class define an enumeration? Always true.
115 116 117 |
# File 'lib/enumerate_by.rb', line 115 def enumeration? true end |
#find_all_by_enumerator(enumerators) ⇒ Object
Finds records with the given enumerators.
For example,
Color.find_all_by_enumerator(['red', 'green']) # => [#<Color id: 1, name: "red">, #<Color id: 1, name: "green">]
Color.find_all_by_enumerator('invalid') # => []
152 153 154 |
# File 'lib/enumerate_by.rb', line 152 def find_all_by_enumerator(enumerators) all(:conditions => {enumerator_attribute => typecast_enumerator(enumerators)}) end |
#find_all_by_enumerator!(enumerators) ⇒ Object
Finds records with the given enumerators. If no record is found for a particular enumerator, then an ActiveRecord::RecordNotFound exception is raised.
For Example,
Color.find_all_by_enumerator!(['red', 'green']) # => [#<Color id: 1, name: "red">, #<Color id: 1, name: "green">]
Color.find_all_by_enumerator!('invalid') # => ActiveRecord::RecordNotFound: Couldn't find Color with name(s) "invalid"
To avoid raising an exception on invalid enumerators, use find_all_by_enumerator.
166 167 168 169 170 171 |
# File 'lib/enumerate_by.rb', line 166 def find_all_by_enumerator!(enumerators) enumerators = [enumerators].flatten records = find_all_by_enumerator(enumerators) missing = enumerators - records.map(&:enumerator) missing.empty? ? records : raise(ActiveRecord::RecordNotFound, "Couldn't find #{name} with #{enumerator_attribute}(s) #{missing.map(&:inspect).to_sentence}") end |
#find_by_enumerator(enumerator) ⇒ Object
Finds the record that is associated with the given enumerator. The attribute that defines the enumerator is based on what was specified when calling enumerate_by.
For example,
Color.find_by_enumerator('red') # => #<Color id: 1, name: "red">
Color.find_by_enumerator('invalid') # => nil
127 128 129 |
# File 'lib/enumerate_by.rb', line 127 def find_by_enumerator(enumerator) first(:conditions => {enumerator_attribute => typecast_enumerator(enumerator)}) end |
#find_by_enumerator!(enumerator) ⇒ Object Also known as: []
Finds the record that is associated with the given enumerator. If no record is found, then an ActiveRecord::RecordNotFound exception is raised.
For example,
Color['red'] # => #<Color id: 1, name: "red">
Color['invalid'] # => ActiveRecord::RecordNotFound: Couldn't find Color with name "red"
To avoid raising an exception on invalid enumerators, use find_by_enumerator.
141 142 143 |
# File 'lib/enumerate_by.rb', line 141 def find_by_enumerator!(enumerator) find_by_enumerator(enumerator) || raise(ActiveRecord::RecordNotFound, "Couldn't find #{name} with #{enumerator_attribute} #{typecast_enumerator(enumerator).inspect}") end |
#uncached ⇒ Object
Temporarily disables the enumeration cache (as well as the query cache) within the context of the given block if the enumeration is configured to allow caching.
191 192 193 194 195 196 197 |
# File 'lib/enumerate_by.rb', line 191 def uncached old = perform_enumerator_caching self.perform_enumerator_caching = false super ensure self.perform_enumerator_caching = old end |