table_structure 0.3.21 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -1
  3. data/CHANGELOG.md +37 -0
  4. data/Gemfile.lock +7 -7
  5. data/README.md +84 -78
  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 +2 -2
  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/context_builder.rb +3 -5
  19. data/lib/table_structure/schema/dsl/row_builder.rb +5 -5
  20. data/lib/table_structure/schema/{key_converter.rb → keys_builder.rb} +2 -2
  21. data/lib/table_structure/schema/row_context_builder_factory.rb +30 -0
  22. data/lib/table_structure/table.rb +31 -56
  23. data/lib/table_structure/utils.rb +40 -0
  24. data/lib/table_structure/version.rb +1 -1
  25. data/lib/table_structure/writer.rb +7 -54
  26. metadata +8 -14
  27. data/lib/table_structure/schema/column_converter.rb +0 -48
  28. data/lib/table_structure/schema/definition/column_converter.rb +0 -31
  29. data/lib/table_structure/schema/definition/context_builder.rb +0 -17
  30. data/lib/table_structure/schema/definition/row_builder.rb +0 -25
  31. data/lib/table_structure/schema/dsl/column_converter.rb +0 -34
  32. data/lib/table_structure/schema/dsl/option.rb +0 -19
  33. data/lib/table_structure/schema/row_builder.rb +0 -22
  34. data/lib/table_structure/table/column_converter.rb +0 -46
  35. data/lib/table_structure/table/context_builder.rb +0 -49
  36. data/lib/table_structure/table/iterator.rb +0 -22
  37. data/lib/table_structure/table/row_builder.rb +0 -38
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_structure
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.21
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - jsmmr
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-18 00:00:00.000000000 Z
11
+ date: 2020-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -75,31 +75,25 @@ files:
75
75
  - lib/table_structure/iterator.rb
76
76
  - lib/table_structure/schema.rb
77
77
  - lib/table_structure/schema/class_methods.rb
78
- - lib/table_structure/schema/column_converter.rb
78
+ - lib/table_structure/schema/column_builder_factory.rb
79
79
  - lib/table_structure/schema/columns/attributes.rb
80
80
  - lib/table_structure/schema/columns/schema.rb
81
- - lib/table_structure/schema/definition/column_converter.rb
81
+ - lib/table_structure/schema/composite_class.rb
82
82
  - lib/table_structure/schema/definition/columns/attributes.rb
83
83
  - lib/table_structure/schema/definition/columns/compiler.rb
84
84
  - lib/table_structure/schema/definition/columns/error.rb
85
85
  - lib/table_structure/schema/definition/columns/schema_class.rb
86
86
  - lib/table_structure/schema/definition/columns/schema_instance.rb
87
87
  - lib/table_structure/schema/definition/columns/validator.rb
88
- - lib/table_structure/schema/definition/context_builder.rb
89
- - lib/table_structure/schema/definition/row_builder.rb
90
- - lib/table_structure/schema/dsl/column_converter.rb
88
+ - lib/table_structure/schema/dsl/column_builder.rb
91
89
  - lib/table_structure/schema/dsl/column_definition.rb
92
90
  - lib/table_structure/schema/dsl/context_builder.rb
93
- - lib/table_structure/schema/dsl/option.rb
94
91
  - lib/table_structure/schema/dsl/row_builder.rb
95
- - lib/table_structure/schema/key_converter.rb
96
- - lib/table_structure/schema/row_builder.rb
92
+ - lib/table_structure/schema/keys_builder.rb
93
+ - lib/table_structure/schema/row_context_builder_factory.rb
97
94
  - lib/table_structure/schema/utils.rb
98
95
  - lib/table_structure/table.rb
99
- - lib/table_structure/table/column_converter.rb
100
- - lib/table_structure/table/context_builder.rb
101
- - lib/table_structure/table/iterator.rb
102
- - lib/table_structure/table/row_builder.rb
96
+ - lib/table_structure/utils.rb
103
97
  - lib/table_structure/version.rb
104
98
  - lib/table_structure/writer.rb
105
99
  - table_structure.gemspec
@@ -1,48 +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
- lambda { |val, *|
31
- val.nil? ? val : "#{string}#{val}"
32
- },
33
- **options
34
- )
35
- end
36
-
37
- def create_appender(string, **options)
38
- Definition::ColumnConverter.new(
39
- lambda { |val, *|
40
- val.nil? ? val : "#{val}#{string}"
41
- },
42
- **options
43
- )
44
- end
45
- end
46
- end
47
- end
48
- 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
- callable,
13
- header: true,
14
- body: true
15
- )
16
- @callable = callable
17
- @applicable_to_header = header
18
- @applicable_to_body = body
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(callable)
12
- @callable = callable
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
- callable,
13
- enabled_row_types: %i[array hash]
14
- )
15
- @callable = callable
16
- @enabled_row_types = [enabled_row_types].flatten
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,34 +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,
10
- header: true,
11
- body: true,
12
- **deprecated_options
13
- )
14
- if deprecated_options.key?(:row)
15
- warn '[TableStructure] `:row` option has been deprecated. Use `:body` option instead.'
16
- body = deprecated_options[:row]
17
- end
18
-
19
- column_converters[name] =
20
- ::TableStructure::Schema::Definition::ColumnConverter.new(
21
- callable,
22
- header: header,
23
- body: body
24
- )
25
- nil
26
- end
27
-
28
- def column_converters
29
- @__column_converters__ ||= {}
30
- end
31
- end
32
- end
33
- end
34
- 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,22 +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
- lambda { |values, keys, *|
9
- keys.map.with_index { |key, i| [key || i, values[i]] }.to_h
10
- },
11
- enabled_row_types: [:hash]
12
- )
13
- }.freeze
14
-
15
- class << self
16
- def prepend_default_builders(builders)
17
- DEFAULT_ROW_BUILDERS.merge(builders)
18
- end
19
- end
20
- end
21
- end
22
- 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] # will remove
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