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 17
def ==(other)
  super && @nested_exposures == other.nested_exposures
end
deep_complex_nesting?(entity) click to toggle source

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

# File lib/grape_entity/exposure/nesting_exposure.rb, line 59
def deep_complex_nesting?(entity)
  nested_exposures.deep_complex_nesting?(entity)
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 13
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 25
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 21
def nesting?
  true
end
serializable_value(entity, options) click to toggle source
# File lib/grape_entity/exposure/nesting_exposure.rb, line 39
def serializable_value(entity, options)
  map_entity_exposures(entity, options) do |exposure, nested_options|
    exposure.serializable_value(entity, nested_options)
  end
end
setup(nested_exposures = []) click to toggle source
# File lib/grape_entity/exposure/nesting_exposure.rb, line 9
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 29
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 45
def valid_value_for(key, entity, options)
  new_options = nesting_options_for(options)

  key_exposures = normalized_exposures(entity, new_options).select { |e| e.key(entity) == key }

  key_exposures.map do |exposure|
    exposure.with_attr_path(entity, new_options) do
      exposure.valid_value(entity, new_options)
    end
  end.last
end
value(entity, options) click to toggle source
# File lib/grape_entity/exposure/nesting_exposure.rb, line 33
def value(entity, options)
  map_entity_exposures(entity, options) do |exposure, nested_options|
    exposure.value(entity, nested_options)
  end
end

Private Instance Methods

easy_normalized_exposures(entity, options) click to toggle source
# File lib/grape_entity/exposure/nesting_exposure.rb, line 73
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
map_entity_exposures(entity, options) { |exposure, new_options| ... } click to toggle source
# File lib/grape_entity/exposure/nesting_exposure.rb, line 116
def map_entity_exposures(entity, options)
  new_options = nesting_options_for(options)
  output = OutputBuilder.new(entity)

  normalized_exposures(entity, new_options).each_with_object(output) do |exposure, out|
    exposure.with_attr_path(entity, new_options) do
      result = yield(exposure, new_options)
      out.add(exposure, result)
    end
  end
end
nesting_options_for(options) click to toggle source
# File lib/grape_entity/exposure/nesting_exposure.rb, line 65
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 82
def normalized_exposures(entity, options)
  return easy_normalized_exposures(entity, options) unless deep_complex_nesting?(entity) # 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(entity)] ||= []
    output[exposure.key(entity)] << 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|
        nesting_tail.unshift exposure if exposure.nesting?
      end
      new_nested_exposures = nesting_tail.flat_map(&:nested_exposures)
      NestingExposure.new(key, {}, [], new_nested_exposures).tap do |new_exposure|
        if nesting_tail.any? { |exposure| exposure.deep_complex_nesting?(entity) }
          new_exposure.instance_variable_set(:@deep_complex_nesting, true)
        end
      end
    else
      last_exposure
    end
  end
end