Class: Sheetah::Attribute

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

Overview

The main building block of a Template.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, type:) ⇒ Attribute

Returns a new instance of Attribute.

Parameters:

  • key (String, Symbol)

    The key in the resulting Hash after processing a row.

  • type (AttributeType)

    The type of the value.



25
26
27
28
# File 'lib/sheetah/attribute.rb', line 25

def initialize(key:, type:)
  @key = key
  @type = type
end

Instance Attribute Details

#keySymbol, String (readonly)

Returns:

  • (Symbol, String)


31
32
33
# File 'lib/sheetah/attribute.rb', line 31

def key
  @key
end

#typeAttributeType (readonly)

An abstract specification of the type of a value in the resulting hash.

It will be used to produce the concrete type of a column (or a list of columns) when a TemplateConfig is applied to the Template owning the attribtue.

Returns:



39
40
41
# File 'lib/sheetah/attribute.rb', line 39

def type
  @type
end

Class Method Details

.build(key:, type:) ⇒ Attribute

A smarter version of #initialize.

  • It automatically freezes the instance before returning it.

  • It instantiates and injects a type automatically by passing the arguments to Sheetah::AttributeTypes.build.

Returns:



16
17
18
19
20
21
# File 'lib/sheetah/attribute.rb', line 16

def self.build(key:, type:)
  type = AttributeTypes.build(type)

  attribute = new(key: key, type: type)
  attribute.freeze
end

Instance Method Details

#each_column(config) ⇒ Object



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

def each_column(config)
  return enum_for(:each_column, config) unless block_given?

  compiled_type = type.compile(config.types)

  type.each_column do |index, required|
    header, header_pattern = config.header(key, index)

    yield Column.new(
      key: key,
      type: compiled_type,
      index: index,
      header: header,
      header_pattern: header_pattern,
      required: required
    )
  end
end