123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::Enum::EnumMethods

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::Module
Instance Chain:
Inherits: Module
Defined in: activerecord/lib/active_record/enum.rb

Constant Summary

::Module - Inherited

DelegationError

Class Attribute Summary

Class Method Summary

Instance Attribute Summary

::Module - Inherited

#anonymous?

A module may or may not have a name.

Instance Method Summary

::Module - Inherited

#alias_attribute

Allows you to make aliases for attributes, which includes getter, setter, and a predicate.

#attr_internal
#attr_internal_accessor

Declares an attribute reader and writer backed by an internally-named instance variable.

#attr_internal_reader

Declares an attribute reader backed by an internally-named instance variable.

#attr_internal_writer

Declares an attribute writer backed by an internally-named instance variable.

#cattr_accessor
#cattr_reader
#cattr_writer
#deep_dup

Returns a copy of module or class if it's anonymous.

#delegate

Provides a delegate class method to easily expose contained objects' public methods as your own.

#delegate_missing_to

When building decorators, a common pattern may emerge:

#deprecate

deprecate :foo, deprecator: MyLib.deprecator

#mattr_accessor

Defines both class and instance accessors for class attributes.

#mattr_reader

Defines a class attribute and creates a class and instance reader methods.

#mattr_writer

Defines a class attribute and creates a class and instance writer methods to allow assignment to the attribute.

#module_parent

Returns the module which contains this one according to its name.

#module_parent_name

Returns the name of the module containing this one.

#module_parents

Returns all the parents of this module according to its name, ordered from nested outwards.

#redefine_method

Replaces the existing method definition, if there is one, with the passed block as its body.

#redefine_singleton_method

Replaces the existing singleton method definition, if there is one, with the passed block as its body.

#remove_possible_method

Removes the named method, if it exists.

#remove_possible_singleton_method

Removes the named singleton method, if it exists.

#silence_redefinition_of_method

Marks the named method as intended to be redefined, if it exists.

#thread_cattr_accessor
#thread_cattr_reader
#thread_cattr_writer
#thread_mattr_accessor

Defines both class and instance accessors for class attributes.

#attr_internal_define, #as_json, #method_visibility,
#thread_mattr_reader

Defines a per-thread class attribute and creates class and instance reader methods.

#thread_mattr_writer

Defines a per-thread class attribute and creates a class and instance writer methods to allow assignment to the attribute.

::Module::Concerning - Included

#concern

A low-cruft shortcut to define a concern.

#concerning

Define a new concern and mix it in.

Constructor Details

.new(klass) ⇒ EnumMethods

[ GitHub ]

  
# File 'activerecord/lib/active_record/enum.rb', line 335

def initialize(klass)
  @klass = klass
end

Instance Attribute Details

#klass (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/enum.rb', line 340

attr_reader :klass

Instance Method Details

#define_enum_method(code_generator, name, value, suffix, as:) (private)

The generated body only depends on the attribute name and value, so the canonical name encodes both and the method is shared by every model with the same enum value.

[ GitHub ]

  
# File 'activerecord/lib/active_record/enum.rb', line 388

def define_enum_method(code_generator, name, value, suffix, as:)
  method_name = "__temp__#{"#{name}\0#{value.inspect}".unpack1("h*")}#{suffix}"
  code_generator.define_cached_method(method_name, as: as, namespace: :active_record_enum) do |batch|
    yield batch, method_name
  end
end

#define_enum_methods(code_generator, name, value_method_name, value, scopes, instance_methods) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/enum.rb', line 342

def define_enum_methods(code_generator, name, value_method_name, value, scopes, instance_methods)
  if instance_methods
    # Non-finite floats have no literal representation.
    compilable = !(Float === value && !value.finite?)

    # def active?() status_for_database == 0 end
    klass.send(:detect_enum_conflict!, name, "#{value_method_name}?")
    if compilable
      reader = "#{name}_for_database"
      reader_call = if ActiveModel::AttributeMethods::CALL_COMPILABLE_REGEXP.match?(reader)
        "self.#{reader}"
      else
        "public_send(#{reader.to_sym.inspect})"
      end
      define_enum_method(code_generator, name, value, "?", as: "#{value_method_name}?") do |batch, method_name|
        batch << "def #{method_name}" << "#{reader_call} == #{value.inspect}" << "end"
      end
    else
      define_method("#{value_method_name}?") { public_send(:"#{name}_for_database") == value }
    end

    # def active!() update!(status: 0) end
    klass.send(:detect_enum_conflict!, name, "#{value_method_name}!")
    if compilable
      define_enum_method(code_generator, name, value, "!", as: "#{value_method_name}!") do |batch, method_name|
        batch << "def #{method_name}" << "update!(#{name.dump} => #{value.inspect})" << "end"
      end
    else
      define_method("#{value_method_name}!") { update!(name => value) }
    end
  end

  if scopes
    # scope :active, -> { where(status: 0) }
    klass.send(:detect_enum_conflict!, name, value_method_name, true)
    klass.scope value_method_name, -> { where(name => value) }

    # scope :not_active, -> { where.not(status: 0) }
    klass.send(:detect_enum_conflict!, name, "not_#{value_method_name}", true)
    klass.scope "not_#{value_method_name}", -> { where(predicate_builder[name, value, :is_distinct_from]) }
  end
end