table_structure 0.3.11 → 0.3.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ccde21d51d86d14d5aba71a9924bcc8d3669032b380005019866699cf5a9fd87
4
- data.tar.gz: 31d7f45528e0972886db3a2f07f53cd030671bbabc79dda03e6728262da56d95
3
+ metadata.gz: 2b675232423fea16fcc7aaaeae505f782fd8db4371065e242eb794eeed07b0c9
4
+ data.tar.gz: c8f02884ae3ad9e768fde5253f9023299bd9967cb8b49406799a6f467ce1de17
5
5
  SHA512:
6
- metadata.gz: '04844310bb6ebc8eb89aa78d9ee4f350ffa566d581d3acd555bdef70271326d674cb2e8dad9843b243a46daea29f0fe0ac879eeaa15da7b68b34c5c93dc670e4'
7
- data.tar.gz: aebff92798ac40979500d660e4392eb567bcf4f0efbbc825859fc9a60b5b8be52e458776adab3665243706df995314834512335a7e0aa01f0b526b65d6509f6e
6
+ metadata.gz: 537a05a78c42d4250d5c64d51b839c9abfb16ee0055d2fdc2f59fb36e70d38f0a406d549c6d2ad00634a80812c2f9673064659829263cdb682e84c2ff5456429
7
+ data.tar.gz: e58886afb4755ef78a6520e7d4e07d941a4c5bc0c419fbe4e9f0bfd0739ed8005278a32bcb958c0284ae1e84d2dfcdca53db9d5b5c826682e367412a33c951ef
data/CHANGELOG.md CHANGED
@@ -1,14 +1,19 @@
1
+ # 0.3.12
2
+ Changes:
3
+ - `TableStructure::Schema`
4
+ - Fix `:name_prefix` and `:name_suffix` options so that they are applied after the column converters defined in the schema.
5
+
1
6
  # 0.3.11
2
7
  Changes:
3
8
  - `TableStructure::CSV::Writer`
4
- - Add `:csv_options` option. This option's value is simply passed to `::CSV.new`'s options.
9
+ - Add `:csv_options` option. This option's value is simply passed to `::CSV.new` as options'.
5
10
 
6
11
  # 0.3.10
7
12
  Changes:
8
13
  - `TableStructure::Writer`
9
14
  - Fix an issue that objects with both `call` and` each` methods could not be enumerated.
10
15
  - `TableStructure::CSV::Writer`
11
- - Fix write method's block to work.
16
+ - Fix `write` method's block to work.
12
17
 
13
18
  # 0.3.9
14
19
  Changes:
@@ -19,7 +24,7 @@ Changes:
19
24
  - `TableStructure::Schema`
20
25
  - Add `:nil_definitions_ignored` option.
21
26
  - This defaults to `false`, which is same behavior as before.
22
- - If `true` is set, ignore the column definitions evaluated to `nil`. this behaves like as if to define `omitted: true` in the column definition.
27
+ - If `true` is set, the column definitions evaluated to `nil` are ignored. this behaves like as if to define `omitted: true` in the column definition.
23
28
 
24
29
  # 0.3.7
25
30
  Changes:
@@ -50,7 +55,7 @@ Changes:
50
55
  - `TableStructure::Writer`
51
56
  - Fix broken `:result_type` option.
52
57
  - `TableStructure::Iterator`
53
- - Fix broken `:result_type` option. (Passed to the writer internally.)
58
+ - Fix broken `:result_type` option.
54
59
 
55
60
  # 0.3.3
56
61
  Changes:
@@ -70,9 +75,7 @@ Changes:
70
75
  - `TableStructure::Writer`
71
76
  - Make `:result_type` option available.
72
77
  - `TableStructure::Iterator`
73
- - Make `:result_type` option available. (Passed to the writer internally.)
74
- - `TableStructure::Schema`
75
- - `:result_type` option is deprecated.
78
+ - Make `:result_type` option available.
76
79
 
77
80
  # 0.3.0
78
81
  Changes:
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- table_structure (0.3.11)
4
+ table_structure (0.3.12)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -342,11 +342,10 @@ end
342
342
  You can also use only `TableStructure::Schema`.
343
343
  ```ruby
344
344
  schema = SampleTableSchema.new
345
- table = schema.create_table
345
+ table = schema.create_table(result_type: :hash)
346
346
  header = table.header
347
347
  items.each do |item|
348
348
  row = table.row(context: item)
349
- ...
350
349
  end
351
350
  ```
352
351
 
@@ -15,12 +15,11 @@ module TableStructure
15
15
  require 'table_structure/schema/definition/compiler'
16
16
  require 'table_structure/schema/definition/error'
17
17
  require 'table_structure/schema/definition/validator'
18
+ require 'table_structure/schema/column_converters'
19
+ require 'table_structure/schema/context_builders'
20
+ require 'table_structure/schema/result_builders'
18
21
  require 'table_structure/schema/table'
19
- require 'table_structure/schema/table/column_converter'
20
- require 'table_structure/schema/table/context_builder'
21
22
  require 'table_structure/schema/table/key_decorator'
22
- require 'table_structure/schema/table/result_builder'
23
- require 'table_structure/schema/column'
24
23
  require 'table_structure/schema/column/attrs'
25
24
  require 'table_structure/schema/column/schema'
26
25
  require 'table_structure/schema/utils'
@@ -36,9 +36,8 @@ module TableStructure
36
36
  )
37
37
  end
38
38
 
39
- def create_table(result_type: :array, **options)
40
- options = @table_structure_schema_definition_.options.merge(options)
41
- @table_structure_schema_definition_.create_table(result_type: result_type, **options)
39
+ def create_table(**options)
40
+ @table_structure_schema_definition_.create_table(options)
42
41
  end
43
42
  end
44
43
  end
@@ -6,12 +6,11 @@ module TableStructure
6
6
  class Attrs
7
7
  attr_reader :keys, :size
8
8
 
9
- def initialize(definition, options)
9
+ def initialize(definition)
10
10
  @name = definition[:name]
11
11
  @keys = optimize_size([definition[:key]].flatten, definition[:size])
12
12
  @value = definition[:value]
13
13
  @size = definition[:size]
14
- @options = options
15
14
  end
16
15
 
17
16
  def name(context, table_context)
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TableStructure
4
+ module Schema
5
+ class ColumnConverters
6
+ def initialize(converters)
7
+ @header_converters = select_converters_for(:header, converters)
8
+ @row_converters = select_converters_for(:row, converters)
9
+ end
10
+
11
+ def extend_methods_for(table)
12
+ table_context = table.instance_variable_get(:@context)
13
+ table_options = table.instance_variable_get(:@options)
14
+
15
+ header_converters = @header_converters.merge(optional_header_converters(table_options))
16
+ row_converters = @row_converters
17
+
18
+ methods = {}
19
+ unless header_converters.empty?
20
+ methods[:header] = create_method(header_converters, table_context)
21
+ end
22
+ unless row_converters.empty?
23
+ methods[:row] = create_method(row_converters, table_context)
24
+ end
25
+
26
+ return if methods.empty?
27
+
28
+ table.extend ColumnConverter.new(methods)
29
+ end
30
+
31
+ private
32
+
33
+ def select_converters_for(method, converters)
34
+ converters
35
+ .select { |_k, v| v[:options][method] }
36
+ .map { |k, v| [k, v[:callable]] }
37
+ .to_h
38
+ end
39
+
40
+ def optional_header_converters(options)
41
+ converters = {}
42
+ if options[:name_prefix]
43
+ converters[:_prepend_prefix_] =
44
+ create_prefix_converter(options[:name_prefix])
45
+ end
46
+ if options[:name_suffix]
47
+ converters[:_append_suffix_] =
48
+ create_suffix_converter(options[:name_suffix])
49
+ end
50
+
51
+ converters
52
+ end
53
+
54
+ def create_prefix_converter(prefix)
55
+ lambda { |val, *|
56
+ val.nil? ? val : "#{prefix}#{val}"
57
+ }
58
+ end
59
+
60
+ def create_suffix_converter(suffix)
61
+ lambda { |val, *|
62
+ val.nil? ? val : "#{val}#{suffix}"
63
+ }
64
+ end
65
+
66
+ def create_method(converters, table_context)
67
+ proc do |context: nil|
68
+ values = super(context: context)
69
+ values.map do |val|
70
+ converters.reduce(val) do |val, (_, converter)|
71
+ converter.call(val, context, table_context)
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+
78
+ class ColumnConverter < Module
79
+ def initialize(methods)
80
+ methods.each do |name, method|
81
+ define_method(name, &method)
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TableStructure
4
+ module Schema
5
+ class ContextBuilders
6
+ def initialize(builders)
7
+ @table_builder = builders[:table]
8
+ @header_builder = builders[:header]
9
+ @row_builder = builders[:row]
10
+ end
11
+
12
+ def build_for_table(context)
13
+ if @table_builder
14
+ @table_builder.call(context)
15
+ else
16
+ context
17
+ end
18
+ end
19
+
20
+ def extend_methods_for(table)
21
+ methods = {}
22
+ methods[:header] = create_method(@header_builder) if @header_builder
23
+ methods[:row] = create_method(@row_builder) if @row_builder
24
+
25
+ return if methods.empty?
26
+
27
+ table.extend ContextBuilder.new(methods)
28
+ end
29
+
30
+ private
31
+
32
+ def create_method(builder)
33
+ proc do |context: nil|
34
+ super(context: builder.call(context))
35
+ end
36
+ end
37
+ end
38
+
39
+ class ContextBuilder < Module
40
+ def initialize(methods)
41
+ methods.each do |name, method|
42
+ define_method(name, &method)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -3,17 +3,6 @@
3
3
  module TableStructure
4
4
  module Schema
5
5
  class Definition
6
- RESULT_BUILDERS = {
7
- to_hash: {
8
- callable: lambda { |values, keys, *|
9
- keys.map.with_index { |key, i| [key || i, values[i]] }.to_h
10
- },
11
- options: {
12
- enabled_result_types: [:hash]
13
- }
14
- }
15
- }.freeze
16
-
17
6
  attr_reader :options
18
7
 
19
8
  def initialize(
@@ -25,38 +14,30 @@ module TableStructure
25
14
  context,
26
15
  options
27
16
  )
28
- table_context_builder = context_builders.delete(:table)
29
- context = table_context_builder.call(context) if table_context_builder
30
-
31
17
  @name = name
32
- @columns = create_columns(name, column_definitions, context, options)
33
- @header_context_builder = context_builders[:header]
34
- @row_context_builder = context_builders[:row]
35
- @header_column_converters = select_column_converters(:header, column_converters)
36
- @row_column_converters = select_column_converters(:row, column_converters)
37
- @result_builders = result_builders
38
- @context = context
18
+ @context_builders = ContextBuilders.new(context_builders)
19
+ @column_converters = ColumnConverters.new(column_converters)
20
+ @result_builders = ResultBuilders.new(result_builders)
21
+ @context = @context_builders.build_for_table(context)
39
22
  @options = options
23
+
24
+ @columns = create_columns(@name, column_definitions, @context, @options)
40
25
  end
41
26
 
42
- def create_table(result_type: :array, **options)
27
+ def create_table(**options)
43
28
  options = @options.merge(options)
44
29
 
45
- header_column_converters =
46
- optional_header_column_converters(options).merge(@header_column_converters)
47
-
48
- result_builders = select_result_builders(result_type)
49
-
50
- Table.new(
30
+ table = Table.new(
51
31
  @columns,
52
- @header_context_builder,
53
- @row_context_builder,
54
- header_column_converters,
55
- @row_column_converters,
56
- result_builders,
57
32
  @context,
58
33
  options
59
34
  )
35
+
36
+ @context_builders.extend_methods_for(table)
37
+ @column_converters.extend_methods_for(table)
38
+ @result_builders.extend_methods_for(table)
39
+
40
+ table
60
41
  end
61
42
 
62
43
  private
@@ -65,38 +46,13 @@ module TableStructure
65
46
  Compiler
66
47
  .new(name, definitions, options)
67
48
  .compile(context)
68
- .map { |definition| Column.create(definition, options) }
69
- end
70
-
71
- def select_column_converters(method, column_converters)
72
- column_converters
73
- .select { |_k, v| v[:options][method] }
74
- .map { |k, v| [k, v[:callable]] }
75
- .to_h
76
- end
77
-
78
- def optional_header_column_converters(options)
79
- column_converters = {}
80
- if options[:name_prefix]
81
- column_converters[:_prepend_prefix] = lambda { |val, *|
82
- val.nil? ? val : "#{options[:name_prefix]}#{val}"
83
- }
84
- end
85
- if options[:name_suffix]
86
- column_converters[:_append_suffix] = lambda { |val, *|
87
- val.nil? ? val : "#{val}#{options[:name_suffix]}"
88
- }
89
- end
90
-
91
- column_converters
92
- end
93
-
94
- def select_result_builders(result_type)
95
- RESULT_BUILDERS
96
- .merge(@result_builders)
97
- .select { |_k, v| v[:options][:enabled_result_types].include?(result_type) }
98
- .map { |k, v| [k, v[:callable]] }
99
- .to_h
49
+ .map do |definition|
50
+ if definition.is_a?(Hash)
51
+ Column::Attrs.new(definition)
52
+ else
53
+ Column::Schema.new(definition)
54
+ end
55
+ end
100
56
  end
101
57
  end
102
58
  end
@@ -24,7 +24,7 @@ module TableStructure
24
24
  @definitions
25
25
  .map { |definition| Utils.evaluate_callable(definition, context) }
26
26
  .map.with_index do |definition, i|
27
- validator = Validator.new(@name, i, @options)
27
+ validator = Validator.new(@name, i)
28
28
 
29
29
  [definition]
30
30
  .flatten
@@ -6,10 +6,9 @@ module TableStructure
6
6
  class Validator
7
7
  DEFAULT_SIZE = 1
8
8
 
9
- def initialize(name, index, options)
9
+ def initialize(name, index)
10
10
  @name = name
11
11
  @index = index
12
- @options = options
13
12
  end
14
13
 
15
14
  def validate(name:, key:, size:, **)
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TableStructure
4
+ module Schema
5
+ class ResultBuilders
6
+ DEFAULT_BUILDERS = {
7
+ _to_hash_: {
8
+ callable: lambda { |values, keys, *|
9
+ keys.map.with_index { |key, i| [key || i, values[i]] }.to_h
10
+ },
11
+ options: {
12
+ enabled_result_types: [:hash]
13
+ }
14
+ }
15
+ }.freeze
16
+
17
+ def initialize(builders)
18
+ @builders = builders
19
+ end
20
+
21
+ def extend_methods_for(table)
22
+ table_context = table.instance_variable_get(:@context)
23
+ table_options = table.instance_variable_get(:@options)
24
+ table_keys = table.send(:keys)
25
+
26
+ builders = select_builders(table_options[:result_type])
27
+
28
+ methods = {}
29
+ unless builders.empty?
30
+ methods[:header] = create_method(builders, table_keys, table_context)
31
+ methods[:row] = create_method(builders, table_keys, table_context)
32
+ end
33
+
34
+ return if methods.empty?
35
+
36
+ table.extend ResultBuilder.new(methods)
37
+ end
38
+
39
+ private
40
+
41
+ def select_builders(result_type)
42
+ DEFAULT_BUILDERS
43
+ .merge(@builders)
44
+ .select { |_k, v| v[:options][:enabled_result_types].include?(result_type) }
45
+ .map { |k, v| [k, v[:callable]] }
46
+ .to_h
47
+ end
48
+
49
+ def create_method(builders, table_keys, table_context)
50
+ proc do |context: nil|
51
+ values = super(context: context)
52
+ builders
53
+ .reduce(values) do |vals, (_, builder)|
54
+ builder.call(vals, table_keys, context, table_context)
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ class ResultBuilder < Module
61
+ def initialize(methods)
62
+ methods.each do |name, method|
63
+ define_method(name, &method)
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -3,55 +3,18 @@
3
3
  module TableStructure
4
4
  module Schema
5
5
  class Table
6
- # TODO: Remove following `attr_reader`.
7
- attr_reader :header_column_converters, :row_column_converters, :result_builders
6
+ DEFAULT_OPTIONS = {
7
+ result_type: :array
8
+ }.freeze
8
9
 
9
10
  def initialize(
10
11
  columns,
11
- header_context_builder,
12
- row_context_builder,
13
- header_column_converters,
14
- row_column_converters,
15
- result_builders,
16
12
  context,
17
13
  options
18
14
  )
19
15
  @columns = columns
20
- @header_column_converters = header_column_converters
21
- @row_column_converters = row_column_converters
22
- @result_builders = result_builders
23
16
  @context = context
24
- @options = options
25
-
26
- if header_context_builder || row_context_builder
27
- singleton_class.include ContextBuilder.new(
28
- [
29
- { method: :header, callable: header_context_builder },
30
- { method: :row, callable: row_context_builder }
31
- ]
32
- )
33
- end
34
-
35
- if !header_column_converters.empty? || !row_column_converters.empty?
36
- singleton_class.include ColumnConverter.new(
37
- [
38
- { method: :header, callables: header_column_converters },
39
- { method: :row, callables: row_column_converters }
40
- ],
41
- context: context
42
- )
43
- end
44
-
45
- unless result_builders.empty?
46
- singleton_class.include ResultBuilder.new(
47
- [
48
- { method: :header, callables: result_builders },
49
- { method: :row, callables: result_builders }
50
- ],
51
- keys: keys,
52
- context: context
53
- )
54
- end
17
+ @options = DEFAULT_OPTIONS.merge(options)
55
18
  end
56
19
 
57
20
  def header(context: nil)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TableStructure
4
- VERSION = '0.3.11'
4
+ VERSION = '0.3.12'
5
5
  end
@@ -5,7 +5,6 @@ module TableStructure
5
5
  DEFAULT_OPTIONS = {
6
6
  header_omitted: false,
7
7
  header_context: nil,
8
- result_type: :array,
9
8
  method: :<<
10
9
  }.freeze
11
10
 
@@ -38,7 +37,7 @@ module TableStructure
38
37
  if items.respond_to?(:each)
39
38
  items
40
39
  elsif items.respond_to?(:call)
41
- warn "[TableStructure] Use `Enumerator` instead of `lambda` to wrap items. `lambda` has been deprecated. #{items}"
40
+ warn "[TableStructure] Use `Enumerator` to wrap items instead of `lambda`. The use of `lambda` has been deprecated. #{items}"
42
41
  Enumerator.new { |y| items.call(y) }
43
42
  else
44
43
  raise ::TableStructure::Error, 'Items is not enumerable.'
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.11
4
+ version: 0.3.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - jsmmr
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-04 00:00:00.000000000 Z
11
+ date: 2019-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -74,9 +74,10 @@ files:
74
74
  - lib/table_structure/csv/writer.rb
75
75
  - lib/table_structure/iterator.rb
76
76
  - lib/table_structure/schema.rb
77
- - lib/table_structure/schema/column.rb
78
77
  - lib/table_structure/schema/column/attrs.rb
79
78
  - lib/table_structure/schema/column/schema.rb
79
+ - lib/table_structure/schema/column_converters.rb
80
+ - lib/table_structure/schema/context_builders.rb
80
81
  - lib/table_structure/schema/definition.rb
81
82
  - lib/table_structure/schema/definition/compiler.rb
82
83
  - lib/table_structure/schema/definition/error.rb
@@ -86,11 +87,9 @@ files:
86
87
  - lib/table_structure/schema/dsl/context_builder.rb
87
88
  - lib/table_structure/schema/dsl/option.rb
88
89
  - lib/table_structure/schema/dsl/result_builder.rb
90
+ - lib/table_structure/schema/result_builders.rb
89
91
  - lib/table_structure/schema/table.rb
90
- - lib/table_structure/schema/table/column_converter.rb
91
- - lib/table_structure/schema/table/context_builder.rb
92
92
  - lib/table_structure/schema/table/key_decorator.rb
93
- - lib/table_structure/schema/table/result_builder.rb
94
93
  - lib/table_structure/schema/utils.rb
95
94
  - lib/table_structure/version.rb
96
95
  - lib/table_structure/writer.rb
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module TableStructure
4
- module Schema
5
- module Column
6
- def self.create(definition, options)
7
- if definition.is_a?(Hash)
8
- Attrs.new(definition, options)
9
- else
10
- Schema.new(definition)
11
- end
12
- end
13
- end
14
- end
15
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module TableStructure
4
- module Schema
5
- class Table
6
- class ColumnConverter < Module
7
- def initialize(overrides, context: nil)
8
- table_context = context
9
- overrides
10
- .reject { |callables:, **| callables.nil? || callables.empty? }
11
- .each do |method:, callables:|
12
- define_method(method) do |context: nil|
13
- values = super(context: context)
14
- values.map do |val|
15
- callables.reduce(val) do |val, (_, callable)|
16
- callable.call(val, context, table_context)
17
- end
18
- end
19
- end
20
- end
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module TableStructure
4
- module Schema
5
- class Table
6
- class ContextBuilder < Module
7
- def initialize(overrides)
8
- overrides
9
- .reject { |callable:, **| callable.nil? }
10
- .each do |method:, callable:|
11
- define_method(method) do |context: nil|
12
- super(context: callable.call(context))
13
- end
14
- end
15
- end
16
- end
17
- end
18
- end
19
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module TableStructure
4
- module Schema
5
- class Table
6
- class ResultBuilder < Module
7
- def initialize(overrides, keys:, context: nil)
8
- table_context = context
9
- overrides
10
- .reject { |callables:, **| callables.nil? || callables.empty? }
11
- .each do |method:, callables:|
12
- define_method(method) do |context: nil|
13
- values = super(context: context)
14
- callables
15
- .reduce(values) do |vals, (_, callable)|
16
- callable.call(vals, keys, context, table_context)
17
- end
18
- end
19
- end
20
- end
21
- end
22
- end
23
- end
24
- end