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/column/attrs.rb,
lib/table_structure/schema/class_methods.rb,
lib/table_structure/schema/column/schema.rb,
lib/table_structure/schema/column/factory.rb,
lib/table_structure/schema/result_builders.rb,
lib/table_structure/schema/context_builders.rb,
lib/table_structure/schema/column_converters.rb,
lib/table_structure/schema/dsl/result_builder.rb,
lib/table_structure/schema/dsl/context_builder.rb,
lib/table_structure/schema/table/key_decorator.rb,
lib/table_structure/schema/dsl/column_converter.rb,
lib/table_structure/schema/dsl/column_definition.rb,
lib/table_structure/schema/column/definition/error.rb,
lib/table_structure/schema/column/definition/compiler.rb,
lib/table_structure/schema/column/definition/validator.rb

Defined Under Namespace

Modules: ClassMethods, Column, DSL, Utils Classes: ColumnConverter, ColumnConverters, ContextBuilder, ContextBuilders, Definition, ResultBuilder, ResultBuilders, Table

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_class(&block) ⇒ Object

Raises:



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::ResultBuilder)
  klass.extend(ClassMethods)
end

Instance Method Details

#create_table(**options) ⇒ Object

TODO: Specify options using keyword arguments.



99
100
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
# File 'lib/table_structure/schema.rb', line 99

def create_table(**options)
  options = @_definition_.options.merge(options)

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

  @_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)

  result_builders_options = {
    result_type: options[:result_type]
  }

  @_definition_.result_builders.extend_methods_for(table, **result_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



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
# File 'lib/table_structure/schema.rb', line 35

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 the writer or the 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!)
  )

  result_builders = ResultBuilders.new(
    schema_classes.map(&:result_builders).reduce({}, &:merge!)
  )

  columns = Column::Factory.create(
    name,
    schema_classes.map(&:column_definitions).reduce([], &:concat),
    context_builders.build_for_table(context),
    options
  )

  @_definition_ =
    Definition.new(
      name,
      columns,
      context_builders,
      column_converters,
      result_builders,
      context,
      options
    )
end