table_structure 0.3.22 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -1
  3. data/CHANGELOG.md +33 -0
  4. data/Gemfile.lock +7 -7
  5. data/README.md +30 -31
  6. data/lib/table_structure.rb +6 -12
  7. data/lib/table_structure/csv/writer.rb +4 -41
  8. data/lib/table_structure/iterator.rb +48 -89
  9. data/lib/table_structure/schema.rb +63 -76
  10. data/lib/table_structure/schema/class_methods.rb +9 -28
  11. data/lib/table_structure/schema/column_builder_factory.rb +75 -0
  12. data/lib/table_structure/schema/columns/attributes.rb +14 -9
  13. data/lib/table_structure/schema/columns/schema.rb +14 -9
  14. data/lib/table_structure/schema/composite_class.rb +40 -0
  15. data/lib/table_structure/schema/definition/columns/compiler.rb +7 -3
  16. data/lib/table_structure/schema/definition/columns/validator.rb +2 -6
  17. data/lib/table_structure/schema/dsl/column_builder.rb +29 -0
  18. data/lib/table_structure/schema/dsl/column_definition.rb +12 -2
  19. data/lib/table_structure/schema/dsl/context_builder.rb +2 -11
  20. data/lib/table_structure/schema/dsl/row_builder.rb +2 -9
  21. data/lib/table_structure/schema/{key_converter.rb → keys_builder.rb} +2 -2
  22. data/lib/table_structure/schema/row_context_builder_factory.rb +26 -0
  23. data/lib/table_structure/table.rb +31 -56
  24. data/lib/table_structure/utils.rb +40 -0
  25. data/lib/table_structure/version.rb +1 -1
  26. data/lib/table_structure/writer.rb +7 -54
  27. metadata +8 -14
  28. data/lib/table_structure/schema/column_converter.rb +0 -46
  29. data/lib/table_structure/schema/definition/column_converter.rb +0 -31
  30. data/lib/table_structure/schema/definition/context_builder.rb +0 -17
  31. data/lib/table_structure/schema/definition/row_builder.rb +0 -25
  32. data/lib/table_structure/schema/dsl/column_converter.rb +0 -41
  33. data/lib/table_structure/schema/dsl/option.rb +0 -19
  34. data/lib/table_structure/schema/row_builder.rb +0 -21
  35. data/lib/table_structure/table/column_converter.rb +0 -46
  36. data/lib/table_structure/table/context_builder.rb +0 -49
  37. data/lib/table_structure/table/iterator.rb +0 -22
  38. data/lib/table_structure/table/row_builder.rb +0 -38
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module TableStructure
4
- module Schema
5
- module ColumnConverter
6
- class << self
7
- def create_optional_converters(
8
- name_prefix: nil,
9
- name_suffix: nil
10
- )
11
- column_converters = {}
12
-
13
- if name_prefix
14
- column_converters[:_name_prepender_] =
15
- create_prepender(name_prefix, header: true, body: false)
16
- end
17
-
18
- if name_suffix
19
- column_converters[:_name_appender_] =
20
- create_appender(name_suffix, header: true, body: false)
21
- end
22
-
23
- column_converters
24
- end
25
-
26
- private
27
-
28
- def create_prepender(string, **options)
29
- Definition::ColumnConverter.new(
30
- **options
31
- ) do |val, *|
32
- val.nil? ? val : "#{string}#{val}"
33
- end
34
- end
35
-
36
- def create_appender(string, **options)
37
- Definition::ColumnConverter.new(
38
- **options
39
- ) do |val, *|
40
- val.nil? ? val : "#{val}#{string}"
41
- end
42
- end
43
- end
44
- end
45
- end
46
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module TableStructure
4
- module Schema
5
- module Definition
6
- class ColumnConverter
7
- extend Forwardable
8
-
9
- def_delegator :@callable, :call
10
-
11
- def initialize(
12
- header: true,
13
- body: true,
14
- &block
15
- )
16
- @applicable_to_header = header
17
- @applicable_to_body = body
18
- @callable = block
19
- end
20
-
21
- def applicable_to_header?
22
- !!@applicable_to_header
23
- end
24
-
25
- def applicable_to_body?
26
- !!@applicable_to_body
27
- end
28
- end
29
- end
30
- end
31
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module TableStructure
4
- module Schema
5
- module Definition
6
- class ContextBuilder
7
- extend Forwardable
8
-
9
- def_delegator :@callable, :call
10
-
11
- def initialize(&block)
12
- @callable = block
13
- end
14
- end
15
- end
16
- end
17
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module TableStructure
4
- module Schema
5
- module Definition
6
- class RowBuilder
7
- extend Forwardable
8
-
9
- def_delegator :@callable, :call
10
-
11
- def initialize(
12
- enabled_row_types: %i[array hash],
13
- &block
14
- )
15
- @enabled_row_types = [enabled_row_types].flatten
16
- @callable = block
17
- end
18
-
19
- def enabled?(row_type)
20
- @enabled_row_types.include?(row_type)
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module TableStructure
4
- module Schema
5
- module DSL
6
- module ColumnConverter
7
- def column_converter(
8
- name,
9
- callable = nil,
10
- header: true,
11
- body: true,
12
- **deprecated_options,
13
- &block
14
- )
15
- if deprecated_options.key?(:row)
16
- warn '[TableStructure] `:row` option has been deprecated. Use `:body` option instead.'
17
- body = deprecated_options[:row]
18
- end
19
-
20
- if callable
21
- warn "[TableStructure] Use `block` instead of #{callable}."
22
- end
23
-
24
- block ||= callable
25
-
26
- column_converters[name] =
27
- ::TableStructure::Schema::Definition::ColumnConverter.new(
28
- header: header,
29
- body: body,
30
- &block
31
- )
32
- nil
33
- end
34
-
35
- def column_converters
36
- @__column_converters__ ||= {}
37
- end
38
- end
39
- end
40
- end
41
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module TableStructure
4
- module Schema
5
- module DSL
6
- module Option
7
- def option(name, value)
8
- warn "[TableStructure] The use of `option` DSL has been deprecated. #{caller_locations(1, 1)}"
9
- options[name] = value
10
- nil
11
- end
12
-
13
- def options
14
- @__options__ ||= {}
15
- end
16
- end
17
- end
18
- end
19
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module TableStructure
4
- module Schema
5
- module RowBuilder
6
- DEFAULT_ROW_BUILDERS = {
7
- _to_hash_: Definition::RowBuilder.new(
8
- enabled_row_types: [:hash]
9
- ) do |values, keys, *|
10
- keys.map.with_index { |key, i| [key || i, values[i]] }.to_h
11
- end
12
- }.freeze
13
-
14
- class << self
15
- def prepend_default_builders(builders)
16
- DEFAULT_ROW_BUILDERS.merge(builders)
17
- end
18
- end
19
- end
20
- end
21
- end
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module TableStructure
4
- class Table::ColumnConverter
5
- class ColumnConvertible < Module
6
- def initialize(methods)
7
- methods.each do |name, method|
8
- define_method(name, &method)
9
- end
10
- end
11
- end
12
-
13
- class << self
14
- def create_module(converters, context:)
15
- return if converters.empty?
16
-
17
- header_converters = converters.select { |_k, v| v.applicable_to_header? }
18
- body_converters = converters.select { |_k, v| v.applicable_to_body? }
19
-
20
- methods = {}
21
-
22
- unless header_converters.empty?
23
- methods[:header] = create_method(header_converters, context)
24
- end
25
-
26
- unless body_converters.empty?
27
- methods[:data] = create_method(body_converters, context)
28
- end
29
-
30
- yield ColumnConvertible.new(methods)
31
- end
32
-
33
- private
34
-
35
- def create_method(converters, table_context)
36
- proc do |context: nil|
37
- super(context: context).map do |val|
38
- converters.reduce(val) do |val, (_, converter)|
39
- converter.call(val, context, table_context)
40
- end
41
- end
42
- end
43
- end
44
- end
45
- end
46
- end
@@ -1,49 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module TableStructure
4
- class Table::ContextBuilder
5
- class ContextBuildable < Module
6
- def initialize(methods)
7
- methods.each do |name, method|
8
- define_method(name, &method)
9
- end
10
- end
11
- end
12
-
13
- class << self
14
- def create_module(
15
- builders,
16
- apply_to_name: true,
17
- apply_to_value: true,
18
- context:
19
- )
20
- return if builders.empty?
21
-
22
- header_builder = builders[:header] # TODO: Change not to use keyword of `header`
23
- row_builder = builders[:row]
24
-
25
- methods = {}
26
-
27
- if apply_to_name && builders.key?(:header)
28
- methods[:header] = create_method(builders[:header])
29
- end
30
-
31
- if apply_to_value && builders.key?(:row)
32
- methods[:data] = create_method(builders[:row])
33
- end
34
-
35
- yield ContextBuildable.new(methods)
36
- end
37
-
38
- private
39
-
40
- def create_method(builder)
41
- return if builder.nil?
42
-
43
- proc do |context: nil|
44
- super(context: builder.call(context))
45
- end
46
- end
47
- end
48
- end
49
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module TableStructure
4
- class Table::Iterator
5
- def initialize(table, header: { context: nil })
6
- @table = table
7
- @header_options = header
8
- end
9
-
10
- def iterate(items)
11
- ::Enumerator.new do |y|
12
- if @header_options
13
- header_context = @header_options.is_a?(Hash) ? @header_options[:context] : nil
14
- y << @table.header(context: header_context)
15
- end
16
- @table
17
- .body(items)
18
- .each { |row| y << row }
19
- end
20
- end
21
- end
22
- end
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module TableStructure
4
- class Table::RowBuilder
5
- class ResultBuildable < Module
6
- def initialize(methods)
7
- methods.each do |name, method|
8
- define_method(name, &method)
9
- end
10
- end
11
- end
12
-
13
- class << self
14
- def create_module(builders, row_type:, keys:, context:)
15
- return if builders.empty?
16
-
17
- builders = builders.select { |_k, v| v.enabled?(row_type) }
18
- return if builders.empty?
19
-
20
- yield ResultBuildable.new(
21
- header: create_method(builders, keys, context),
22
- data: create_method(builders, keys, context)
23
- )
24
- end
25
-
26
- private
27
-
28
- def create_method(builders, table_keys, table_context)
29
- proc do |context: nil|
30
- builders
31
- .reduce(super(context: context)) do |vals, (_, builder)|
32
- builder.call(vals, table_keys, context, table_context)
33
- end
34
- end
35
- end
36
- end
37
- end
38
- end