Module: Shamu::Attributes
- Extended by:
- ActiveSupport::Concern
- Included in:
- 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/camel_case.rb,
lib/shamu/attributes/validation.rb,
lib/shamu/attributes/validators.rb,
lib/shamu/attributes/html_sanitation.rb,
lib/shamu/attributes/fluid_assignment.rb,
lib/shamu/attributes/validators/valid_validator.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, CamelCase, Equality, FluidAssignment, HtmlSanitation, Validation, Validators
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.
-
#slice(*names) ⇒ Hash
A hash with the keys for each of the given names.
-
#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.
204 205 206 207 208 |
# File 'lib/shamu/attributes.rb', line 204 def association( name, *args, **, &block ) [:association] = true attribute( name, *args, **, &block ) end |
.associations ⇒ Hash
Returns of all association attributes defined on the class.
152 153 154 |
# File 'lib/shamu/attributes.rb', line 152 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.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/shamu/attributes.rb', line 179 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.
147 148 149 |
# File 'lib/shamu/attributes.rb', line 147 def attributes @attributes ||= {} end |
Instance Method Details
#[](name) ⇒ Object
Access an attribute using a Hash like index.
69 70 71 |
# File 'lib/shamu/attributes.rb', line 69 def []( name ) send name if attribute?( name ) end |
#assign_attributes(attributes) ⇒ self
Assign a hash of values to the matching instance variables.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/shamu/attributes.rb', line 100 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.
61 62 63 |
# File 'lib/shamu/attributes.rb', line 61 def attribute?( name ) self.class.attributes.key?( name.to_sym ) end |
#initialize(*attributes) ⇒ Object
35 36 37 |
# File 'lib/shamu/attributes.rb', line 35 def initialize( *attributes ) assign_attributes( attributes.last ) end |
#set?(attribute) ⇒ Boolean
Returns true if the attribute has been set.
75 76 77 |
# File 'lib/shamu/attributes.rb', line 75 def set?( attribute ) instance_variable_defined? :"@#{ attribute }" end |
#slice(*names) ⇒ Hash
Returns a hash with the keys for each of the given names.
55 56 57 |
# File 'lib/shamu/attributes.rb', line 55 def slice( *names ) to_attributes only: names 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.
45 46 47 48 49 50 51 52 |
# File 'lib/shamu/attributes.rb', line 45 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 |