table_structure 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -2
- data/Gemfile.lock +1 -1
- data/README.md +1 -1
- data/lib/table_structure/schema/column/attrs.rb +12 -12
- data/lib/table_structure/schema/column/schema.rb +13 -5
- data/lib/table_structure/schema/definition/error.rb +5 -4
- data/lib/table_structure/schema/definition/validator.rb +6 -8
- data/lib/table_structure/schema/definition.rb +4 -3
- data/lib/table_structure/schema/table.rb +12 -5
- data/lib/table_structure/schema/utils.rb +0 -9
- data/lib/table_structure/schema.rb +2 -1
- data/lib/table_structure/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: edea213f708a84a4bfaf1a149a7c4fd9a78b2c50e36b72b951f6c0bc2b4d3a14
|
4
|
+
data.tar.gz: 71db859f76a5a55a33772d0764647afcfb0b6fa814705649d26c09f37f3ae35c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72abcf6d37f57322848493cd45e335276dc729524000007385d382dc0cb835fbf6ebe740c970e6de06bd4a78e45b4f63a1858f060bbbbbfc86396a9c2b57d088
|
7
|
+
data.tar.gz: 9332514abe32694295162c5349d5058e57c1bdf37a9c26eafb83e3bf18d9e01c88a128563f74193aece52fa0158adc6c2d2979fbd2663ad3b380aa17c4c3e03b
|
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,16 @@
|
|
1
|
+
# 0.3.2
|
2
|
+
Changes:
|
3
|
+
- `TableStructure::Writer`
|
4
|
+
- When `result_type: :hash` option is specified and `column(s)` key of the schema is undefined, index number is used as the key.
|
5
|
+
|
1
6
|
# 0.3.1
|
2
7
|
Changes:
|
8
|
+
- `TableStructure::Writer`
|
9
|
+
- Make `:result_type` option available.
|
10
|
+
- `TableStructure::Iterator`
|
11
|
+
- Make `:result_type` option available. (Passed to the writer internally.)
|
3
12
|
- `TableStructure::Schema`
|
4
|
-
-
|
5
|
-
- `:result_type` option for `TableStructure::Schema` is deprecated.
|
13
|
+
- `:result_type` option is deprecated.
|
6
14
|
|
7
15
|
# 0.3.0
|
8
16
|
Changes:
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -107,7 +107,7 @@ response_body = Enumerator.new { |y| writer.write(items, to: CSV.new(y)) }
|
|
107
107
|
```
|
108
108
|
|
109
109
|
#### TableStructure::Iterator
|
110
|
-
Specifying `result_type: :hash` option
|
110
|
+
Specifying `result_type: :hash` option works well.
|
111
111
|
To use this option, define `column(s)` with `:key`.
|
112
112
|
```ruby
|
113
113
|
class SampleTableSchema
|
@@ -8,7 +8,7 @@ module TableStructure
|
|
8
8
|
|
9
9
|
def initialize(definition, options)
|
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
14
|
@options = options
|
@@ -16,27 +16,29 @@ module TableStructure
|
|
16
16
|
|
17
17
|
def name(header_context, table_context)
|
18
18
|
name = Utils.evaluate_callable(@name, header_context, table_context)
|
19
|
-
optimize_size(name)
|
19
|
+
optimize_size(name, @size)
|
20
20
|
end
|
21
21
|
|
22
22
|
def key
|
23
|
-
|
24
|
-
|
23
|
+
if @options[:key_prefix] || @options[:key_suffix]
|
24
|
+
decorate_keys
|
25
|
+
else
|
26
|
+
@keys
|
27
|
+
end
|
25
28
|
end
|
26
29
|
|
27
30
|
def value(row_context, table_context)
|
28
31
|
value = Utils.evaluate_callable(@value, row_context, table_context)
|
29
|
-
optimize_size(value)
|
32
|
+
optimize_size(value, @size)
|
30
33
|
end
|
31
34
|
|
32
35
|
private
|
33
36
|
|
34
|
-
def optimize_size(value)
|
35
|
-
return value if
|
37
|
+
def optimize_size(value, expected_size)
|
38
|
+
return value if expected_size == 1 && !value.is_a?(Array)
|
36
39
|
|
37
40
|
values = [value].flatten
|
38
41
|
actual_size = values.size
|
39
|
-
expected_size = @size
|
40
42
|
if actual_size > expected_size
|
41
43
|
values[0, expected_size]
|
42
44
|
elsif actual_size < expected_size
|
@@ -46,10 +48,8 @@ module TableStructure
|
|
46
48
|
end
|
47
49
|
end
|
48
50
|
|
49
|
-
def
|
50
|
-
|
51
|
-
|
52
|
-
[key].flatten.map do |key|
|
51
|
+
def decorate_keys
|
52
|
+
@keys.map do |key|
|
53
53
|
next key unless key
|
54
54
|
|
55
55
|
decorated_key = "#{@options[:key_prefix]}#{key}#{@options[:key_suffix]}"
|
@@ -11,17 +11,25 @@ module TableStructure
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def name(header_context, _table_context)
|
14
|
-
schema.header(context: header_context)
|
14
|
+
@schema.header(context: header_context)
|
15
15
|
end
|
16
16
|
|
17
17
|
def key
|
18
|
-
|
19
|
-
.instance_variable_get(:@table_structure_schema_table_)
|
20
|
-
.send(:keys)
|
18
|
+
table.send(:keys)
|
21
19
|
end
|
22
20
|
|
23
21
|
def value(row_context, _table_context)
|
24
|
-
schema.row(context: row_context)
|
22
|
+
@schema.row(context: row_context)
|
23
|
+
end
|
24
|
+
|
25
|
+
def size
|
26
|
+
table.send(:size)
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def table
|
32
|
+
@schema.instance_variable_get(:@table_structure_schema_table_)
|
25
33
|
end
|
26
34
|
end
|
27
35
|
end
|
@@ -4,11 +4,12 @@ module TableStructure
|
|
4
4
|
module Schema
|
5
5
|
class Definition
|
6
6
|
class Error < ::TableStructure::Error
|
7
|
-
attr_reader :
|
7
|
+
attr_reader :schema_name, :definition_index
|
8
8
|
|
9
|
-
def initialize(error_message,
|
10
|
-
@
|
11
|
-
|
9
|
+
def initialize(error_message, schema_name, definition_index)
|
10
|
+
@schema_name = schema_name
|
11
|
+
@definition_index = definition_index
|
12
|
+
super("#{error_message} [#{schema_name}] defined position of column(s): #{definition_index + 1}")
|
12
13
|
end
|
13
14
|
end
|
14
15
|
end
|
@@ -6,26 +6,24 @@ module TableStructure
|
|
6
6
|
class Validator
|
7
7
|
DEFAULT_SIZE = 1
|
8
8
|
|
9
|
-
def initialize(index, options)
|
9
|
+
def initialize(name, index, options)
|
10
|
+
@name = name
|
10
11
|
@index = index
|
11
12
|
@options = options
|
12
13
|
end
|
13
14
|
|
14
15
|
def validate(name:, key:, size:, **)
|
15
16
|
if key.respond_to?(:call)
|
16
|
-
raise Error.new('"key" must not be lambda.', @index)
|
17
|
+
raise Error.new('"key" must not be lambda.', @name, @index)
|
17
18
|
end
|
18
19
|
if !key && name.respond_to?(:call) && !size
|
19
|
-
raise Error.new('"size" must be defined, because column size cannot be determined.', @index)
|
20
|
+
raise Error.new('"size" must be defined, because column size cannot be determined.', @name, @index)
|
20
21
|
end
|
21
22
|
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 defined when "result_type: :hash" is specified.', @index)
|
23
|
+
raise Error.new('"size" must be positive.', @name, @index)
|
26
24
|
end
|
27
25
|
if key && size && [key].flatten.size < size
|
28
|
-
raise Error.new('"key" size must be greater than or equal to specified "size".', @index)
|
26
|
+
raise Error.new('"key" size must be greater than or equal to specified "size".', @name, @index)
|
29
27
|
end
|
30
28
|
|
31
29
|
true
|
@@ -13,7 +13,8 @@ module TableStructure
|
|
13
13
|
|
14
14
|
DEFAULT_SIZE = 1
|
15
15
|
|
16
|
-
def initialize(definitions, options)
|
16
|
+
def initialize(name, definitions, options)
|
17
|
+
@name = name
|
17
18
|
@definitions = definitions
|
18
19
|
@options = options
|
19
20
|
end
|
@@ -22,7 +23,7 @@ module TableStructure
|
|
22
23
|
@definitions
|
23
24
|
.map { |definition| Utils.evaluate_callable(definition, context) }
|
24
25
|
.map.with_index do |definition, i|
|
25
|
-
validator = Validator.new(i, @options)
|
26
|
+
validator = Validator.new(@name, i, @options)
|
26
27
|
|
27
28
|
[definition]
|
28
29
|
.flatten
|
@@ -40,7 +41,7 @@ module TableStructure
|
|
40
41
|
elsif Utils.schema_class?(definition)
|
41
42
|
definition.new(context: context)
|
42
43
|
else
|
43
|
-
raise Error.new('Invalid definition.', i)
|
44
|
+
raise Error.new('Invalid definition.', @name, i)
|
44
45
|
end
|
45
46
|
end
|
46
47
|
end
|
@@ -4,13 +4,16 @@ module TableStructure
|
|
4
4
|
module Schema
|
5
5
|
class Table
|
6
6
|
RESULT_BUILDERS = {
|
7
|
-
hash:
|
7
|
+
hash: lambda { |values, keys, *|
|
8
|
+
keys.map.with_index { |key, i| [key || i, values[i]] }.to_h
|
9
|
+
}
|
8
10
|
}.freeze
|
9
11
|
|
10
12
|
attr_reader :columns, :column_converters, :result_builders, :options
|
11
13
|
|
12
|
-
def initialize(column_definitions, column_converters, result_builders, context, options)
|
13
|
-
@
|
14
|
+
def initialize(name, column_definitions, column_converters, result_builders, context, options)
|
15
|
+
@name = name
|
16
|
+
@columns = build_columns(name, column_definitions, context, options)
|
14
17
|
@column_converters = column_converters
|
15
18
|
@result_builders = result_builders
|
16
19
|
@context = context
|
@@ -29,11 +32,15 @@ module TableStructure
|
|
29
32
|
@keys ||= @columns.map(&:key).flatten
|
30
33
|
end
|
31
34
|
|
35
|
+
def size
|
36
|
+
@size ||= @columns.map(&:size).reduce(0) { |memo, size| memo + size }
|
37
|
+
end
|
38
|
+
|
32
39
|
private
|
33
40
|
|
34
|
-
def build_columns(definitions, context, options)
|
41
|
+
def build_columns(name, definitions, context, options)
|
35
42
|
Definition
|
36
|
-
.new(definitions, options)
|
43
|
+
.new(name, definitions, options)
|
37
44
|
.compile(context)
|
38
45
|
.map { |definition| Column.create(definition, options) }
|
39
46
|
end
|
@@ -7,15 +7,6 @@ module TableStructure
|
|
7
7
|
val.respond_to?(:call) ? val.call(*params) : val
|
8
8
|
end
|
9
9
|
|
10
|
-
def self.concat_key(key, prefix, suffix)
|
11
|
-
case key
|
12
|
-
when Symbol
|
13
|
-
"#{prefix}#{key}#{suffix}".to_sym
|
14
|
-
else
|
15
|
-
"#{prefix}#{key}#{suffix}"
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
10
|
def self.schema_class?(val)
|
20
11
|
val.is_a?(Class) &&
|
21
12
|
val.included_modules.include?(::TableStructure::Schema)
|
@@ -16,7 +16,7 @@ module TableStructure
|
|
16
16
|
key_suffix: nil
|
17
17
|
}.freeze
|
18
18
|
|
19
|
-
def initialize(context: nil, **options)
|
19
|
+
def initialize(context: nil, name: self.class.name, **options)
|
20
20
|
column_definitions = self.class.column_definitions
|
21
21
|
column_converters = self.class.column_converters
|
22
22
|
result_builders = self.class.result_builders
|
@@ -24,6 +24,7 @@ module TableStructure
|
|
24
24
|
options = DEFAULT_OPTIONS.merge(self.class.options).merge(options)
|
25
25
|
@table_structure_schema_table_ =
|
26
26
|
Table.new(
|
27
|
+
name,
|
27
28
|
column_definitions,
|
28
29
|
column_converters,
|
29
30
|
result_builders,
|
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.
|
4
|
+
version: 0.3.2
|
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
|
+
date: 2019-09-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|