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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Template.



37
38
39
40
41
42
# File 'lib/sheetah/template.rb', line 37

def initialize(attributes:, ignore_unspecified_columns: false)
  ensure_attributes_unicity(attributes)

  @attributes = attributes
  @ignore_unspecified_columns = ignore_unspecified_columns
end

Class Method Details

.build(attributes:, **kwargs) ⇒ Object



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

def self.build(attributes:, **kwargs)
  attributes = attributes.map { |attribute| Attribute.build(**attribute) }
  attributes.freeze

  template = new(attributes: attributes, **kwargs)
  template.freeze
end

Instance Method Details

#==(other) ⇒ Object



61
62
63
64
65
# File 'lib/sheetah/template.rb', line 61

def ==(other)
  other.is_a?(self.class) &&
    attributes == other.attributes &&
    ignore_unspecified_columns == other.ignore_unspecified_columns
end

#apply(config) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sheetah/template.rb', line 44

def apply(config)
  columns = []

  attributes.each do |attribute|
    attribute.each_column(config) do |column|
      columns << column.freeze
    end
  end

  specification = Specification.new(
    columns: columns.freeze,
    ignore_unspecified_columns: ignore_unspecified_columns
  )

  specification.freeze
end