Module: Shamu::Attributes::Assignment

Extended by:
ActiveSupport::Concern
Included in:
Entities::ListScope, JsonApi::Rails::Pagination, Services::Request
Defined in:
lib/shamu/attributes/assignment.rb

Overview

Provide a means for defining writable attributes.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attribute(name, *args, **options, &block)

This method returns an undefined value.

Define a new attribute for the class.

Examples:


class Params
  include Shamu::Attributes
  include Shamu::Attributes::Assignment

  attribute :created_at, coerce: :to_datetime
  attribute :count, coerce: :to_i
  attribute :label, coerce: ->(value){ value.upcase.to_sym }
  attribute :tags, coerce: :to_s, array: true
end

Parameters:

  • coerce (Symbol, #call)

    name of a method on the assigned value to call, or a custom method that can parse values when assigning the attribute.

  • array (Boolean)

    true if the expected value should be an array.



68
69
70
71
72
# File 'lib/shamu/attributes/assignment.rb', line 68

def attribute( name, *args, **options, &block )
  super( name, *args, **options )
  define_attribute_assignment( name, **options )
  define_attribute_writer( name, **options )
end

Instance Method Details

#[]=(name, value) ⇒ Object

Parameters:

  • name (Symbol)

    of the attribute to assign.

  • value (Object)

    to assign.



17
18
19
# File 'lib/shamu/attributes/assignment.rb', line 17

def []=( name, value )
  send :"assign_#{ name }", value if attribute?( name )
end

#assigned?(name) ⇒ Boolean

not just present/memoized.

Returns:

  • (Boolean)

    true if the attribute as explicitly been defined -



33
34
35
# File 'lib/shamu/attributes/assignment.rb', line 33

def assigned?( name )
  assigned_attributes.include?( name )
end

#assigned_attributesArray<Symbol>

Returns the attributes that have been assigned.

Returns:

  • (Array<Symbol>)

    the attributes that have been assigned.



22
23
24
# File 'lib/shamu/attributes/assignment.rb', line 22

def assigned_attributes
  @assigned_attributes.to_a || []
end

#unassigned_attributesArray<Symbol>

Returns the attributes that have not been assigned.

Returns:

  • (Array<Symbol>)

    the attributes that have not been assigned.



27
28
29
# File 'lib/shamu/attributes/assignment.rb', line 27

def unassigned_attributes
  self.class.attributes.keys - assigned_attributes
end