Class: TableStructure::Schema::RowBuilders

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

Constant Summary collapse

DEFAULT_BUILDERS =
{
  _to_hash_: ::TableStructure::Schema::Definition::RowBuilder.new(
    lambda { |values, keys, *|
      keys.map.with_index { |key, i| [key || i, values[i]] }.to_h
    },
    enabled_row_types: [:hash]
  )
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(builders) ⇒ RowBuilders

Returns a new instance of RowBuilders.



15
16
17
# File 'lib/table_structure/schema/row_builders.rb', line 15

def initialize(builders)
  @builders = builders
end

Instance Method Details

#extend_methods_for(table, row_type: :array) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/table_structure/schema/row_builders.rb', line 19

def extend_methods_for(table, row_type: :array)
  builders =
    DEFAULT_BUILDERS
    .merge(@builders)
    .select { |_k, v| v.enabled?(row_type) }

  return if builders.empty?

  table_context = table.instance_variable_get(:@context)
  table_keys = table.send(:keys)

  table.extend ResultBuildable.new(
    header: create_method(builders, table_keys, table_context),
    data: create_method(builders, table_keys, table_context)
  )
end