Module: Shamu::Attributes
- Extended by:
- ActiveSupport::Concern
- Included in:
- Shamu::Auditing::Transaction, Entities::Entity, Entities::ListScope, Entities::ListScope::ScopedPaging::PageScope, Events::Message, Features::Context, JsonApi::Rails::Pagination, Services::Request
- Defined in:
- lib/shamu/attributes.rb,
lib/shamu/attributes/equality.rb,
lib/shamu/attributes/assignment.rb,
lib/shamu/attributes/validation.rb,
lib/shamu/attributes/html_sanitation.rb,
lib/shamu/attributes/fluid_assignment.rb
Overview
Provide attributes that project data from another source (such as an external API, ActiveRecord model, cached data, etc.) providing simple transformations.
To add additional attribute functionality see
Defined Under Namespace
Modules: Assignment, Equality, FluidAssignment, HtmlSanitation, Validation
Class Method Summary collapse
-
.association(name, *args, **options, &block) ⇒ self
Define an Attributes.attribute that defines an association to another resource that also has it's own attributes.
-
.associations ⇒ Hash
Of all association Attributes.attributes defined on the class.
-
.attribute(name, *args, **options, &block) ⇒ self
Define a new attribute for the class.
-
.attributes ⇒ Hash
Of attributes and their options defined on the class.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Access an attribute using a Hash like index.
-
#assign_attributes(attributes) ⇒ self
Assign a hash of values to the matching instance variables.
-
#attribute?(name) ⇒ Boolean
(also: #key?)
Indicates if the object has an attribute with the given name.
- #initialize(*attributes) ⇒ Object
-
#set?(attribute) ⇒ Boolean
True if the attribute has been set.
-
#to_attributes(only: nil, except: nil) ⇒ Hash
Project the current state of the object to a hash of attributes that can be used to restore the attribute object at a later time.
Class Method Details
.association(name, *args, **options, &block) ⇒ self
Define an attribute that defines an association to another resource that also has it's own attributes.
195 196 197 198 199 |
# File 'lib/shamu/attributes.rb', line 195 def association( name, *args, **, &block ) [:association] = true attribute( name, *args, **, &block ) end |
.associations ⇒ Hash
Returns of all association attributes defined on the class.
143 144 145 |
# File 'lib/shamu/attributes.rb', line 143 def associations attributes.select { |_, v| v[:association] } end |
.attribute(name, on: , default: , build: , &block) ⇒ self .attribute(name, build, on: , default: , &block) ⇒ self
Define a new attribute for the class.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/shamu/attributes.rb', line 170 def attribute( name, *args, **, &block ) name = name.to_sym = create_attribute( name, *args, ** ) define_attribute_reader( name, ** ) define_attribute_assignment( name, ** ) if .key?( :on ) define_delegate_fetcher( name, [:on], [:build] ) else define_virtual_fetcher( name, [:default], &block ) end private :"fetch_#{ name }" private :"assign_#{ name }" self end |
.attributes ⇒ Hash
Returns of attributes and their options defined on the class.
138 139 140 |
# File 'lib/shamu/attributes.rb', line 138 def attributes @attributes ||= {} end |
Instance Method Details
#[](name) ⇒ Object
Access an attribute using a Hash like index.
61 62 63 |
# File 'lib/shamu/attributes.rb', line 61 def []( name ) send name if attribute?( name ) end |
#assign_attributes(attributes) ⇒ self
Assign a hash of values to the matching instance variables.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/shamu/attributes.rb', line 92 def assign_attributes( attributes ) attributes = resolve_attributes( attributes ) self.class.attributes.each do |key, | as = [ :as ] # Alias support next unless attributes.key?( key ) || ( as && attributes.key?( as ) ) value = attributes[ key ] value ||= attributes[ as ] if as if build = [:build] value = build_value( build, value ) end send :"assign_#{ key }", value end end |
#attribute?(name) ⇒ Boolean Also known as: key?
Indicates if the object has an attribute with the given name. Aliased to #key? to make the object look like a Hash.
53 54 55 |
# File 'lib/shamu/attributes.rb', line 53 def attribute?( name ) self.class.attributes.key?( name.to_sym ) end |
#initialize(*attributes) ⇒ Object
33 34 35 |
# File 'lib/shamu/attributes.rb', line 33 def initialize( *attributes ) assign_attributes( attributes.last ) end |
#set?(attribute) ⇒ Boolean
Returns true if the attribute has been set.
67 68 69 |
# File 'lib/shamu/attributes.rb', line 67 def set?( attribute ) instance_variable_defined? :"@#{ attribute }" end |
#to_attributes(only: nil, except: nil) ⇒ Hash
Project the current state of the object to a hash of attributes that can be used to restore the attribute object at a later time.
43 44 45 46 47 48 49 |
# File 'lib/shamu/attributes.rb', line 43 def to_attributes( only: nil, except: nil ) self.class.attributes.each_with_object({}) do |(name, ), attrs| next if ( only && !match_attribute?( only, name ) ) || ( except && match_attribute?( except, name ) ) next unless serialize_attribute?( name, ) attrs[name] = send( name ) end end |