Class: TableStructure::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/table_structure/table.rb

Defined Under Namespace

Classes: ColumnConverter, ContextBuilder, Iterator, RowBuilder

Instance Method Summary collapse

Constructor Details

#initialize(schema, row_type: :array) {|_self| ... } ⇒ Table

Returns a new instance of Table.

Yields:

  • (_self)

Yield Parameters:



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/table_structure/table.rb', line 5

def initialize(schema, row_type: :array)
  @columns = schema.columns
  @context = schema.context
  @key_converter = schema.key_converter

  ContextBuilder.create_module(
    schema.context_builders,
    apply_to_name: schema.contain_callable?(:name),
    apply_to_value: schema.contain_callable?(:value),
    context: schema.context
  ) { |mod| extend mod }

  ColumnConverter.create_module(
    schema.column_converters,
    context: schema.context
  ) { |mod| extend mod }

  RowBuilder.create_module(
    schema.row_builders,
    row_type: row_type,
    keys: keys,
    context: schema.context
  ) { |mod| extend mod }

  yield self if block_given?
end

Instance Method Details

#body(items) ⇒ Object



36
37
38
39
40
# File 'lib/table_structure/table.rb', line 36

def body(items)
  Enumerator.new do |y|
    items.each { |item| y << data(context: item) }
  end
end

#header(context: nil) ⇒ Object



32
33
34
# File 'lib/table_structure/table.rb', line 32

def header(context: nil)
  row_values(:names, context)
end