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
Class Method Summary collapse
-
.filter_attributes ⇒ Array<#to_s, Regexp, Proc>
Specifies attributes which won’t be exposed while calling #inspect.
-
.filter_attributes=(new_filter_attributes) ⇒ Object
Configure the global default filtered attributes.
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.
Class Method Details
.filter_attributes ⇒ Array<#to_s, Regexp, Proc>
Specifies attributes which won’t be exposed while calling #inspect
global default filtered attributes
51 52 53 |
# File 'lib/active_attr/attributes.rb', line 51 def self.filter_attributes @filter_attributes ||= [] end |
.filter_attributes=(new_filter_attributes) ⇒ Object
Configure the global default filtered attributes
global default filtered attributes
61 62 63 |
# File 'lib/active_attr/attributes.rb', line 61 def self.filter_attributes=(new_filter_attributes) @filter_attributes = new_filter_attributes end |
Instance Method Details
#==(other) ⇒ true, false
Performs equality checking on the result of attributes and its type.
84 85 86 87 |
# File 'lib/active_attr/attributes.rb', line 84 def ==(other) return false unless other.instance_of? self.class attributes == other.attributes end |
#attributes ⇒ Hash{String => Object}
Returns a Hash of all attributes
97 98 99 |
# File 'lib/active_attr/attributes.rb', line 97 def attributes attributes_map { |name| send name } end |
#inspect ⇒ String
Returns the class name plus its attributes
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/active_attr/attributes.rb', line 110 def inspect inspection_filter = PARAMETER_FILTER.new(filter_attributes) original_attributes = attributes filtered_attributes = inspection_filter.filter(original_attributes) attribute_descriptions = filtered_attributes.sort.map { |key, value| inspect_value = case when original_attributes[key].nil? then nil.inspect when value == FILTERED then FILTERED else value.inspect end "#{key}: #{inspect_value}" }.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.
143 144 145 146 147 148 149 |
# File 'lib/active_attr/attributes.rb', line 143 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.
165 166 167 168 169 170 171 |
# File 'lib/active_attr/attributes.rb', line 165 def write_attribute(name, value) if respond_to? "#{name}=" send "#{name}=", value else raise UnknownAttributeError, "unknown attribute: #{name}" end end |