class Grape::Entity::Exposure::NestingExposure::OutputBuilder

Public Class Methods

new(entity) click to toggle source
Calls superclass method
# File lib/grape_entity/exposure/nesting_exposure/output_builder.rb, line 8
def initialize(entity)
  @entity = entity
  @output_hash = {}
  @output_collection = []

  super
end

Public Instance Methods

__getobj__() click to toggle source
# File lib/grape_entity/exposure/nesting_exposure/output_builder.rb, line 36
def __getobj__
  output
end
add(exposure, result) click to toggle source
# 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
is_a?(klass)
Alias for: kind_of?
kind_of?(klass) click to toggle source
Calls superclass method
# File lib/grape_entity/exposure/nesting_exposure/output_builder.rb, line 31
def kind_of?(klass)
  klass == output.class || super
end
Also aliased as: is_a?

Private Instance Methods

merge_strategy(for_merge) click to toggle source

In case if we want to solve collisions providing lambda to :merge option

# File lib/grape_entity/exposure/nesting_exposure/output_builder.rb, line 55
def merge_strategy(for_merge)
  if for_merge.respond_to? :call
    for_merge
  else
    -> {}
  end
end
output() click to toggle source

If output_collection contains at least one element we have to represent the output as a collection

# File lib/grape_entity/exposure/nesting_exposure/output_builder.rb, line 43
def output
  if @output_collection.empty?
    output = @output_hash
  else
    output = @output_collection
    output << @output_hash unless @output_hash.empty?
    output.flatten!
  end
  output
end