Module: TableStructure::Schema

Defined in:
lib/table_structure/schema.rb,
lib/table_structure/schema/utils.rb,
lib/table_structure/schema/dsl/option.rb,
lib/table_structure/schema/row_builder.rb,
lib/table_structure/schema/class_methods.rb,
lib/table_structure/schema/key_converter.rb,
lib/table_structure/schema/columns/schema.rb,
lib/table_structure/schema/dsl/row_builder.rb,
lib/table_structure/schema/column_converter.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, ColumnConverter, Columns, DSL, Definition, RowBuilder, Utils Classes: KeyConverter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#column_converters ⇒ Object (readonly)

Returns the value of attribute column_converters.



24
25
26
# File 'lib/table_structure/schema.rb', line 24

def column_converters
  @column_converters
end

#columns ⇒ Object (readonly)

Returns the value of attribute columns.



24
25
26
# File 'lib/table_structure/schema.rb', line 24

def columns
  @columns
end

#context ⇒ Object (readonly)

Returns the value of attribute context.



24
25
26
# File 'lib/table_structure/schema.rb', line 24

def context
  @context
end

#context_builders ⇒ Object (readonly)

Returns the value of attribute context_builders.



24
25
26
# File 'lib/table_structure/schema.rb', line 24

def context_builders
  @context_builders
end

#key_converter ⇒ Object (readonly)

Returns the value of attribute key_converter.



24
25
26
# File 'lib/table_structure/schema.rb', line 24

def key_converter
  @key_converter
end

#row_builders ⇒ Object (readonly)

Returns the value of attribute row_builders.



24
25
26
# File 'lib/table_structure/schema.rb', line 24

def row_builders
  @row_builders
end

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

#contain_callable?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/table_structure/schema.rb', line 127

def contain_callable?(attribute)
  @columns.any? { |column| column.contain_callable?(attribute) }
end

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



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/table_structure/schema.rb', line 114

def create_table(row_type: :array, **deprecated_options, &block)
  warn '[TableStructure] `TableStructure::Schema#create_table` has been deprecated. Use `TableStructure::Table.new` instead.'

  options = @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

  ::TableStructure::Table.new(self, row_type: options[:row_type] || row_type, &block)
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



31
32
33
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
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/table_structure/schema.rb', line 31

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
)
  if deprecated_options.key?(:row_type)
    raise ::TableStructure::Error, 'Use :row_type option with Table, Writer or Iterator.'
  end

  if deprecated_options.key?(:result_type)
    raise ::TableStructure::Error, ':result_type option has been deprecated. Use :row_type option instead.'
  end

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

  schema_classes = [self.class]

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

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

  table_context_builder = @context_builders.delete(:table)

  @context = table_context_builder ? table_context_builder.call(context) : context

  @column_converters =
    schema_classes
    .map(&:column_converters)
    .reduce({}, &:merge!)
    .merge(
      ColumnConverter.create_optional_converters(
        name_prefix: options.delete(:name_prefix),
        name_suffix: options.delete(:name_suffix)
      )
    )

  @key_converter = KeyConverter.new(
    prefix: options.delete(:key_prefix),
    suffix: options.delete(:key_suffix)
  )

  @row_builders =
    RowBuilder.prepend_default_builders(
      schema_classes
        .map(&:row_builders)
        .reduce({}, &:merge!)
    )

  @columns =
    Definition::Columns::Compiler
    .new(
      name,
      schema_classes.map(&:column_definitions).reduce([], &:concat),
      { nil_definitions_ignored: options.delete(:nil_definitions_ignored) }
    )
    .compile(@context)

  @options = options
end