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::ObservedRequest, 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.
-
#as_json(options = {}) ⇒ Hash
The assigned attributes as a hash for JSON serialization.
-
#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
- #pretty_print(pp) ⇒ 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.
-
#to_json(options = {}) ⇒ String
JSON encoded version of the assigned attributes.
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.
230 231 232 233 234 |
# File 'lib/shamu/attributes.rb', line 230 def association( name, *args, **, &block ) [:association] = true attribute( name, *args, **, &block ) end |
.associations ⇒ Hash
Returns of all association attributes defined on the class.
178 179 180 |
# File 'lib/shamu/attributes.rb', line 178 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.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/shamu/attributes.rb', line 205 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.
173 174 175 |
# File 'lib/shamu/attributes.rb', line 173 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 |
#as_json(options = {}) ⇒ Hash
Returns the assigned attributes as a hash for JSON serialization.
80 81 82 |
# File 'lib/shamu/attributes.rb', line 80 def as_json( = {} ) # rubocop:disable Styles/OptionHash to_attributes( .slice( :only, :except ) ).as_json end |
#assign_attributes(attributes) ⇒ self
Assign a hash of values to the matching instance variables.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/shamu/attributes.rb', line 126 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 |
#pretty_print(pp) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/shamu/attributes.rb', line 89 def pretty_print( pp ) attributes = to_attributes pp.object_address_group( self ) do pp.seplist( attributes.keys, -> { pp.text "," } ) do |name| pp.breakable " " pp.group( 1 ) do pp.text name.to_s pp.text ":" pp.breakable " " pp.pp attributes[ name ] end end end 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 |
#to_json(options = {}) ⇒ String
Returns JSON encoded version of the assigned attributes.
85 86 87 |
# File 'lib/shamu/attributes.rb', line 85 def to_json( = {} ) # rubocop:disable Styles/OptionHash as_json( ).to_json end |