Class: ROM::HTTP::Dataset::ResponseTransformers::Schemad

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/http/dataset/response_transformers/schemad.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Schemad

Returns a new instance of Schemad.



10
11
12
# File 'lib/rom/http/dataset/response_transformers/schemad.rb', line 10

def initialize(schema)
  @schema = Types::Hash.schema(schema)
end

Instance Attribute Details

#schemaObject (readonly)



8
9
10
# File 'lib/rom/http/dataset/response_transformers/schemad.rb', line 8

def schema
  @schema
end

Instance Method Details

#call(response, dataset) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rom/http/dataset/response_transformers/schemad.rb', line 14

def call(response, dataset)
  projections = dataset.projections

  if projections.size > 0 && schema.member_types.keys != projections
    projected_schema = Types::Hash.schema(
      schema.member_types.select { |k, _| projections.include?(k) }
    )
    projected_schema[response]
  else
    response.map { |tuple| schema[tuple] }
  end
end