class Thinreports::SectionReport::Builder::ReportBuilder

Attributes

schema[R]

Public Class Methods

new(schema) click to toggle source
# File lib/thinreports/section_report/builder/report_builder.rb, line 10
def initialize(schema)
  @schema = schema
end

Public Instance Methods

build(params) click to toggle source
# File lib/thinreports/section_report/builder/report_builder.rb, line 14
def build(params)
  ReportData::Main.new(
    schema,
    build_groups(params[:groups])
  )
end

Private Instance Methods

build_detail_sections(details_params) click to toggle source
# File lib/thinreports/section_report/builder/report_builder.rb, line 53
def build_detail_sections(details_params)
  details_params.each_with_object([]) do |detail_params, details|
    detail_id = detail_params[:id].to_sym
    detail_schema = schema.details[detail_id]

    next unless detail_schema

    items = build_items(detail_schema, detail_params[:items] || {})
    details << ReportData::Section.new(detail_schema, items, detail_params[:min_height])
  end
end
build_groups(groups_params) click to toggle source
# File lib/thinreports/section_report/builder/report_builder.rb, line 25
def build_groups(groups_params)
  return [] unless groups_params

  groups_params.map do |group_params|
    ReportData::Group.new(
      build_sections(:header, group_params[:headers] || {}),
      build_detail_sections(group_params[:details] || []),
      build_sections(:footer, group_params[:footers] || {})
    )
  end
end
build_items(section_schema, items_params) click to toggle source
# File lib/thinreports/section_report/builder/report_builder.rb, line 65
def build_items(section_schema, items_params)
  section_schema.items.each_with_object([]) do |item_schema, items|
    item = ItemBuilder.new(item_schema, section_schema).build(items_params[item_schema.id&.to_sym])
    items << item if item.visible?
  end
end
build_sections(section_type, sections_params) click to toggle source
# File lib/thinreports/section_report/builder/report_builder.rb, line 37
def build_sections(section_type, sections_params)
  sections_schemas =
    case section_type
    when :header then schema.headers
    when :footer then schema.footers
    end

  sections_schemas.each_with_object([]) do |(section_id, section_schema), sections|
    section_params = sections_params[section_id.to_sym] || {}
    next unless section_enabled?(section_schema, section_params)

    items = build_items(section_schema, section_params[:items] || {})
    sections << ReportData::Section.new(section_schema, items, section_params[:min_height])
  end
end
section_enabled?(section_schema, section_params) click to toggle source
# File lib/thinreports/section_report/builder/report_builder.rb, line 72
def section_enabled?(section_schema, section_params)
  if section_params.key?(:display)
    section_params[:display]
  else
    section_schema.display?
  end
end