Class: CacheCrispies::Attribute
- Inherits:
-
Object
- Object
- CacheCrispies::Attribute
- 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
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#coerce_to ⇒ Object
readonly
Returns the value of attribute coerce_to.
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
#nesting ⇒ Object
readonly
Returns the value of attribute nesting.
-
#serializer ⇒ Object
readonly
Returns the value of attribute serializer.
-
#through ⇒ Object
readonly
Returns the value of attribute through.
Instance Method Summary collapse
-
#initialize(key, from: nil, with: nil, through: nil, to: nil, collection: nil, optional: nil, nesting: [], conditions: [], &block) ⇒ Attribute
constructor
Initializes a new CacheCrispies::Attribute instance.
-
#value_for(target, options) ⇒ Object
Gets the value of the attribute for the given target object and options.
Constructor Details
#initialize(key, from: nil, with: nil, through: nil, to: nil, collection: nil, optional: nil, nesting: [], conditions: [], &block) ⇒ Attribute
Initializes a new CacheCrispies::Attribute instance
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/cache_crispies/attribute.rb', line 26 def initialize( key, from: nil, with: nil, through: nil, to: nil, collection: nil, optional: 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 @conditions << Optional.new(key) if optional end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
46 47 48 |
# File 'lib/cache_crispies/attribute.rb', line 46 def block @block end |
#coerce_to ⇒ Object (readonly)
Returns the value of attribute coerce_to.
46 47 48 |
# File 'lib/cache_crispies/attribute.rb', line 46 def coerce_to @coerce_to end |
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
46 47 48 |
# File 'lib/cache_crispies/attribute.rb', line 46 def collection @collection end |
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
46 47 48 |
# File 'lib/cache_crispies/attribute.rb', line 46 def conditions @conditions end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
46 47 48 |
# File 'lib/cache_crispies/attribute.rb', line 46 def key @key end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
46 47 48 |
# File 'lib/cache_crispies/attribute.rb', line 46 def method_name @method_name end |
#nesting ⇒ Object (readonly)
Returns the value of attribute nesting.
46 47 48 |
# File 'lib/cache_crispies/attribute.rb', line 46 def nesting @nesting end |
#serializer ⇒ Object (readonly)
Returns the value of attribute serializer.
46 47 48 |
# File 'lib/cache_crispies/attribute.rb', line 46 def serializer @serializer end |
#through ⇒ Object (readonly)
Returns the value of attribute through.
46 47 48 |
# File 'lib/cache_crispies/attribute.rb', line 46 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
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/cache_crispies/attribute.rb', line 65 def value_for(target, ) value = if block? block.call(target, ) elsif through? target.public_send(through)&.public_send(method_name) else target.public_send(method_name) end serializer ? serialize(value, ) : coerce(value) end |