Class: Grape::Entity::Exposure::NestingExposure::OutputBuilder

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/grape_entity/exposure/nesting_exposure/output_builder.rb

Instance Method Summary collapse

Constructor Details

#initializeOutputBuilder

Returns a new instance of OutputBuilder.



6
7
8
9
# File 'lib/grape_entity/exposure/nesting_exposure/output_builder.rb', line 6

def initialize
  @output_hash = {}
  @output_collection = []
end

Instance Method Details

#__getobj__Object



28
29
30
# File 'lib/grape_entity/exposure/nesting_exposure/output_builder.rb', line 28

def __getobj__
  output
end

#add(exposure, result) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/grape_entity/exposure/nesting_exposure/output_builder.rb', line 11

def add(exposure, result)
  # Save a result array in collections' array if it should be merged
  if result.is_a?(Array) && exposure.for_merge
    @output_collection << result
  else

    # If we have an array which should not be merged - save it with a key as a hash
    # If we have hash which should be merged - save it without a key (merge)
    if exposure.for_merge
      @output_hash.merge! result, &merge_strategy(exposure.for_merge)
    else
      @output_hash[exposure.key] = result
    end

  end
end