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

Instance Method Summary collapse

Methods inherited from Base

#attr_path, #conditional?, #conditions_met?, #dup, #initialize, #key, new, #override?, #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)


59
60
61
# File 'lib/grape_entity/exposure/nesting_exposure.rb', line 59

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



39
40
41
42
43
# 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 = []) ⇒ 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)

  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) ⇒ Object



33
34
35
36
37
# 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