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
13
14
# File 'lib/grape_entity/exposure/nesting_exposure/output_builder.rb', line 8

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

  super
end

Instance Method Details

#__getobj__Object



36
37
38
# File 'lib/grape_entity/exposure/nesting_exposure/output_builder.rb', line 36

def __getobj__
  output
end

#add(exposure, result) ⇒ Object



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

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)


31
32
33
# File 'lib/grape_entity/exposure/nesting_exposure/output_builder.rb', line 31

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