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

#initialize(entity) ⇒ OutputBuilder

Returns a new instance of OutputBuilder


8
9
10
11
12
# File 'lib/grape_entity/exposure/nesting_exposure/output_builder.rb', line 8

def initialize(entity)
  @entity = entity
  @output_hash = {}
  @output_collection = []
end

Instance Method Details

#__getobj__Object


33
34
35
# File 'lib/grape_entity/exposure/nesting_exposure/output_builder.rb', line 33

def __getobj__
  output
end

#add(exposure, result) ⇒ Object


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

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
  elsif exposure.for_merge
    # 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)
    return unless result
    @output_hash.merge! result, &merge_strategy(exposure.for_merge)
  else
    @output_hash[exposure.key(@entity)] = result
  end
end

#kind_of?(klass) ⇒ Boolean Also known as: is_a?

Returns:

  • (Boolean)

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

def kind_of?(klass)
  klass == output.class || super
end