Class: SimpleJsonapi::Node::Data::Singular

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

Instance Attribute Summary collapse

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



10
11
12
13
14
15
# File 'lib/simple_jsonapi/node/data/singular.rb', line 10

def initialize(resource:, **options)
  super(options)

  @resource = resource
  @resource_node = build_child_node(SimpleJsonapi::Node::Resource::Full, resource: @resource) unless @resource.nil?
end

Instance Attribute Details

#resourceObject (readonly)

Returns:

  • (Object)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/simple_jsonapi/node/data/singular.rb', line 5

class Singular < SimpleJsonapi::Node::Base
  attr_reader :resource

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

    @resource = resource
    @resource_node = build_child_node(SimpleJsonapi::Node::Resource::Full, resource: @resource) unless @resource.nil?
  end

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

Instance Method Details

#as_jsonapiHash{Symbol => Hash}

Returns:

  • (Hash{Symbol => Hash})


18
19
20
21
22
23
24
# File 'lib/simple_jsonapi/node/data/singular.rb', line 18

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