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
-
#coerce_to ⇒ Object
readonly
Returns the value of attribute coerce_to.
-
#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.
Instance Method Summary collapse
-
#initialize(key, from: nil, with: nil, to: nil, nesting: [], conditions: []) ⇒ Attribute
constructor
Initializes a new CacheCrispies::Attribute instance.
-
#value_for(model, options) ⇒ Object
Gets the value of the attribute for the given model and options.
Constructor Details
#initialize(key, from: nil, with: nil, to: nil, nesting: [], conditions: []) ⇒ Attribute
Initializes a new CacheCrispies::Attribute instance
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/cache_crispies/attribute.rb', line 23 def initialize( key, from: nil, with: nil, to: nil, nesting: [], conditions: [] ) @key = key @method_name = from || key || :itself @serializer = with @coerce_to = to @nesting = Array(nesting) @conditions = Array(conditions) end |
Instance Attribute Details
#coerce_to ⇒ Object (readonly)
Returns the value of attribute coerce_to.
35 36 37 |
# File 'lib/cache_crispies/attribute.rb', line 35 def coerce_to @coerce_to end |
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
35 36 37 |
# File 'lib/cache_crispies/attribute.rb', line 35 def conditions @conditions end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
35 36 37 |
# File 'lib/cache_crispies/attribute.rb', line 35 def key @key end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
35 36 37 |
# File 'lib/cache_crispies/attribute.rb', line 35 def method_name @method_name end |
#nesting ⇒ Object (readonly)
Returns the value of attribute nesting.
35 36 37 |
# File 'lib/cache_crispies/attribute.rb', line 35 def nesting @nesting end |
#serializer ⇒ Object (readonly)
Returns the value of attribute serializer.
35 36 37 |
# File 'lib/cache_crispies/attribute.rb', line 35 def serializer @serializer end |
Instance Method Details
#value_for(model, options) ⇒ Object
Gets the value of the attribute for the given model and options
51 52 53 54 55 |
# File 'lib/cache_crispies/attribute.rb', line 51 def value_for(model, ) value = model.public_send(method_name) serializer ? serialize(value, ) : coerce(value) end |