Class: Grape::Entity::Exposure::NestingExposure

Inherits:
Base
  • Object
show all
Defined in:
lib/grape_entity/exposure/nesting_exposure.rb,
lib/grape_entity/exposure/nesting_exposure/output_builder.rb,
lib/grape_entity/exposure/nesting_exposure/nested_exposures.rb

Defined Under Namespace

Classes: NestedExposures, OutputBuilder

Instance Attribute Summary collapse

Attributes inherited from Base

#attribute, #conditions, #documentation, #for_merge, #is_safe

Instance Method Summary collapse

Methods inherited from Base

#attr_path, #conditional?, #conditions_met?, #dup, #initialize, #key, new, #should_expose?, #should_return_key?, #valid_value, #with_attr_path

Constructor Details

This class inherits a constructor from Grape::Entity::Exposure::Base

Instance Attribute Details

#nested_exposuresObject (readonly)

Returns the value of attribute nested_exposures


7
8
9
# File 'lib/grape_entity/exposure/nesting_exposure.rb', line 7

def nested_exposures
  @nested_exposures
end

Instance Method Details

#==(other) ⇒ Object


17
18
19
# File 'lib/grape_entity/exposure/nesting_exposure.rb', line 17

def ==(other)
  super && @nested_exposures == other.nested_exposures
end

#deep_complex_nesting?(entity) ⇒ Boolean

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

Returns:

  • (Boolean)

71
72
73
# File 'lib/grape_entity/exposure/nesting_exposure.rb', line 71

def deep_complex_nesting?(entity)
  nested_exposures.deep_complex_nesting?(entity)
end

#dup_argsObject


13
14
15
# File 'lib/grape_entity/exposure/nesting_exposure.rb', line 13

def dup_args
  [*super, @nested_exposures.map(&:dup)]
end

#find_nested_exposure(attribute) ⇒ Object


25
26
27
# File 'lib/grape_entity/exposure/nesting_exposure.rb', line 25

def find_nested_exposure(attribute)
  nested_exposures.find_by(attribute)
end

#nesting?Boolean

Returns:

  • (Boolean)

21
22
23
# File 'lib/grape_entity/exposure/nesting_exposure.rb', line 21

def nesting?
  true
end

#serializable_value(entity, options) ⇒ Object


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/grape_entity/exposure/nesting_exposure.rb', line 57

def serializable_value(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 = exposure.serializable_value(entity, new_options)
      out.add(exposure, result)
    end
  end
end

#setup(nested_exposures = []) ⇒ Object


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

def setup(nested_exposures = [])
  @nested_exposures = NestedExposures.new(nested_exposures)
end

#valid?(entity) ⇒ Boolean

Returns:

  • (Boolean)

29
30
31
# 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) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/grape_entity/exposure/nesting_exposure.rb', line 45

def valid_value_for(key, entity, options)
  new_options = nesting_options_for(options)

  result = nil
  normalized_exposures(entity, new_options).select { |e| e.key(entity) == 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) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/grape_entity/exposure/nesting_exposure.rb', line 33

def value(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 = exposure.value(entity, new_options)
      out.add(exposure, result)
    end
  end
end