Module: ActiveAttr::Attributes
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveModel::AttributeMethods
- Included in:
- AttributeDefaults, QueryAttributes, Serialization, TypecastedAttributes
- Defined in:
- lib/active_attr/attributes.rb
Overview
Attributes provides a set of class methods for defining an attributes schema and instance methods for reading and writing attributes.
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- DEPRECATED_OBJECT_METHODS =
Methods deprecated on the Object class which can be safely overridden
%w(id type)
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Performs equality checking on the result of attributes and its type.
-
#attributes ⇒ Hash{String => Object}
Returns a Hash of all attributes.
-
#inspect ⇒ String
Returns the class name plus its attributes.
-
#read_attribute(name) ⇒ Object
(also: #[])
Read a value from the model’s attributes.
-
#write_attribute(name, value) ⇒ Object
(also: #[]=)
Write a single attribute to the model’s attribute hash.
Instance Method Details
#==(other) ⇒ true, false
Performs equality checking on the result of attributes and its type.
46 47 48 49 |
# File 'lib/active_attr/attributes.rb', line 46 def ==(other) return false unless other.instance_of? self.class attributes == other.attributes end |
#attributes ⇒ Hash{String => Object}
Returns a Hash of all attributes
59 60 61 |
# File 'lib/active_attr/attributes.rb', line 59 def attributes attributes_map { |name| send name } end |
#inspect ⇒ String
Returns the class name plus its attributes
72 73 74 75 76 |
# File 'lib/active_attr/attributes.rb', line 72 def inspect attribute_descriptions = attributes.sort.map { |key, value| "#{key}: #{value.inspect}" }.join(", ") separator = " " unless attribute_descriptions.empty? "#<#{self.class.name}#{separator}#{attribute_descriptions}>" end |
#read_attribute(name) ⇒ Object Also known as: []
Read a value from the model’s attributes.
92 93 94 95 96 97 98 |
# File 'lib/active_attr/attributes.rb', line 92 def read_attribute(name) if respond_to? name send name.to_s else raise UnknownAttributeError, "unknown attribute: #{name}" end end |
#write_attribute(name, value) ⇒ Object Also known as: []=
Write a single attribute to the model’s attribute hash.
114 115 116 117 118 119 120 |
# File 'lib/active_attr/attributes.rb', line 114 def write_attribute(name, value) if respond_to? "#{name}=" send "#{name}=", value else raise UnknownAttributeError, "unknown attribute: #{name}" end end |