Class: Ecoportal::API::GraphQL::Base::Page::SectionCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ecoportal/api/graphql/base/page/section_collection.rb

Overview

Wraps an array of raw section docs as typed Section objects. Provides v2-compatible access patterns: sections.get_by_type(:content_section) sections.doc — raw doc array sections.ordered — sections in response order sections.each { |s| s.components ... }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sections_array) ⇒ SectionCollection

Returns a new instance of SectionCollection.



15
16
17
# File 'lib/ecoportal/api/graphql/base/page/section_collection.rb', line 15

def initialize(sections_array)
  @sections = Array(sections_array).map { |s| Section.new(s) }
end

Class Method Details

.from_doc(raw_array) ⇒ Object



19
20
21
# File 'lib/ecoportal/api/graphql/base/page/section_collection.rb', line 19

def self.from_doc(raw_array)
  new(Array(raw_array))
end

Instance Method Details

#componentsObject

All data fields across every section in this collection (memoized).



46
47
48
49
50
51
52
53
54
55
# File 'lib/ecoportal/api/graphql/base/page/section_collection.rb', line 46

def components
  @components ||= begin
    all_docs = @sections.flat_map do |s|
      (s.doc['dataFields'] || []) +
        (s.doc['leftDataFields']  || []) +
        (s.doc['rightDataFields'] || [])
    end
    DataField::Collection.from_doc(all_docs)
  end
end

#docObject

Raw doc array — v2 compat (scripts iterate sections.doc).



36
37
38
# File 'lib/ecoportal/api/graphql/base/page/section_collection.rb', line 36

def doc
  @sections.map(&:doc)
end

#each(&block) ⇒ Object



57
58
59
# File 'lib/ecoportal/api/graphql/base/page/section_collection.rb', line 57

def each(&block)
  @sections.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/ecoportal/api/graphql/base/page/section_collection.rb', line 61

def empty?
  @sections.empty?
end

#get_by_heading(heading) ⇒ Object

Find section by heading (case-insensitive).



31
32
33
# File 'lib/ecoportal/api/graphql/base/page/section_collection.rb', line 31

def get_by_heading(heading)
  @sections.find { |s| s.heading.to_s.downcase == heading.to_s.downcase }
end

#get_by_type(type_key) ⇒ Object

Filter by section type. Accepts symbol (:content_section / :split_section) or string ('ContentSection' / 'SplitSection'). Case-insensitive, underscore-tolerant.



25
26
27
28
# File 'lib/ecoportal/api/graphql/base/page/section_collection.rb', line 25

def get_by_type(type_key)
  target = normalise_type(type_key)
  @sections.select { |s| normalise_type(s.type) == target }
end

#lengthObject



65
66
67
# File 'lib/ecoportal/api/graphql/base/page/section_collection.rb', line 65

def length
  @sections.length
end

#orderedObject

Sections in response order (already ordered from server).



41
42
43
# File 'lib/ecoportal/api/graphql/base/page/section_collection.rb', line 41

def ordered
  @sections
end