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 =
{
  hash: lambda { |values, keys, *|
    keys.map.with_index { |key, i| [key || i, values[i]] }.to_h
  }
}.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.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/table_structure/schema/definition.rb', line 14

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 = Table::ContextBuilder.new(:header, context_builders[:header])
  @row_context_builder = Table::ContextBuilder.new(:row, context_builders[:row])
  @header_converters = select_column_converters(:header, column_converters)
  @row_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.



12
13
14
# File 'lib/table_structure/schema/definition.rb', line 12

def options
  @options
end

Instance Method Details

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



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

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

  header_converters =
    optional_header_converters(options).merge(@header_converters)

  result_builders =
    RESULT_BUILDERS
    .select { |k, _v| k == result_type }
    .merge(@result_builders)

  Table.new(
    @columns,
    @header_context_builder,
    @row_context_builder,
    header_converters,
    @row_converters,
    result_builders,
    @context,
    options
  )
end