Module: ActiveModel::AttributeRegistration::ClassMethods

Defined in:
lib/active_model/attribute_registration.rb

Overview

:nodoc:

Defined Under Namespace

Classes: PendingDecorator, PendingDefault, PendingType

Instance Method Summary collapse

Instance Method Details

#_default_attributesObject

:nodoc:



31
32
33
34
35
# File '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

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



12
13
14
15
16
17
18
19
20
21
# File '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_typesObject

:nodoc:



37
38
39
40
41
# File '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) ⇒ Object

:nodoc:



23
24
25
26
27
28
29
# File '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

#type_for_attribute(attribute_name, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
# File '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