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

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/grape_entity/exposure/nesting_exposure/nested_exposures.rb

Instance Method Summary collapse

Constructor Details

#initialize(exposures) ⇒ NestedExposures

Returns a new instance of NestedExposures


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

def initialize(exposures)
  @exposures = exposures
  @deep_complex_nesting = nil
end

Instance Method Details

#<<(exposure) ⇒ Object


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

def <<(exposure)
  reset_memoization!
  @exposures << exposure
end

#clearObject


30
31
32
33
# File 'lib/grape_entity/exposure/nesting_exposure/nested_exposures.rb', line 30

def clear
  reset_memoization!
  @exposures.clear
end

#deep_complex_nesting?(entity) ⇒ Boolean

Determine if we have any nesting exposures with the same name.

Returns:

  • (Boolean)

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

def deep_complex_nesting?(entity)
  if @deep_complex_nesting.nil?
    all_nesting = select(&:nesting?)
    @deep_complex_nesting =
      all_nesting
      .group_by { |exposure| exposure.key(entity) }
      .any? { |_key, exposures| exposures.length > 1 }
  else
    @deep_complex_nesting
  end
end

#delete_by(*attributes) ⇒ Object


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

def delete_by(*attributes)
  reset_memoization!
  @exposures.reject! { |e| attributes.include? e.attribute }
  @exposures
end

#find_by(attribute) ⇒ Object


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

def find_by(attribute)
  @exposures.find { |e| e.attribute == attribute }
end