Class: FastJsonapi::Attribute
- Inherits:
-
Object
- Object
- FastJsonapi::Attribute
- Defined in:
- lib/fast_jsonapi/attribute.rb
Instance Attribute Summary collapse
-
#conditional_proc ⇒ Object
readonly
Returns the value of attribute conditional_proc.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
Instance Method Summary collapse
- #include_attribute?(record, serialization_params) ⇒ Boolean
-
#initialize(key:, method:, options: {}) ⇒ Attribute
constructor
A new instance of Attribute.
- #serialize(record, serialization_params, output_hash) ⇒ Object
Constructor Details
#initialize(key:, method:, options: {}) ⇒ Attribute
Returns a new instance of Attribute.
5 6 7 8 9 |
# File 'lib/fast_jsonapi/attribute.rb', line 5 def initialize(key:, method:, options: {}) @key = key @method = method @conditional_proc = [:if] end |
Instance Attribute Details
#conditional_proc ⇒ Object (readonly)
Returns the value of attribute conditional_proc.
3 4 5 |
# File 'lib/fast_jsonapi/attribute.rb', line 3 def conditional_proc @conditional_proc end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
3 4 5 |
# File 'lib/fast_jsonapi/attribute.rb', line 3 def key @key end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
3 4 5 |
# File 'lib/fast_jsonapi/attribute.rb', line 3 def method @method end |
Instance Method Details
#include_attribute?(record, serialization_params) ⇒ Boolean
21 22 23 24 25 26 27 |
# File 'lib/fast_jsonapi/attribute.rb', line 21 def include_attribute?(record, serialization_params) if conditional_proc.present? conditional_proc.call(record, serialization_params) else true end end |
#serialize(record, serialization_params, output_hash) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/fast_jsonapi/attribute.rb', line 11 def serialize(record, serialization_params, output_hash) if include_attribute?(record, serialization_params) output_hash[key] = if method.is_a?(Proc) method.arity.abs == 1 ? method.call(record) : method.call(record, serialization_params) else record.public_send(method) end end end |