Module: EnumerateBy::InstanceMethods

Defined in:
lib/enumerate_by.rb

Instance Method Summary collapse

Instance Method Details

#==(arg) ⇒ Object

Whether or not this record is equal to the given value. If the value is a String, then it is compared against the enumerator. Otherwise, ActiveRecord's default equality comparator is used.



385
386
387
# File 'lib/enumerate_by.rb', line 385

def ==(arg)
  arg.nil? || arg.is_a?(self.class) ? super : self == self.class.find_by_enumerator!(arg)
end

#enumeratorObject

A helper method for getting the current value of the enumerator attribute for this record. For example, if this record's model is enumerated by the attribute name, then this will return the current value for name.



404
405
406
# File 'lib/enumerate_by.rb', line 404

def enumerator
  send(enumerator_attribute)
end

#in?(*list) ⇒ Boolean

Determines whether this enumeration is in the given list.

For example,

color = Color.find_by_name('red')   # => #<Color id: 1, name: "red">
color.in?('green')                  # => false
color.in?('red', 'green')           # => true

Returns:

  • (Boolean)


396
397
398
# File 'lib/enumerate_by.rb', line 396

def in?(*list)
  list.any? {|item| self === item}
end

#to_sObject

Stringifies the record typecasted to the enumerator value.

For example,

color = Color.find_by_name('red')   # => #<Color id: 1, name: "red">
color.to_s                          # => "red"


414
415
416
# File 'lib/enumerate_by.rb', line 414

def to_s
  to_str
end

#to_strObject

Add support for equality comparison with strings



419
420
421
# File 'lib/enumerate_by.rb', line 419

def to_str
  enumerator.to_s
end