Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions lib/thinreports/section_report/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ def call(report_params)
def load_schema(report_params)
loader = Schema::Loader.new

case
when report_params[:layout_file]
loader.load_from_file(report_params[:layout_file])
when report_params[:layout_data]
loader.load_from_data(report_params[:layout_data])
if file = report_params[:layout_file]
loader.load_from_file(file)
elsif data = report_params[:layout_data]
loader.load_from_data(data)
else
raise Errors::LayoutFileNotFound
end
Expand Down
34 changes: 24 additions & 10 deletions lib/thinreports/section_report/builder/item_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,41 @@ class ItemBuilder
Context = Struct.new(:parent_schema)

def initialize(item_schema, parent_schema)
@item = Core::Shape::Interface(nil, item_schema)
@item_schema = item_schema
@parent_schema = parent_schema
end

def build(item_params)
params = build_params(item_params)

item.visible(params[:display]) if params.key?(:display)
item.value(params[:value]) if params.key?(:value)
item.styles(params[:styles]) if params.key?(:styles)

if item.internal.format.attributes['type'] == Core::Shape::StackView::TYPE_NAME
StackViewBuilder.new(item).update(params)
build_item(item_schema).tap do |item|
item.visible(params[:display]) if params.key?(:display)
item.value(params[:value]) if params.key?(:value)
item.styles(params[:styles]) if params.key?(:styles)
end

item
end

private

attr_reader :item, :parent_schema
attr_reader :item_schema, :parent_schema

def build_item(schema)
item_class =
case schema
when Schema::TextBlock
Item::TextBlock
when Schema::ImageBlock
Item::ImageBlock
when Schema::StackView
Item::StackView
when Schema::Text
Item::Text
when Schema::Basic
Item::Basic
end

item_class.new(schema)
end

def build_params(params)
return {} unless params
Expand Down
29 changes: 18 additions & 11 deletions lib/thinreports/section_report/builder/report_builder.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
# frozen_string_literal: true

require_relative 'report_data'
require_relative 'item_builder'

module Thinreports
module SectionReport
module Builder
class ReportBuilder
Root = Struct.new(:schema, :groups)

def initialize(schema)
@schema = schema
end

def build(params)
ReportData::Main.new(
schema,
build_groups(params[:groups])
)
Root.new(schema, build_groups(params[:groups]))
end

private
Expand All @@ -26,7 +24,7 @@ def build_groups(groups_params)
return [] unless groups_params

groups_params.map do |group_params|
ReportData::Group.new(
Report::Group.new(
build_sections(:header, group_params[:headers] || {}),
build_detail_sections(group_params[:details] || []),
build_sections(:footer, group_params[:footers] || {})
Expand All @@ -43,10 +41,11 @@ def build_sections(section_type, sections_params)

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])
sections << Report::Section.new(section_schema, items, **section_params.slice(:min_height))
end
end

Expand All @@ -58,14 +57,22 @@ def build_detail_sections(details_params)
next unless detail_schema

items = build_items(detail_schema, detail_params[:items] || {})
details << ReportData::Section.new(detail_schema, items, detail_params[:min_height])
details << Report::Section.new(detail_schema, items, **detail_params.slice(:min_height))
end
end

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?
section_schema.items.each_with_object([]) do |item_schema, m|
builder =
if item_schema.is_a?(Schema::StackView)
StackViewItemBuilder
else
ItemBuilder
end

item = builder.new(item_schema, section_schema).build(item_params[item_schema.id.to_sym])

m << item if item.visible?
end
end

Expand Down
13 changes: 0 additions & 13 deletions lib/thinreports/section_report/builder/report_data.rb

This file was deleted.

11 changes: 0 additions & 11 deletions lib/thinreports/section_report/builder/stack_view_data.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,40 @@
module Thinreports
module SectionReport
module Builder
class StackViewBuilder
def initialize(item)
@item = item
class StackViewItemBuilder
def initialize(item_schema, parent_schema)
@item_schema = item_schema
@parent_schema = parent_schema
end

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

rows = []
rows_schema.each do |row_schema|
item = Report::Item::StackView.new(item_schema, rows)
item.visible(item_params[:display]) if item_params.key?(:display)

item
end

private

attr_reader :item_schema

def build_rows(rows_params)
item_schema.rows.each_with_object([]) do |row_schema, m|
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])
m << Report::Item::StackViewRow.new(row_schema, items, **row_params.slice(: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])
Expand Down
15 changes: 9 additions & 6 deletions lib/thinreports/section_report/generate.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
# frozen_string_literal: true

require_relative 'build'
require_relative 'pdf/render'

module Thinreports
module SectionReport
class Generate
def initialize
@pdf = Thinreports::Generator::PDF::Document.new
end

def call(report_params, filename: nil)
report = Build.new.call(report_params)

PDF::Render.new(pdf).call!(report)
pdf = SectionReport::Pdf::Document.new(report.schema)

render(pdf, report)

filename ? pdf.render_file(filename) : pdf.render
end

private

attr_reader :pdf

def render(pdf, report)
report.groups.each do |group|
group.render(pdf)
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/thinreports/section_report/pdf/component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Thinreports
module SectionReport
module Pdf
module Component
def point(x, y)
[x, pdf.bounds.height - y]
end
end
end
end
end
37 changes: 37 additions & 0 deletions lib/thinreports/section_report/pdf/document.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require_relative 'rect'

module Thinreports
module SectionReport
module Pdf
class Document
def draw_rect(&block)
Rect.new(pdf).tap(&block).draw
end

def draw_line
end

def draw_ellipse
end

def draw_text
end

def draw_text_block
end

def draw_image
end

def draw_image_block
end

def section(height, &block)
pdf.bounding_box([0, pdf.cursor], width: pdf.bounds.width, height: height, &block)
end
end
end
end
end
20 changes: 20 additions & 0 deletions lib/thinreports/section_report/pdf/fill.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Thinreports
module SectionReport
module Pdf
module Fill
attr_accessor :fill_color

def initialize(*)
super
@fill_color = 'none'
end

def fill?
fill_color && fill_color != 'none'
end
end
end
end
end
53 changes: 53 additions & 0 deletions lib/thinreports/section_report/pdf/rect.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

require_relative 'component'
require_relative 'stroke'
require_relative 'fill'

module Thinreports
module SectionReport
module Pdf
class Rect
include Component
include Stroke
include Fill

attr_reader :pdf
attr_accessor :x, :y, :width, :height
attr_accessor :radius

def initialize(pdf)
@pdf = pdf
@x, @y = 0, 0
@width, @height = 0, 0
@radius = 0
end

def draw
pdf.fill_and_stroke do
set_stroke
set_fill

if radius.positive?
pdf.rounded_rectangle(point(x, y), width, height, radius)
else
pdf.rectangle(point(x, y), width, height)
end
end
end

def set_stroke
return unless stroke?

pdf.stroke_color(stroke_color)
pdf.line_width(stroke_width)
pdf.dash(stroke_dash_length, space: stroke_dash_space) if stroke_dash?
end

def set_fill
pdf.fill_color(fill_color) if fill?
end
end
end
end
end
Loading