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.



367
368
369
# File 'lib/enumerate_by.rb', line 367

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.



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

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)


378
379
380
# File 'lib/enumerate_by.rb', line 378

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"


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

def to_s
  to_str
end

#to_strObject

Add support for equality comparison with strings



401
402
403
# File 'lib/enumerate_by.rb', line 401

def to_str
  enumerator.to_s
end