2017-10-12 14:13:45 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-19 12:45:07 -04:00
|
|
|
require "active_model/attribute_set"
|
|
|
|
require "active_model/attribute/user_provided_default"
|
2017-10-12 14:13:45 -04:00
|
|
|
|
|
|
|
module ActiveModel
|
|
|
|
module Attributes #:nodoc:
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
include ActiveModel::AttributeMethods
|
|
|
|
|
|
|
|
included do
|
|
|
|
attribute_method_suffix "="
|
|
|
|
class_attribute :attribute_types, :_default_attributes, instance_accessor: false
|
2017-10-19 12:45:07 -04:00
|
|
|
self.attribute_types = Hash.new(Type.default_value)
|
|
|
|
self._default_attributes = AttributeSet.new({})
|
2017-10-12 14:13:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
2017-10-19 12:45:07 -04:00
|
|
|
def attribute(name, type = Type::Value.new, **options)
|
|
|
|
name = name.to_s
|
|
|
|
if type.is_a?(Symbol)
|
|
|
|
type = ActiveModel::Type.lookup(type, **options.except(:default))
|
|
|
|
end
|
|
|
|
self.attribute_types = attribute_types.merge(name => type)
|
|
|
|
define_default_attribute(name, options.fetch(:default, NO_DEFAULT_PROVIDED), type)
|
2018-01-21 22:06:59 -05:00
|
|
|
define_attribute_method(name)
|
2017-10-12 14:13:45 -04:00
|
|
|
end
|
|
|
|
|
2019-04-22 19:48:17 -04:00
|
|
|
# Returns an array of attribute names as strings
|
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Attributes
|
|
|
|
#
|
|
|
|
# attribute :name, :string
|
|
|
|
# attribute :age, :integer
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Person.attribute_names
|
|
|
|
# # => ["name", "age"]
|
|
|
|
def attribute_names
|
|
|
|
attribute_types.keys
|
|
|
|
end
|
|
|
|
|
2017-10-12 14:13:45 -04:00
|
|
|
private
|
2020-05-01 06:21:16 -04:00
|
|
|
def define_method_attribute=(name, owner:)
|
2018-06-22 11:54:31 -04:00
|
|
|
ActiveModel::AttributeMethods::AttrNames.define_attribute_accessor_method(
|
2020-05-01 06:21:16 -04:00
|
|
|
owner, name, writer: true,
|
2018-06-22 11:54:31 -04:00
|
|
|
) do |temp_method_name, attr_name_expr|
|
2020-05-01 06:21:16 -04:00
|
|
|
owner <<
|
|
|
|
"def #{temp_method_name}(value)" <<
|
|
|
|
" write_attribute(#{attr_name_expr}, value)" <<
|
|
|
|
"end"
|
2018-06-22 11:54:31 -04:00
|
|
|
end
|
2017-10-12 14:13:45 -04:00
|
|
|
end
|
2017-10-19 12:45:07 -04:00
|
|
|
|
|
|
|
NO_DEFAULT_PROVIDED = Object.new # :nodoc:
|
|
|
|
private_constant :NO_DEFAULT_PROVIDED
|
|
|
|
|
|
|
|
def define_default_attribute(name, value, type)
|
|
|
|
self._default_attributes = _default_attributes.deep_dup
|
|
|
|
if value == NO_DEFAULT_PROVIDED
|
|
|
|
default_attribute = _default_attributes[name].with_type(type)
|
|
|
|
else
|
|
|
|
default_attribute = Attribute::UserProvidedDefault.new(
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
type,
|
|
|
|
_default_attributes.fetch(name.to_s) { nil },
|
|
|
|
)
|
|
|
|
end
|
|
|
|
_default_attributes[name] = default_attribute
|
|
|
|
end
|
2017-10-12 14:13:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(*)
|
2017-10-19 12:45:07 -04:00
|
|
|
@attributes = self.class._default_attributes.deep_dup
|
2017-10-12 14:13:45 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2020-05-05 11:37:35 -04:00
|
|
|
def initialize_dup(other) # :nodoc:
|
|
|
|
@attributes = @attributes.deep_dup
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2019-04-22 19:48:17 -04:00
|
|
|
# Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
|
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Attributes
|
|
|
|
#
|
|
|
|
# attribute :name, :string
|
|
|
|
# attribute :age, :integer
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# person = Person.new(name: 'Francesco', age: 22)
|
|
|
|
# person.attributes
|
|
|
|
# # => {"name"=>"Francesco", "age"=>22}
|
2018-02-07 16:14:45 -05:00
|
|
|
def attributes
|
|
|
|
@attributes.to_hash
|
|
|
|
end
|
|
|
|
|
2019-04-22 19:48:17 -04:00
|
|
|
# Returns an array of attribute names as strings
|
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Attributes
|
|
|
|
#
|
|
|
|
# attribute :name, :string
|
|
|
|
# attribute :age, :integer
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# person = Person.new
|
|
|
|
# person.attribute_names
|
|
|
|
# # => ["name", "age"]
|
|
|
|
def attribute_names
|
|
|
|
@attributes.keys
|
|
|
|
end
|
|
|
|
|
2020-01-28 21:23:41 -05:00
|
|
|
def freeze
|
|
|
|
@attributes = @attributes.clone.freeze
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2017-10-12 14:13:45 -04:00
|
|
|
private
|
|
|
|
def write_attribute(attr_name, value)
|
2019-04-22 05:12:14 -04:00
|
|
|
name = attr_name.to_s
|
|
|
|
name = self.class.attribute_aliases[name] || name
|
2017-10-12 14:13:45 -04:00
|
|
|
|
2020-06-03 19:46:46 -04:00
|
|
|
_write_attribute(name, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def _write_attribute(attr_name, value)
|
|
|
|
@attributes.write_from_user(attr_name, value)
|
2017-10-19 12:45:07 -04:00
|
|
|
value
|
2017-10-12 14:13:45 -04:00
|
|
|
end
|
2020-06-03 19:46:46 -04:00
|
|
|
alias :attribute= :_write_attribute
|
2017-10-12 14:13:45 -04:00
|
|
|
|
2020-06-03 19:46:46 -04:00
|
|
|
def read_attribute(attr_name)
|
2019-04-22 05:12:14 -04:00
|
|
|
name = attr_name.to_s
|
|
|
|
name = self.class.attribute_aliases[name] || name
|
|
|
|
|
2020-06-03 19:46:46 -04:00
|
|
|
_read_attribute(name)
|
2017-10-12 14:13:45 -04:00
|
|
|
end
|
|
|
|
|
2020-06-03 19:46:46 -04:00
|
|
|
def _read_attribute(attr_name)
|
|
|
|
@attributes.fetch_value(attr_name)
|
2017-10-12 14:13:45 -04:00
|
|
|
end
|
2020-06-03 19:46:46 -04:00
|
|
|
alias :attribute :_read_attribute
|
2017-10-12 14:13:45 -04:00
|
|
|
end
|
|
|
|
end
|