class Grape::Entity::Exposure::NestingExposure

Attributes

nested_exposures[R]

Public Instance Methods

==(other) click to toggle source
Calls superclass method Grape::Entity::Exposure::Base#==
# File lib/grape_entity/exposure/nesting_exposure.rb, line 15
def ==(other)
  super && @nested_exposures == other.nested_exposures
end
deep_complex_nesting?() click to toggle source

if we have any nesting exposures with the same name. delegate :deep_complex_nesting?, to: :nested_exposures

# File lib/grape_entity/exposure/nesting_exposure.rb, line 69
def deep_complex_nesting?
  nested_exposures.deep_complex_nesting?
end
dup_args() click to toggle source
Calls superclass method Grape::Entity::Exposure::Base#dup_args
# File lib/grape_entity/exposure/nesting_exposure.rb, line 11
def dup_args
  [*super, @nested_exposures.map(&:dup)]
end
find_nested_exposure(attribute) click to toggle source
# File lib/grape_entity/exposure/nesting_exposure.rb, line 23
def find_nested_exposure(attribute)
  nested_exposures.find_by(attribute)
end
nesting?() click to toggle source
# File lib/grape_entity/exposure/nesting_exposure.rb, line 19
def nesting?
  true
end
serializable_value(entity, options) click to toggle source
# File lib/grape_entity/exposure/nesting_exposure.rb, line 55
def serializable_value(entity, options)
  new_options = nesting_options_for(options)
  output = OutputBuilder.new

  normalized_exposures(entity, new_options).each_with_object(output) do |exposure, out|
    exposure.with_attr_path(entity, new_options) do
      result = exposure.serializable_value(entity, new_options)
      out.add(exposure, result)
    end
  end
end
setup(nested_exposures = []) click to toggle source
# File lib/grape_entity/exposure/nesting_exposure.rb, line 7
def setup(nested_exposures = [])
  @nested_exposures = NestedExposures.new(nested_exposures)
end
valid?(entity) click to toggle source
# File lib/grape_entity/exposure/nesting_exposure.rb, line 27
def valid?(entity)
  nested_exposures.all? { |e| e.valid?(entity) }
end
valid_value_for(key, entity, options) click to toggle source
# File lib/grape_entity/exposure/nesting_exposure.rb, line 43
def valid_value_for(key, entity, options)
  new_options = nesting_options_for(options)

  result = nil
  normalized_exposures(entity, new_options).select { |e| e.key == key }.each do |exposure|
    exposure.with_attr_path(entity, new_options) do
      result = exposure.valid_value(entity, new_options)
    end
  end
  result
end
value(entity, options) click to toggle source
# File lib/grape_entity/exposure/nesting_exposure.rb, line 31
def value(entity, options)
  new_options = nesting_options_for(options)
  output = OutputBuilder.new

  normalized_exposures(entity, new_options).each_with_object(output) do |exposure, out|
    exposure.with_attr_path(entity, new_options) do
      result = exposure.value(entity, new_options)
      out.add(exposure, result)
    end
  end
end

Private Instance Methods

easy_normalized_exposures(entity, options) click to toggle source
# File lib/grape_entity/exposure/nesting_exposure.rb, line 83
def easy_normalized_exposures(entity, options)
  nested_exposures.select do |exposure|
    exposure.with_attr_path(entity, options) do
      exposure.should_expose?(entity, options)
    end
  end
end
nesting_options_for(options) click to toggle source
# File lib/grape_entity/exposure/nesting_exposure.rb, line 75
def nesting_options_for(options)
  if @key
    options.for_nesting(@key)
  else
    options
  end
end
normalized_exposures(entity, options) click to toggle source

This method 'merges' subsequent nesting exposures with the same name if it's needed

# File lib/grape_entity/exposure/nesting_exposure.rb, line 92
def normalized_exposures(entity, options)
  return easy_normalized_exposures(entity, options) unless deep_complex_nesting? # optimization

  table = nested_exposures.each_with_object({}) do |exposure, output|
    should_expose = exposure.with_attr_path(entity, options) do
      exposure.should_expose?(entity, options)
    end
    next unless should_expose
    output[exposure.key] ||= []
    output[exposure.key] << exposure
  end
  table.map do |key, exposures|
    last_exposure = exposures.last

    if last_exposure.nesting?
      # For the given key if the last candidates for exposing are nesting then combine them.
      nesting_tail = []
      exposures.reverse_each do |exposure|
        if exposure.nesting?
          nesting_tail.unshift exposure
        else
          break
        end
      end
      new_nested_exposures = nesting_tail.flat_map(&:nested_exposures)
      NestingExposure.new(key, {}, [], new_nested_exposures).tap do |new_exposure|
        new_exposure.instance_variable_set(:@deep_complex_nesting, true) if nesting_tail.any?(&:deep_complex_nesting?)
      end
    else
      last_exposure
    end
  end
end