Class: LcpRuby::Dsl::NestedSectionBuilder

Inherits:
Object
  • Object
show all
Includes:
SourceLocationCapture
Defined in:
lib/lcp_ruby/dsl/presenter_builder.rb

Overview

Builder for nested_fields blocks that supports both flat fields and sub-sections. Using both ‘field` and `section` in the same block is not allowed.

Instance Method Summary collapse

Methods included from SourceLocationCapture

capture_source_loc

Constructor Details

#initializeNestedSectionBuilder

Returns a new instance of NestedSectionBuilder.



770
771
772
773
# File 'lib/lcp_ruby/dsl/presenter_builder.rb', line 770

def initialize
  @fields = []
  @sub_sections = []
end

Instance Method Details

#field(name, **options) ⇒ Object



775
776
777
778
779
780
781
782
783
784
785
# File 'lib/lcp_ruby/dsl/presenter_builder.rb', line 775

def field(name, **options)
  if @sub_sections.any?
    raise ArgumentError, "Cannot mix field and section calls in nested_fields — use one or the other"
  end

  field_hash = { "field" => name.to_s }
  options.each do |k, v|
    field_hash[k.to_s] = v.is_a?(Symbol) ? v.to_s : HashUtils.stringify_deep(v)
  end
  @fields << field_hash
end

#has_sub_sections?Boolean

Returns:

  • (Boolean)


813
814
815
# File 'lib/lcp_ruby/dsl/presenter_builder.rb', line 813

def has_sub_sections?
  @sub_sections.any?
end

#section(title = nil, key: nil, columns: nil, collapsible: false, collapsed: false, visible_when: nil, disable_when: nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
# File 'lib/lcp_ruby/dsl/presenter_builder.rb', line 787

def section(title = nil, key: nil, columns: nil, collapsible: false, collapsed: false,
            visible_when: nil, disable_when: nil, &block)
  if @fields.any?
    raise ArgumentError, "Cannot mix field and section calls in nested_fields — use one or the other"
  end
  raise ArgumentError, "section requires either a title or key:" if title.nil? && key.nil?

  ss = {}
  ss["title"] = title if title
  ss["_label_source_loc"] = capture_source_loc if title
  ss["section_key"] = key.to_s if key
  ss["columns"] = columns if columns
  ss["collapsible"] = collapsible if collapsible
  ss["collapsed"] = collapsed if collapsed
  ss["visible_when"] = HashUtils.stringify_deep(visible_when) if visible_when
  ss["disable_when"] = HashUtils.stringify_deep(disable_when) if disable_when

  if block
    builder = SectionBuilder.new
    builder.instance_eval(&block)
    ss["fields"] = builder.to_fields
  end

  @sub_sections << ss
end

#to_fieldsObject



817
818
819
# File 'lib/lcp_ruby/dsl/presenter_builder.rb', line 817

def to_fields
  @fields
end

#to_sub_sectionsObject



821
822
823
# File 'lib/lcp_ruby/dsl/presenter_builder.rb', line 821

def to_sub_sections
  @sub_sections
end