Module: Jat::Plugins::SimpleApi::ResponsePiece::InstanceMethods

Included in:
Jat::Plugins::SimpleApi::ResponsePiece
Defined in:
lib/jat/plugins/simple_api/lib/response_piece.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



15
16
17
# File 'lib/jat/plugins/simple_api/lib/response_piece.rb', line 15

def context
  @context
end

#jat_classObject (readonly)

Returns the value of attribute jat_class.



15
16
17
# File 'lib/jat/plugins/simple_api/lib/response_piece.rb', line 15

def jat_class
  @jat_class
end

#objectObject (readonly)

Returns the value of attribute object.



15
16
17
# File 'lib/jat/plugins/simple_api/lib/response_piece.rb', line 15

def object
  @object
end

Instance Method Details

#initialize(object, context) ⇒ Object



17
18
19
20
21
# File 'lib/jat/plugins/simple_api/lib/response_piece.rb', line 17

def initialize(object, context)
  @object = object
  @context = context
  @jat_class = self.class.jat_class
end

#piece(map) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jat/plugins/simple_api/lib/response_piece.rb', line 23

def piece(map)
  return unless object

  result = {}

  map.each do |key, inner_map|
    attribute = jat_class.attributes.fetch(key)
    value = attribute.value(object, context)

    result[key] =
      if attribute.relation?
        if many?(attribute, value)
          value.map { |obj| inner_piece(attribute, obj, inner_map) }
        else
          inner_piece(attribute, value, inner_map)
        end
      else
        value
      end
  end

  result
end