Sha256: 0e7b622b0165230690903cfca37ec53bde489d54887e34210bd61e6a9407e64f

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

require_relative 'stack_view_data'

module Thinreports
  module SectionReport
    module Builder
      class StackViewBuilder
        def initialize(item)
          @item = item
        end

        def update(params)
          rows_params = params[:rows] || {}
          rows_schema = item.internal.format.rows

          schema_row_ids = rows_schema.map {|row_schema| row_schema.id.to_sym}.to_set

          rows = []
          rows_schema.each do |row_schema|
            row_params = rows_params[row_schema.id.to_sym] || {}
            next unless row_enabled?(row_schema, row_params)

            items = build_row_items(
              row_schema,
              row_params[:items] || {}
            )

            rows << StackViewData::Row.new(row_schema, items, row_params[:min_height])
          end
          item.internal.rows = rows
        end

        private

        attr_reader :item

        def build_row_items(row_schema, items_params)
          row_schema.items.each_with_object([]) do |item_schema, items|
            item = ItemBuilder.new(item_schema, row_schema).build(items_params[item_schema.id&.to_sym])
            items << item if item.visible?
          end
        end

        def row_enabled?(row_schema, row_params)
          if row_params.key?(:display)
            row_params[:display]
          else
            row_schema.display?
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
thinreports-0.12.0 lib/thinreports/section_report/builder/stack_view_builder.rb