Sha256: d3ef2f8742042629f60bae6fa24226c05e92b34a5ef4cee288133c638c5ecd29

Contents?: true

Size: 1.56 KB

Versions: 4

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

#
# Copyright (c) 2019-present, Blue Marble Payroll, LLC
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#

module Proforma
  # A Template instance is a set of modeling objects with directions on how to compile it into
  # a Prototype object.  The prototype instance can then be rendered into an end-result such as
  # a text, pdf, or spreadsheet file.
  class Template < Prototype
    include Compiling::Compilable
    acts_as_hashable

    attr_writer :split

    def initialize(children: [], split: false, title: '')
      @split = split

      super(children: children, title: title)
    end

    def split
      @split || false
    end
    alias split? split

    def compile(data, evaluator)
      if split?
        compile_record(data, evaluator)
      else
        compile_collection(data, evaluator)
      end
    end

    private

    def compile_collection(data, evaluator)
      compiled_children = Modeling::Collection.new(children: children).compile(data, evaluator)

      [
        Prototype.new(
          children: compiled_children,
          title: evaluator.text({}, title)
        )
      ]
    end

    def compile_record(data, evaluator)
      default_grouping = Modeling::Grouping.new(children: children)

      array(data).map do |record|
        compiled_children = default_grouping.compile(record, evaluator)

        Prototype.new(
          children: compiled_children,
          title: evaluator.text(record, title)
        )
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
proforma-1.0.2 lib/proforma/template.rb
proforma-1.0.1 lib/proforma/template.rb
proforma-1.0.0 lib/proforma/template.rb
proforma-1.0.0.pre.alpha lib/proforma/template.rb