Class: Sheetah::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/sheetah/template.rb

Overview

A Template represents the abstract structure of a tabular document.

The main component of the structure is the object obtained by processing a row. A template therefore specifies all possible attributes of that object as a list of (key, abstract type) pairs.

Each attribute will eventually be compiled into as many concrete columns as necessary with the help of a config to produce a specification.

In other words, a Template specifies the structure of the processing result (its attributes), whereas a Specification specifies the columns that may be involved into building the processing result.

Attributes may either be composite (their value is a composition of multiple values) or scalar (their value is a single value). Scalar attributes will thus produce a single column in the specification, and composite attributes will produce as many columns as required by the number of scalar values they hold.

Instance Method Summary collapse

Constructor Details

#initialize(attributes:, ignore_unspecified_columns: false) ⇒ Template

Returns a new instance of Template.



29
30
31
32
# File 'lib/sheetah/template.rb', line 29

def initialize(attributes:, ignore_unspecified_columns: false)
  @attributes = build_attributes(attributes)
  @ignore_unspecified_columns = ignore_unspecified_columns
end

Instance Method Details

#apply(config) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sheetah/template.rb', line 34

def apply(config)
  specification = Specification.new(ignore_unspecified_columns: @ignore_unspecified_columns)

  @attributes.each do |attribute|
    attribute.each_column(config) do |column|
      specification.set(column.header_pattern, column)
    end
  end

  specification.freeze
end