Class: SimpleJsonapi::Node::RelationshipData::Singular

Inherits:
Base
  • Object
show all
Defined in:
lib/simple_jsonapi/node/relationship_data/singular.rb

Overview

Represents a relationship’s data object containing a single resource linkage object.

Instance Attribute Summary collapse

Attributes inherited from Base

#add_to_included, #relationship_definition

Attributes inherited from Base

#extras, #fields_spec, #include_spec, #root_node, #serializer, #serializer_inferrer, #sort_spec

Instance Method Summary collapse

Constructor Details

#initialize(resource:, **options) ⇒ Singular

Returns a new instance of Singular.

Parameters:

  • resource (Object)
  • options

    see Base#initialize for additional parameters



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/simple_jsonapi/node/relationship_data/singular.rb', line 12

def initialize(resource:, **options)
  super

  @resource = resource

  unless @resource.nil?
    @linkage_node = build_linkage_node(@resource)

    add_resource_to_included(@resource, @linkage_node)
  end
end

Instance Attribute Details

#resourceObject (readonly)

Returns:

  • (Object)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/simple_jsonapi/node/relationship_data/singular.rb', line 7

class Singular < Base
  attr_reader :resource

  # @param resource [Object]
  # @param options see {Node::RelationshipData::Base#initialize} for additional parameters
  def initialize(resource:, **options)
    super

    @resource = resource

    unless @resource.nil?
      @linkage_node = build_linkage_node(@resource)

      add_resource_to_included(@resource, @linkage_node)
    end
  end

  # @return [Hash{Symbol => Hash}]
  def as_jsonapi
    if resource.nil?
      { data: nil }
    else
      { data: @linkage_node.as_jsonapi }
    end
  end
end

Instance Method Details

#as_jsonapiHash{Symbol => Hash}

Returns:

  • (Hash{Symbol => Hash})


25
26
27
28
29
30
31
# File 'lib/simple_jsonapi/node/relationship_data/singular.rb', line 25

def as_jsonapi
  if resource.nil?
    { data: nil }
  else
    { data: @linkage_node.as_jsonapi }
  end
end