Class: CacheCrispies::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/cache_crispies/attribute.rb

Overview

Reperesents a single serialized attribute in a serializer. It’s generated by a call to either Base.serialize or Base.merge.

Defined Under Namespace

Classes: InvalidCoercionType

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, from: nil, with: nil, through: nil, to: nil, collection: nil, nesting: [], conditions: [], &block) ⇒ Attribute

Initializes a new CacheCrispies::Attribute instance

Parameters:

  • key (Symbol)

    the JSON key for this attribute

  • from (Symbol) (defaults to: nil)

    the method on the model to call to get the value

  • with (CacheCrispies::Base) (defaults to: nil)

    a serializer to use to serialize the

  • to (Class, Symbol) (defaults to: nil)

    the data type to coerce the value into

  • collection (Boolean) (defaults to: nil)

    force rendering as single or collection

  • nesting (Array<Symbol>) (defaults to: [])

    the JSON keys this attribute will be nested inside

  • conditions (Array<CacheCrispies::Condition>) (defaults to: [])

    the show_if condition blocks this attribute is nested inside. These will be evaluated for thruthiness and must all be true for this attribute to reneder. argument’s value



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cache_crispies/attribute.rb', line 24

def initialize(
  key,
  from: nil, with: nil, through: nil, to: nil, collection: nil,
  nesting: [], conditions: [],
  &block
)
  @key = key
  @method_name = from || key || :itself
  @serializer = with
  @through = through
  @coerce_to = to
  @collection = collection
  @nesting = Array(nesting)
  @conditions = Array(conditions)
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



41
42
43
# File 'lib/cache_crispies/attribute.rb', line 41

def block
  @block
end

#coerce_toObject (readonly)

Returns the value of attribute coerce_to.



41
42
43
# File 'lib/cache_crispies/attribute.rb', line 41

def coerce_to
  @coerce_to
end

#collectionObject (readonly)

Returns the value of attribute collection.



41
42
43
# File 'lib/cache_crispies/attribute.rb', line 41

def collection
  @collection
end

#conditionsObject (readonly)

Returns the value of attribute conditions.



41
42
43
# File 'lib/cache_crispies/attribute.rb', line 41

def conditions
  @conditions
end

#keyObject (readonly)

Returns the value of attribute key.



41
42
43
# File 'lib/cache_crispies/attribute.rb', line 41

def key
  @key
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



41
42
43
# File 'lib/cache_crispies/attribute.rb', line 41

def method_name
  @method_name
end

#nestingObject (readonly)

Returns the value of attribute nesting.



41
42
43
# File 'lib/cache_crispies/attribute.rb', line 41

def nesting
  @nesting
end

#serializerObject (readonly)

Returns the value of attribute serializer.



41
42
43
# File 'lib/cache_crispies/attribute.rb', line 41

def serializer
  @serializer
end

#throughObject (readonly)

Returns the value of attribute through.



41
42
43
# File 'lib/cache_crispies/attribute.rb', line 41

def through
  @through
end

Instance Method Details

#value_for(target, options) ⇒ Object

Gets the value of the attribute for the given target object and options

Parameters:

  • target (Object)

    typically ActiveRecord::Base, but could be anything

  • options (Hash)

    any optional values from the serializer instance

Returns:

  • the value for the attribute for the given model and options

Raises:



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cache_crispies/attribute.rb', line 60

def value_for(target, options)
  value =
    if block?
      block.call(target, options)
    elsif through?
      target.public_send(through)&.public_send(method_name)
    else
      target.public_send(method_name)
    end

  serializer ? serialize(value, options) : coerce(value)
end