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.



353
354
355
# File 'lib/enumerate_by.rb', line 353

def ==(arg)
  arg.is_a?(String) ? self == self.class.find_by_enumerator!(arg) : super
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.



372
373
374
# File 'lib/enumerate_by.rb', line 372

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


364
365
366
# File 'lib/enumerate_by.rb', line 364

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"


382
383
384
# File 'lib/enumerate_by.rb', line 382

def to_s
  to_str
end

#to_strObject

Add support for equality comparison with strings



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

def to_str
  enumerator.to_s
end