Class: TableStructure::Schema::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/table_structure/schema/definition.rb,
lib/table_structure/schema/definition/error.rb,
lib/table_structure/schema/definition/compiler.rb,
lib/table_structure/schema/definition/validator.rb

Defined Under Namespace

Classes: Compiler, Error, Validator

Constant Summary collapse

RESULT_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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, column_definitions, context_builders, column_converters, result_builders, context, options) ⇒ Definition

Returns a new instance of Definition.



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

def initialize(
  name,
  column_definitions,
  context_builders,
  column_converters,
  result_builders,
  context,
  options
)
  table_context_builder = context_builders.delete(:table)
  context = table_context_builder.call(context) if table_context_builder

  @name = name
  @columns = create_columns(name, column_definitions, context, options)
  @header_context_builder = context_builders[:header]
  @row_context_builder = context_builders[:row]
  @header_column_converters = select_column_converters(:header, column_converters)
  @row_column_converters = select_column_converters(:row, column_converters)
  @result_builders = result_builders
  @context = context
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#create_table(result_type: :array, **options) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/table_structure/schema/definition.rb', line 42

def create_table(result_type: :array, **options)
  options = @options.merge(options)

  header_column_converters =
    optional_header_column_converters(options).merge(@header_column_converters)

  result_builders = select_result_builders(result_type)

  Table.new(
    @columns,
    @header_context_builder,
    @row_context_builder,
    header_column_converters,
    @row_column_converters,
    result_builders,
    @context,
    options
  )
end