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, #key

Instance Method Summary collapse

Methods inherited from Base

#attr_path, #conditional?, #conditions_met?, #dup, #initialize, 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.



5
6
7
# File 'lib/grape_entity/exposure/nesting_exposure.rb', line 5

def nested_exposures
  @nested_exposures
end

Instance Method Details

#==(other) ⇒ Object



15
16
17
# File 'lib/grape_entity/exposure/nesting_exposure.rb', line 15

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

#deep_complex_nesting?Boolean

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

Returns:

  • (Boolean)


69
70
71
# File 'lib/grape_entity/exposure/nesting_exposure.rb', line 69

def deep_complex_nesting?
  nested_exposures.deep_complex_nesting?
end

#dup_argsObject



11
12
13
# File 'lib/grape_entity/exposure/nesting_exposure.rb', line 11

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

#find_nested_exposure(attribute) ⇒ Object



23
24
25
# File 'lib/grape_entity/exposure/nesting_exposure.rb', line 23

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

#nesting?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/grape_entity/exposure/nesting_exposure.rb', line 19

def nesting?
  true
end

#serializable_value(entity, options) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# 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 = []) ⇒ Object



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

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

#valid?(entity) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# 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) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# 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) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# 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