Class: TableStructure::Schema::ResultBuilders

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

Constant Summary collapse

DEFAULT_BUILDERS =
{
  _to_hash_: {
    callable: lambda { |values, keys, *|
      keys.map.with_index { |key, i| [key || i, values[i]] }.to_h
    },
    options: {
      enabled_result_types: [:hash]
    }
  }
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(builders) ⇒ ResultBuilders

Returns a new instance of ResultBuilders.



17
18
19
# File 'lib/table_structure/schema/result_builders.rb', line 17

def initialize(builders)
  @builders = builders
end

Instance Method Details

#extend_methods_for(table) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/table_structure/schema/result_builders.rb', line 21

def extend_methods_for(table)
  table_context = table.instance_variable_get(:@context)
  table_options = table.instance_variable_get(:@options)
  table_keys = table.send(:keys)

  builders = select_builders(table_options[:result_type])

  methods = {}
  unless builders.empty?
    methods[:header] = create_method(builders, table_keys, table_context)
    methods[:row] = create_method(builders, table_keys, table_context)
  end

  return if methods.empty?

  table.extend ResultBuilder.new(methods)
end