Module: EnumerateBy::ClassMethods

Defined in:
lib/enumerate_by.rb

Instance Method Summary collapse

Instance Method Details

#enumeration?Boolean

Does this class define an enumeration? Always true.

Returns:

  • (Boolean)


119
120
121
# File 'lib/enumerate_by.rb', line 119

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')         # => []


156
157
158
# File 'lib/enumerate_by.rb', line 156

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.



170
171
172
173
174
175
# File 'lib/enumerate_by.rb', line 170

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


131
132
133
# File 'lib/enumerate_by.rb', line 131

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.



145
146
147
# File 'lib/enumerate_by.rb', line 145

def find_by_enumerator!(enumerator)
  find_by_enumerator(enumerator) || raise(ActiveRecord::RecordNotFound, "Couldn't find #{name} with #{enumerator_attribute} #{typecast_enumerator(enumerator).inspect}")
end

#uncachedObject

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.



195
196
197
198
199
200
201
# File 'lib/enumerate_by.rb', line 195

def uncached
  old = perform_enumerator_caching
  self.perform_enumerator_caching = false
  super
ensure
  self.perform_enumerator_caching = old
end