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



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

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

#clearObject



34
35
36
37
# File 'lib/grape_entity/exposure/nesting_exposure/nested_exposures.rb', line 34

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)


62
63
64
65
66
67
68
69
70
71
72
# File 'lib/grape_entity/exposure/nesting_exposure/nested_exposures.rb', line 62

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



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

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

#select_by(attribute) ⇒ Object



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

def select_by(attribute)
  @exposures.select { |e| e.attribute == attribute }
end