123456789_123456789_123456789_123456789_123456789_

Module: ActiveModel::AttributeRegistration::ClassMethods

Do not use. This module is for internal use only.
Relationships & Source Files
Namespace Children
Classes:
Defined in: activemodel/lib/active_model/attribute_registration.rb

Instance Method Summary

Instance Method Details

#_default_attributes

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_registration.rb', line 31

def _default_attributes # :nodoc:
  @default_attributes ||= AttributeSet.new({}).tap do |attribute_set|
    apply_pending_attribute_modifications(attribute_set)
  end
end

#apply_pending_attribute_modifications(attribute_set) (private)

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_registration.rb', line 81

def apply_pending_attribute_modifications(attribute_set)
  if superclass.respond_to?(:apply_pending_attribute_modifications, true)
    superclass.send(:apply_pending_attribute_modifications, attribute_set)
  end

  pending_attribute_modifications.each do |modification|
    modification.apply_to(attribute_set)
  end
end

#attribute(name, type = nil, default: (no_default = true), **options)

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_registration.rb', line 12

def attribute(name, type = nil, default: (no_default = true), **options)
  name = resolve_attribute_name(name)
  type = resolve_type_name(type, **options) if type.is_a?(Symbol)
  type = hook_attribute_type(name, type) if type

  pending_attribute_modifications << PendingType.new(name, type) if type || no_default
  pending_attribute_modifications << PendingDefault.new(name, default) unless no_default

  reset_default_attributes
end

#attribute_types

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_registration.rb', line 37

def attribute_types # :nodoc:
  @attribute_types ||= _default_attributes.cast_types.tap do |hash|
    hash.default = Type.default_value
  end
end

#decorate_attributes(names = nil, &decorator)

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_registration.rb', line 23

def decorate_attributes(names = nil, &decorator) # :nodoc:
  names = names&.map { |name| resolve_attribute_name(name) }

  pending_attribute_modifications << PendingDecorator.new(names, decorator)

  reset_default_attributes
end

#hook_attribute_type(attribute, type) (private)

Hook for other modules to override. The attribute type is passed through this method immediately after it is resolved, before any type decorations are applied.

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_registration.rb', line 112

def hook_attribute_type(attribute, type)
  type
end

#pending_attribute_modifications (private)

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_registration.rb', line 77

def pending_attribute_modifications
  @pending_attribute_modifications ||= []
end

#reset_default_attributes (private)

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_registration.rb', line 91

def reset_default_attributes
  reset_default_attributes!
  subclasses.each { |subclass| subclass.send(:reset_default_attributes) }
end

#reset_default_attributes! (private)

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_registration.rb', line 96

def reset_default_attributes!
  @default_attributes = nil
  @attribute_types = nil
end

#resolve_attribute_name(name) (private)

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_registration.rb', line 101

def resolve_attribute_name(name)
  name.to_s
end

#resolve_type_name(name, **options) (private)

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_registration.rb', line 105

def resolve_type_name(name, **options)
  Type.lookup(name, **options)
end

#type_for_attribute(attribute_name, &block)

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_registration.rb', line 43

def type_for_attribute(attribute_name, &block)
  attribute_name = resolve_attribute_name(attribute_name)

  if block
    attribute_types.fetch(attribute_name, &block)
  else
    attribute_types[attribute_name]
  end
end