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 Attribute Summary
- #pending_attribute_modifications readonly private
Instance Method Summary
- #_default_attributes
-
#apply_pending_attribute_modifications(attributes)
Applies this class's (and its ancestors') attribute declarations to
attributes, a hash of name =>::ActiveModel::Attribute, and returns it. - #attribute(name, type = nil, default: (no_default = true), **options)
- #attribute_types
- #decorate_attributes(names = nil, &decorator)
- #inherited(base)
- #make_pending_attribute_modifications_shareable
- #type_for_attribute(attribute_name, &block)
- #add_pending_attribute_modification(modification) private
-
#hook_attribute_type(attribute, type)
private
Hook for other modules to override.
- #reset_default_attributes private
- #reset_default_attributes! private
- #resolve_attribute_name(name) private
- #resolve_type_name(name, **options) private
Instance Attribute Details
#pending_attribute_modifications (readonly, private)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_registration.rb', line 109
attr_reader :pending_attribute_modifications
Instance Method Details
#_default_attributes
[ GitHub ]# File 'activemodel/lib/active_model/attribute_registration.rb', line 41
def _default_attributes # :nodoc: @default_attributes ||= AttributeSet.new(apply_pending_attribute_modifications({})) end
#add_pending_attribute_modification(modification) (private)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_registration.rb', line 111
def add_pending_attribute_modification(modification) @pending_attribute_modifications = [*@pending_attribute_modifications, modification] end
#apply_pending_attribute_modifications(attributes)
Applies this class's (and its ancestors') attribute declarations to
attributes, a hash of name => ::ActiveModel::Attribute, and returns it.
# File 'activemodel/lib/active_model/attribute_registration.rb', line 64
def apply_pending_attribute_modifications(attributes) # :nodoc: if superclass.respond_to?(:apply_pending_attribute_modifications, true) superclass.send(:apply_pending_attribute_modifications, attributes) end pending_attribute_modifications.each do |modification| modification.apply_to(attributes) end attributes end
#attribute(name, type = nil, default: (no_default = true), **options)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_registration.rb', line 22
def attribute(name, type = nil, default: (no_default = true), **) name = resolve_attribute_name(name) type = resolve_type_name(type, **) if type.is_a?(Symbol) type = hook_attribute_type(name, type) if type add_pending_attribute_modification(PendingType.new(name, type)) if type || no_default add_pending_attribute_modification(PendingDefault.new(name, default)) unless no_default reset_default_attributes end
#attribute_types
[ GitHub ]# File 'activemodel/lib/active_model/attribute_registration.rb', line 45
def attribute_types # :nodoc: @attribute_types ||= _default_attributes.cast_types.tap do |hash| hash.default = Type.default_value ActiveSupport::Ractors.try_make_shareable(hash) end end
#decorate_attributes(names = nil, &decorator)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_registration.rb', line 33
def decorate_attributes(names = nil, &decorator) # :nodoc: names = names&.map { |name| resolve_attribute_name(name) } add_pending_attribute_modification(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.
# File 'activemodel/lib/active_model/attribute_registration.rb', line 136
def hook_attribute_type(attribute, type) type end
#inherited(base)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_registration.rb', line 17
def inherited(base) super base.instance_variable_set(:@pending_attribute_modifications, []) end
#reset_default_attributes (private)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_registration.rb', line 115
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 120
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 125
def resolve_attribute_name(name) name.to_s end
#resolve_type_name(name, **options) (private)
[ GitHub ]#type_for_attribute(attribute_name, &block)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_registration.rb', line 52
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