Class: JSONAPI::Materializer::Resource::Relationship
- Inherits:
-
Object
- Object
- JSONAPI::Materializer::Resource::Relationship
- Includes:
- ActiveModel::Model
- Defined in:
- lib/jsonapi/materializer/resource/relationship.rb
Instance Attribute Summary collapse
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#related ⇒ Object
Returns the value of attribute related.
Instance Method Summary collapse
- #as_json ⇒ Object
- #data ⇒ Object
-
#initialize(**keyword_arguments) ⇒ Relationship
constructor
A new instance of Relationship.
- #links_related ⇒ Object
-
#related_parent_materializer ⇒ Object
rubocop:enable Metrics/AbcSize.
Constructor Details
#initialize(**keyword_arguments) ⇒ Relationship
Returns a new instance of Relationship.
15 16 17 18 19 |
# File 'lib/jsonapi/materializer/resource/relationship.rb', line 15 def initialize(**keyword_arguments) super(**keyword_arguments) validate! end |
Instance Attribute Details
#parent ⇒ Object
Returns the value of attribute parent.
10 11 12 |
# File 'lib/jsonapi/materializer/resource/relationship.rb', line 10 def parent @parent end |
#related ⇒ Object
Returns the value of attribute related.
9 10 11 |
# File 'lib/jsonapi/materializer/resource/relationship.rb', line 9 def @related end |
Instance Method Details
#as_json ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/jsonapi/materializer/resource/relationship.rb', line 21 def as_json(*) { data:, links: { self: links_self, related: } }.transform_values(&:presence).compact end |
#data ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/jsonapi/materializer/resource/relationship.rb', line 43 def data return if .blank? @data ||= if .many? .map do |child| { id: child.attribute("id").for(child).to_s, type: child.type.to_s } end else { id: .attribute("id").for().to_s, type: .type.to_s } end end |
#links_related ⇒ Object
37 38 39 40 41 |
# File 'lib/jsonapi/materializer/resource/relationship.rb', line 37 def Addressable::Template.new( "#{parent.links_self}/#{.name}" ).pattern end |
#related_parent_materializer ⇒ Object
rubocop:enable Metrics/AbcSize
62 63 64 |
# File 'lib/jsonapi/materializer/resource/relationship.rb', line 62 def .for(parent) end |