Class: Sheetah::Attribute

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, type:) ⇒ Attribute

Returns a new instance of Attribute.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sheetah/attribute.rb', line 7

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

  @type =
    case type
    when Hash
      CompositeType.new(**type)
    when Array
      CompositeType.new(composite: :array, scalars: type)
    else
      ScalarType.new(type)
    end

  freeze
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



23
24
25
# File 'lib/sheetah/attribute.rb', line 23

def key
  @key
end

#typeObject (readonly)

Returns the value of attribute type.



23
24
25
# File 'lib/sheetah/attribute.rb', line 23

def type
  @type
end

Instance Method Details

#each_column(config) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sheetah/attribute.rb', line 25

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