table_structure 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +1 -1
- data/README.md +4 -4
- data/lib/table_structure/iterator.rb +2 -2
- data/lib/table_structure/schema/definition.rb +4 -3
- data/lib/table_structure/schema/table.rb +29 -30
- data/lib/table_structure/schema.rb +9 -5
- data/lib/table_structure/version.rb +1 -1
- data/lib/table_structure/writer.rb +9 -2
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f880b928161f99c6d4346f06304cb9cfe4a1c44f8ffb307243a70d74ec5e400d
|
4
|
+
data.tar.gz: 9d1a30376429abe0906e3e0879eb230e4daf51c5c7de87cf28b63673433df83e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b5ee4c95815cb891a25ac377ec72359d70394da3ea1aaf58ea71f77618ffaaa8a5443fff95366ab0ca8ecccd871aa33bafbbe134ccb8109645146392dcbc4fa
|
7
|
+
data.tar.gz: 422e6db13c3940bdf873535c1f02d3f32211cca6a81ced5d1aebf678504f14daeda50c8de46d47762be33fcad79f90cb924d6d175a3a19c2029406d02d5a5a43
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
# 0.3.1
|
2
|
+
Changes:
|
3
|
+
- `TableStructure::Schema`
|
4
|
+
- Make `:result_type` option available for `TableStructure::Write` and `TableStructure::Iterator`.
|
5
|
+
- `:result_type` option for `TableStructure::Schema` is deprecated.
|
6
|
+
|
1
7
|
# 0.3.0
|
2
8
|
Changes:
|
3
9
|
- `TableStructure::Schema`
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -147,10 +147,10 @@ context = {
|
|
147
147
|
]
|
148
148
|
}
|
149
149
|
|
150
|
-
schema = SampleTableSchema.new(context: context
|
151
|
-
iterator = TableStructure::Iterator.new(schema, header_omitted: true)
|
150
|
+
schema = SampleTableSchema.new(context: context)
|
151
|
+
iterator = TableStructure::Iterator.new(schema, result_type: :hash, header_omitted: true)
|
152
152
|
## or
|
153
|
-
# writer = TableStructure::Writer.new(schema, header_omitted: true)
|
153
|
+
# writer = TableStructure::Writer.new(schema, result_type: :hash, header_omitted: true)
|
154
154
|
# iterator = TableStructure::Iterator.new(writer)
|
155
155
|
|
156
156
|
items = [
|
@@ -293,7 +293,7 @@ class SampleTableSchema
|
|
293
293
|
end
|
294
294
|
```
|
295
295
|
|
296
|
-
If you want to convert CSV character code, you do so within block of `write` method.
|
296
|
+
If you want to convert CSV character code, you can do so within block of `write` method.
|
297
297
|
```ruby
|
298
298
|
File.open('sample.csv', 'w') do |f|
|
299
299
|
writer.write(items, to: CSV.new(f)) do |row_values|
|
@@ -37,9 +37,10 @@ module TableStructure
|
|
37
37
|
definition
|
38
38
|
elsif Utils.schema_instance?(definition)
|
39
39
|
definition
|
40
|
-
|
41
|
-
|
42
|
-
|
40
|
+
elsif Utils.schema_class?(definition)
|
41
|
+
definition.new(context: context)
|
42
|
+
else
|
43
|
+
raise Error.new('Invalid definition.', i)
|
43
44
|
end
|
44
45
|
end
|
45
46
|
end
|
@@ -3,26 +3,30 @@
|
|
3
3
|
module TableStructure
|
4
4
|
module Schema
|
5
5
|
class Table
|
6
|
-
|
6
|
+
RESULT_BUILDERS = {
|
7
|
+
hash: ->(values, keys, *) { keys.zip(values).to_h }
|
8
|
+
}.freeze
|
9
|
+
|
10
|
+
attr_reader :columns, :column_converters, :result_builders, :options
|
7
11
|
|
8
12
|
def initialize(column_definitions, column_converters, result_builders, context, options)
|
9
13
|
@columns = build_columns(column_definitions, context, options)
|
10
|
-
@column_converters =
|
11
|
-
@result_builders =
|
14
|
+
@column_converters = column_converters
|
15
|
+
@result_builders = result_builders
|
12
16
|
@context = context
|
13
17
|
@options = options
|
14
18
|
end
|
15
19
|
|
16
|
-
def header_values(context)
|
17
|
-
values(:name, context)
|
20
|
+
def header_values(context, result_type = nil) # TODO
|
21
|
+
values(:name, result_type, context)
|
18
22
|
end
|
19
23
|
|
20
|
-
def row_values(context)
|
21
|
-
values(:value, context)
|
24
|
+
def row_values(context, result_type = nil) # TODO
|
25
|
+
values(:value, result_type, context)
|
22
26
|
end
|
23
27
|
|
24
28
|
def keys
|
25
|
-
@columns.map(&:key).flatten
|
29
|
+
@keys ||= @columns.map(&:key).flatten
|
26
30
|
end
|
27
31
|
|
28
32
|
private
|
@@ -34,28 +38,23 @@ module TableStructure
|
|
34
38
|
.map { |definition| Column.create(definition, options) }
|
35
39
|
end
|
36
40
|
|
37
|
-
def
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
reduce_callables(@result_builders, columns, context)
|
55
|
-
end
|
56
|
-
|
57
|
-
def reduce_callables(callables, val, context)
|
58
|
-
callables.reduce(val) { |val, (_, callable)| callable.call(val, context, @context) }
|
41
|
+
def values(method, result_type, context)
|
42
|
+
columns =
|
43
|
+
@columns
|
44
|
+
.map { |column| column.send(method, context, @context) }
|
45
|
+
.flatten
|
46
|
+
.map do |val|
|
47
|
+
@column_converters.reduce(val) do |val, (_, column_converter)|
|
48
|
+
column_converter.call(val, context, @context)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
RESULT_BUILDERS
|
53
|
+
.select { |k, _v| k == result_type }
|
54
|
+
.merge(@result_builders)
|
55
|
+
.reduce(columns) do |columns, (_, result_builder)|
|
56
|
+
result_builder.call(columns, keys, context, @context)
|
57
|
+
end
|
59
58
|
end
|
60
59
|
end
|
61
60
|
end
|
@@ -11,7 +11,7 @@ module TableStructure
|
|
11
11
|
end
|
12
12
|
|
13
13
|
DEFAULT_OPTIONS = {
|
14
|
-
result_type: :array,
|
14
|
+
result_type: :array, # deprecated: Change to pass as argument of method.
|
15
15
|
key_prefix: nil,
|
16
16
|
key_suffix: nil
|
17
17
|
}.freeze
|
@@ -32,14 +32,18 @@ module TableStructure
|
|
32
32
|
)
|
33
33
|
end
|
34
34
|
|
35
|
-
def header(context: nil)
|
35
|
+
def header(context: nil, result_type: nil)
|
36
|
+
# TODO
|
37
|
+
result_type ||= @table_structure_schema_table_.options[:result_type]
|
36
38
|
context = self.class.context_builders[:header].call(context)
|
37
|
-
@table_structure_schema_table_.header_values(context)
|
39
|
+
@table_structure_schema_table_.header_values(context, result_type)
|
38
40
|
end
|
39
41
|
|
40
|
-
def row(context: nil)
|
42
|
+
def row(context: nil, result_type: nil)
|
43
|
+
# TODO
|
44
|
+
result_type ||= @table_structure_schema_table_.options[:result_type]
|
41
45
|
context = self.class.context_builders[:row].call(context)
|
42
|
-
@table_structure_schema_table_.row_values(context)
|
46
|
+
@table_structure_schema_table_.row_values(context, result_type)
|
43
47
|
end
|
44
48
|
|
45
49
|
def column_converters
|
@@ -5,6 +5,7 @@ module TableStructure
|
|
5
5
|
DEFAULT_OPTIONS = {
|
6
6
|
header_omitted: false,
|
7
7
|
header_context: nil,
|
8
|
+
result_type: nil, # TODO: :array
|
8
9
|
method: :<<
|
9
10
|
}.freeze
|
10
11
|
|
@@ -16,12 +17,18 @@ module TableStructure
|
|
16
17
|
def write(items, to:, **options)
|
17
18
|
options = @options.merge(options)
|
18
19
|
unless options[:header_omitted]
|
19
|
-
header = @schema.header(
|
20
|
+
header = @schema.header(
|
21
|
+
context: options[:header_context],
|
22
|
+
result_type: options[:result_type]
|
23
|
+
)
|
20
24
|
header = yield header if block_given?
|
21
25
|
to.send(options[:method], header)
|
22
26
|
end
|
23
27
|
enumerize(items).each do |item|
|
24
|
-
row = @schema.row(
|
28
|
+
row = @schema.row(
|
29
|
+
context: item,
|
30
|
+
result_type: options[:result_type]
|
31
|
+
)
|
25
32
|
row = yield row if block_given?
|
26
33
|
to.send(options[:method], row)
|
27
34
|
end
|
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.1
|
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-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -108,8 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
108
|
- !ruby/object:Gem::Version
|
109
109
|
version: '0'
|
110
110
|
requirements: []
|
111
|
-
|
112
|
-
rubygems_version: 2.7.6.2
|
111
|
+
rubygems_version: 3.0.3
|
113
112
|
signing_key:
|
114
113
|
specification_version: 4
|
115
114
|
summary: Creates and outputs table structured data.
|