Module: TableStructure::Schema

Defined in:
lib/table_structure/schema.rb,
lib/table_structure/schema/table.rb,
lib/table_structure/schema/utils.rb,
lib/table_structure/schema/dsl/option.rb,
lib/table_structure/schema/row_builders.rb,
lib/table_structure/schema/class_methods.rb,
lib/table_structure/schema/columns/schema.rb,
lib/table_structure/schema/keys_generator.rb,
lib/table_structure/schema/dsl/row_builder.rb,
lib/table_structure/schema/context_builders.rb,
lib/table_structure/schema/column_converters.rb,
lib/table_structure/schema/columns/attributes.rb,
lib/table_structure/schema/dsl/context_builder.rb,
lib/table_structure/schema/dsl/column_converter.rb,
lib/table_structure/schema/dsl/column_definition.rb,
lib/table_structure/schema/definition/row_builder.rb,
lib/table_structure/schema/definition/columns/error.rb,
lib/table_structure/schema/definition/context_builder.rb,
lib/table_structure/schema/definition/column_converter.rb,
lib/table_structure/schema/definition/columns/compiler.rb,
lib/table_structure/schema/definition/columns/validator.rb,
lib/table_structure/schema/definition/columns/attributes.rb,
lib/table_structure/schema/definition/columns/schema_class.rb,
lib/table_structure/schema/definition/columns/schema_instance.rb

Defined Under Namespace

Modules: ClassMethods, Columns, DSL, Definition, Utils Classes: ColumnConverters, ColumnConvertible, ContextBuildable, ContextBuilders, KeysGenerator, MyDefinition, ResultBuildable, RowBuilders, Table

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_class(&block) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/table_structure/schema.rb', line 14

def self.create_class(&block)
  raise ::TableStructure::Error, 'No block given.' unless block

  schema_module = self
  Class.new do
    include schema_module
    class_eval(&block)
  end
end

.included(klass) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/table_structure/schema.rb', line 5

def self.included(klass)
  klass.extend(DSL::ColumnConverter)
  klass.extend(DSL::ColumnDefinition)
  klass.extend(DSL::ContextBuilder)
  klass.extend(DSL::Option)
  klass.extend(DSL::RowBuilder)
  klass.extend(ClassMethods)
end

Instance Method Details

#create_table(row_type: :array, **deprecated_options) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/table_structure/schema.rb', line 101

def create_table(row_type: :array, **deprecated_options)
  options = @_definition_.options.merge(deprecated_options)

  if options.key?(:result_type)
    warn '[TableStructure] `:result_type` option has been deprecated. Use `:row_type` option instead.'
    options[:row_type] = options[:result_type]
  end

  keys_generator_options = {
    prefix: options[:key_prefix],
    suffix: options[:key_suffix]
  }

  keys_generator = KeysGenerator.new(
    **keys_generator_options
  )

  table = Table.new(
    columns: @_definition_.columns,
    context: @_definition_.context,
    keys_generator: keys_generator
  )

  @_definition_
    .context_builders
    .extend_methods_for(table)

  column_converters_options = {
    name_prefix: options[:name_prefix],
    name_suffix: options[:name_suffix]
  }

  @_definition_
    .column_converters
    .extend_methods_for(table, **column_converters_options)

  row_builders_options = {
    row_type: options[:row_type] || row_type
  }

  @_definition_
    .row_builders
    .extend_methods_for(table, **row_builders_options)

  if block_given?
    yield table
  else
    table
  end
end

#initialize(name: self.class.name, context: nil, name_prefix: nil, name_suffix: nil, key_prefix: nil, key_suffix: nil, nil_definitions_ignored: false, **deprecated_options, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/table_structure/schema.rb', line 34

def initialize(
  name: self.class.name,
  context: nil,
  name_prefix: nil,
  name_suffix: nil,
  key_prefix: nil,
  key_suffix: nil,
  nil_definitions_ignored: false,
  **deprecated_options,
  &block
)
  unless deprecated_options.empty?
    caller_location = caller_locations(1, 1)
    deprecated_options.keys.each do |k|
      warn "[TableStructure] Specify :#{k} option on Writer or Iterator. #{caller_location}"
    end
  end

  options = {
    name_prefix: name_prefix,
    name_suffix: name_suffix,
    key_prefix: key_prefix,
    key_suffix: key_suffix,
    nil_definitions_ignored: nil_definitions_ignored
  }.merge!(self.class.options).merge!(deprecated_options)

  schema_classes = [self.class]

  if block_given?
    schema_classes << ::TableStructure::Schema.create_class(&block)
  end

  context_builders = ContextBuilders.new(
    schema_classes.map(&:context_builders).reduce({}, &:merge!)
  )

  column_converters = ColumnConverters.new(
    schema_classes.map(&:column_converters).reduce({}, &:merge!)
  )

  row_builders = RowBuilders.new(
    schema_classes.map(&:row_builders).reduce({}, &:merge!)
  )

  table_context = context_builders.build_for_table(context)

  columns =
    Definition::Columns::Compiler
    .new(
      name,
      schema_classes.map(&:column_definitions).reduce([], &:concat),
      options
    )
    .compile(table_context)

  @_definition_ =
    MyDefinition.new(
      name,
      columns,
      context_builders,
      column_converters,
      row_builders,
      table_context,
      options
    )
end