table_structure 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -17
- data/lib/table_structure/schema/column.rb +17 -64
- data/lib/table_structure/schema/definition/error.rb +16 -0
- data/lib/table_structure/schema/definition/validator.rb +33 -0
- data/lib/table_structure/schema/definition.rb +55 -0
- data/lib/table_structure/schema/table.rb +11 -15
- data/lib/table_structure/version.rb +1 -1
- data/lib/table_structure.rb +3 -0
- data/table_structure.gemspec +0 -1
- metadata +5 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af7a012ce96e50bfc67ff0360ba39b8f2c2f0b0772e0260b17729777b805eeb7
|
4
|
+
data.tar.gz: 4fd45259f91bde2016be845200cbd88ba3b50c3e7b6c75b257e228f4475a500a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc61fd25875979c3c3e06ba9e0648bea2eed3abcf058072ef2802c1c0379b194aeee4213b76268ae10118368cbd0076303658ca3ce2d19c85ea366709ae6f3f5
|
7
|
+
data.tar.gz: 82203dac6eaf8418eecb9ee4ed62b550277f6c57d54f4f78d04b72edb317a7cec91b16f1f16fa43749d56a55ebb28d871be82e11bccf72ab3db128e45e3c6795
|
data/Gemfile.lock
CHANGED
@@ -1,18 +1,12 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
table_structure (0.1.
|
4
|
+
table_structure (0.1.4)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
-
ast (2.4.0)
|
10
9
|
diff-lcs (1.3)
|
11
|
-
jaro_winkler (1.5.3)
|
12
|
-
parallel (1.17.0)
|
13
|
-
parser (2.6.3.0)
|
14
|
-
ast (~> 2.4.0)
|
15
|
-
rainbow (3.0.0)
|
16
10
|
rake (10.5.0)
|
17
11
|
rspec (3.8.0)
|
18
12
|
rspec-core (~> 3.8.0)
|
@@ -27,15 +21,6 @@ GEM
|
|
27
21
|
diff-lcs (>= 1.2.0, < 2.0)
|
28
22
|
rspec-support (~> 3.8.0)
|
29
23
|
rspec-support (3.8.2)
|
30
|
-
rubocop (0.74.0)
|
31
|
-
jaro_winkler (~> 1.5.1)
|
32
|
-
parallel (~> 1.10)
|
33
|
-
parser (>= 2.6)
|
34
|
-
rainbow (>= 2.2.2, < 4.0)
|
35
|
-
ruby-progressbar (~> 1.7)
|
36
|
-
unicode-display_width (>= 1.4.0, < 1.7)
|
37
|
-
ruby-progressbar (1.10.1)
|
38
|
-
unicode-display_width (1.6.0)
|
39
24
|
|
40
25
|
PLATFORMS
|
41
26
|
ruby
|
@@ -44,7 +29,6 @@ DEPENDENCIES
|
|
44
29
|
bundler (~> 1.17)
|
45
30
|
rake (~> 10.0)
|
46
31
|
rspec (~> 3.0)
|
47
|
-
rubocop (~> 0.74.0)
|
48
32
|
table_structure!
|
49
33
|
|
50
34
|
BUNDLED WITH
|
@@ -3,39 +3,18 @@
|
|
3
3
|
module TableStructure
|
4
4
|
module Schema
|
5
5
|
class Column
|
6
|
-
|
7
|
-
attr_reader :group_index
|
6
|
+
attr_reader :name, :key, :vlaue, :size
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
DEFAULT_DEFINITION = {
|
16
|
-
name: nil,
|
17
|
-
key: nil,
|
18
|
-
value: nil,
|
19
|
-
size: nil
|
20
|
-
}.freeze
|
21
|
-
|
22
|
-
DEFAULT_SIZE = 1
|
23
|
-
|
24
|
-
attr_reader :size, :group_index
|
25
|
-
|
26
|
-
def initialize(definition, group_index)
|
27
|
-
@group_index = group_index
|
28
|
-
definition = DEFAULT_DEFINITION.merge(definition)
|
29
|
-
validate(definition)
|
30
|
-
@name = definition[:name]
|
31
|
-
@key = definition[:key]
|
32
|
-
@value = definition[:value]
|
33
|
-
@size = determine_size(definition)
|
8
|
+
def initialize(name:, key:, value:, size:)
|
9
|
+
@name = name
|
10
|
+
@key = key
|
11
|
+
@value = value
|
12
|
+
@size = size
|
34
13
|
end
|
35
14
|
|
36
15
|
def name(header_context, table_context)
|
37
|
-
|
38
|
-
optimize_size(
|
16
|
+
name = Utils.evaluate_callable(@name, header_context, table_context)
|
17
|
+
optimize_size(name)
|
39
18
|
end
|
40
19
|
|
41
20
|
def key
|
@@ -43,48 +22,22 @@ module TableStructure
|
|
43
22
|
end
|
44
23
|
|
45
24
|
def value(row_context, table_context)
|
46
|
-
|
47
|
-
optimize_size(
|
25
|
+
value = Utils.evaluate_callable(@value, row_context, table_context)
|
26
|
+
optimize_size(value)
|
48
27
|
end
|
49
28
|
|
50
29
|
private
|
51
30
|
|
52
|
-
def validate(name:, key:, size:, **)
|
53
|
-
if !key && name.respond_to?(:call) && !size
|
54
|
-
raise Error.new('"size" must be specified, because column size cannot be determined.', @group_index)
|
55
|
-
end
|
56
|
-
if size && size < DEFAULT_SIZE
|
57
|
-
raise Error.new('"size" must be positive.', @group_index)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def determine_size(name:, key:, size:, **)
|
62
|
-
return size if size
|
63
|
-
|
64
|
-
[calculate_size(name), calculate_size(key)].max
|
65
|
-
end
|
66
|
-
|
67
|
-
def calculate_size(val)
|
68
|
-
if val.is_a?(Array)
|
69
|
-
return val.empty? ? DEFAULT_SIZE : val.size
|
70
|
-
end
|
71
|
-
|
72
|
-
DEFAULT_SIZE
|
73
|
-
end
|
74
|
-
|
75
|
-
def multiple?
|
76
|
-
@size > DEFAULT_SIZE
|
77
|
-
end
|
78
|
-
|
79
31
|
def optimize_size(value)
|
80
|
-
return value
|
32
|
+
return value if @size == 1
|
81
33
|
|
82
|
-
values =
|
34
|
+
values = [value].flatten
|
83
35
|
actual_size = values.size
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
36
|
+
expected_size = @size
|
37
|
+
if actual_size > expected_size
|
38
|
+
values[0, expected_size]
|
39
|
+
elsif actual_size < expected_size
|
40
|
+
[].concat(values).fill(nil, actual_size, (expected_size - actual_size))
|
88
41
|
else
|
89
42
|
values
|
90
43
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TableStructure
|
4
|
+
module Schema
|
5
|
+
class Definition
|
6
|
+
class Error < ::TableStructure::Error
|
7
|
+
attr_reader :index
|
8
|
+
|
9
|
+
def initialize(error_message, index)
|
10
|
+
@index = index
|
11
|
+
super("#{error_message} [defined position of column(s): #{index + 1}]")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TableStructure
|
4
|
+
module Schema
|
5
|
+
class Definition
|
6
|
+
class Validator
|
7
|
+
DEFAULT_SIZE = 1
|
8
|
+
|
9
|
+
def initialize(index, options)
|
10
|
+
@index = index
|
11
|
+
@options = options
|
12
|
+
end
|
13
|
+
|
14
|
+
def validate(name:, key:, size:, **)
|
15
|
+
if key.respond_to?(:call)
|
16
|
+
raise Error.new('"key" must not be lambda.', @index)
|
17
|
+
end
|
18
|
+
if !key && name.respond_to?(:call) && !size
|
19
|
+
raise Error.new('"size" must be specified, because column size cannot be determined.', @index)
|
20
|
+
end
|
21
|
+
if size && size < DEFAULT_SIZE
|
22
|
+
raise Error.new('"size" must be positive.', @index)
|
23
|
+
end
|
24
|
+
if @options[:result_type] == :hash && !key
|
25
|
+
raise Error.new('"key" must be specified when "result_type: :hash" is specified.', @index)
|
26
|
+
end
|
27
|
+
|
28
|
+
true
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TableStructure
|
4
|
+
module Schema
|
5
|
+
class Definition
|
6
|
+
DEFAULT_ATTRS = {
|
7
|
+
name: nil,
|
8
|
+
key: nil,
|
9
|
+
value: nil,
|
10
|
+
size: nil
|
11
|
+
}.freeze
|
12
|
+
|
13
|
+
DEFAULT_SIZE = 1
|
14
|
+
|
15
|
+
def initialize(definitions, options)
|
16
|
+
@definitions = definitions
|
17
|
+
@options = options
|
18
|
+
end
|
19
|
+
|
20
|
+
def compile(context = nil)
|
21
|
+
@definitions
|
22
|
+
.map { |definition| Utils.evaluate_callable(definition, context) }
|
23
|
+
.map.with_index do |definition, i|
|
24
|
+
validator = Validator.new(i, @options)
|
25
|
+
|
26
|
+
[definition]
|
27
|
+
.flatten
|
28
|
+
.map do |definition|
|
29
|
+
definition = DEFAULT_ATTRS.merge(definition)
|
30
|
+
validator.validate(definition)
|
31
|
+
definition[:size] = determine_size(definition)
|
32
|
+
definition
|
33
|
+
end
|
34
|
+
end
|
35
|
+
.flatten
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def determine_size(name:, key:, size:, **)
|
41
|
+
return size if size
|
42
|
+
|
43
|
+
[calculate_size(name), calculate_size(key)].max
|
44
|
+
end
|
45
|
+
|
46
|
+
def calculate_size(val)
|
47
|
+
if val.is_a?(Array)
|
48
|
+
return val.empty? ? DEFAULT_SIZE : val.size
|
49
|
+
end
|
50
|
+
|
51
|
+
DEFAULT_SIZE
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -8,11 +8,11 @@ module TableStructure
|
|
8
8
|
attr_reader :columns, :column_converters, :result_builders
|
9
9
|
|
10
10
|
def initialize(column_definitions, column_converters, result_builders, context, options)
|
11
|
-
|
12
|
-
@
|
13
|
-
@columns = build_columns(column_definitions)
|
11
|
+
options = DEFAULT_OPTIONS.merge(options)
|
12
|
+
@columns = build_columns(column_definitions, context, options)
|
14
13
|
@column_converters = default_column_converters.merge(column_converters)
|
15
|
-
@result_builders = default_result_builders.merge(result_builders)
|
14
|
+
@result_builders = default_result_builders(options).merge(result_builders)
|
15
|
+
@context = context
|
16
16
|
end
|
17
17
|
|
18
18
|
def header(context)
|
@@ -25,24 +25,20 @@ module TableStructure
|
|
25
25
|
|
26
26
|
private
|
27
27
|
|
28
|
-
def build_columns(definitions)
|
29
|
-
|
30
|
-
.
|
31
|
-
.
|
32
|
-
|
33
|
-
.flatten
|
34
|
-
.map { |definition| Column.new(definition, i) }
|
35
|
-
end
|
36
|
-
.flatten
|
28
|
+
def build_columns(definitions, context, options)
|
29
|
+
Definition
|
30
|
+
.new(definitions, options)
|
31
|
+
.compile(context)
|
32
|
+
.map { |attrs| Column.new(attrs) }
|
37
33
|
end
|
38
34
|
|
39
35
|
def default_column_converters
|
40
36
|
{}
|
41
37
|
end
|
42
38
|
|
43
|
-
def default_result_builders
|
39
|
+
def default_result_builders(options)
|
44
40
|
result_builders = {}
|
45
|
-
if
|
41
|
+
if options[:result_type] == :hash
|
46
42
|
result_builders[:to_h] = ->(array, *) { (@keys ||= keys).zip(array).to_h }
|
47
43
|
end
|
48
44
|
result_builders
|
data/lib/table_structure.rb
CHANGED
@@ -10,6 +10,9 @@ module TableStructure
|
|
10
10
|
require 'table_structure/schema/dsl/column_definition'
|
11
11
|
require 'table_structure/schema/dsl/context_builder'
|
12
12
|
require 'table_structure/schema/dsl/result_builder'
|
13
|
+
require 'table_structure/schema/definition'
|
14
|
+
require 'table_structure/schema/definition/error'
|
15
|
+
require 'table_structure/schema/definition/validator'
|
13
16
|
require 'table_structure/schema/table'
|
14
17
|
require 'table_structure/schema/column'
|
15
18
|
require 'table_structure/schema/utils'
|
data/table_structure.gemspec
CHANGED
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.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jsmmr
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-08-
|
11
|
+
date: 2019-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,20 +52,6 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: rubocop
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - "~>"
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: 0.74.0
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - "~>"
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: 0.74.0
|
69
55
|
description: This gem creates and outputs table structure data. Useful for creating
|
70
56
|
CSV.
|
71
57
|
email:
|
@@ -87,6 +73,9 @@ files:
|
|
87
73
|
- lib/table_structure.rb
|
88
74
|
- lib/table_structure/schema.rb
|
89
75
|
- lib/table_structure/schema/column.rb
|
76
|
+
- lib/table_structure/schema/definition.rb
|
77
|
+
- lib/table_structure/schema/definition/error.rb
|
78
|
+
- lib/table_structure/schema/definition/validator.rb
|
90
79
|
- lib/table_structure/schema/dsl/column_converter.rb
|
91
80
|
- lib/table_structure/schema/dsl/column_definition.rb
|
92
81
|
- lib/table_structure/schema/dsl/context_builder.rb
|