table_structure 0.1.1 → 0.1.2
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/.travis.yml +1 -0
- data/Gemfile.lock +1 -1
- data/README.md +6 -7
- data/lib/table_structure/schema/column.rb +42 -16
- data/lib/table_structure/schema/table.rb +10 -6
- data/lib/table_structure/version.rb +1 -1
- data/lib/table_structure.rb +2 -1
- data/table_structure.gemspec +1 -1
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f308e4cc6c48b759e040421bb73d27582bf63555bb3a7e8cffbe9d881409873e
|
4
|
+
data.tar.gz: e7661a42884ac86bd894e9fd9e542433b31627355d71812d525b9e0a66bdc6d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f042133eac79798923ac099a8108d29a9bfd43e36fe68d4811f2074ea9da9e638174e0381ac8fefcaeeea71d8e29a1504a439799e9ac5638d1b45fc040239a6
|
7
|
+
data.tar.gz: 9578b9cd2478a440fd3b8a0b6e84ade031cd0b298ae11e7766c39529e3b795340005bd20fade907cd961879da318166db958818c0b3a38284fd0b450f13a98f0
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -5,9 +5,6 @@
|
|
5
5
|
`TableStructure` has two major functions.
|
6
6
|
The functions are `TableStructure::Schema` that defines the schema of a table using DSL and ` TableStructure::Writer` that converts and outputs data with that schema.
|
7
7
|
|
8
|
-
`TableStructure::Writer` outputs the converted data line by line.
|
9
|
-
This keeps the memory usage constant and is suitable for large-scale CSV output.
|
10
|
-
|
11
8
|
## Installation
|
12
9
|
|
13
10
|
Add this line to your application's Gemfile:
|
@@ -68,6 +65,8 @@ schema = SampleTableSchema.new(context: context)
|
|
68
65
|
#### TableStructure::Writer
|
69
66
|
```ruby
|
70
67
|
writer = TableStructure::Writer.new(schema)
|
68
|
+
## When omitting header line
|
69
|
+
# writer = TableStructure::Writer.new(schema, header_omitted: true)
|
71
70
|
|
72
71
|
items = [
|
73
72
|
{
|
@@ -117,11 +116,11 @@ class SampleTableSchema
|
|
117
116
|
|
118
117
|
TableContext = Struct.new(:questions, keyword_init: true)
|
119
118
|
|
120
|
-
RowContext = Struct.new(:id, :name, :pets, :answers, keyword_init: true)
|
119
|
+
RowContext = Struct.new(:id, :name, :pets, :answers, keyword_init: true) do
|
121
120
|
def more_pets
|
122
121
|
pets + pets
|
123
122
|
end
|
124
|
-
|
123
|
+
end
|
125
124
|
|
126
125
|
context_builder :table, ->(context) { TableContext.new(**context) }
|
127
126
|
context_builder :row, ->(context) { RowContext.new(**context) }
|
@@ -135,14 +134,14 @@ class SampleTableSchema
|
|
135
134
|
columns name: ['Pet 1', 'Pet 2', 'Pet 3'],
|
136
135
|
value: ->(row, *) { row.more_pets }
|
137
136
|
|
138
|
-
columns ->(table)
|
137
|
+
columns ->(table) do
|
139
138
|
table.questions.map do |question|
|
140
139
|
{
|
141
140
|
name: question[:id],
|
142
141
|
value: ->(row, *) { row.answers[question[:id]] }
|
143
142
|
}
|
144
143
|
end
|
145
|
-
|
144
|
+
end
|
146
145
|
|
147
146
|
column_converter :to_s, ->(val, *) { val.to_s }
|
148
147
|
end
|
@@ -2,13 +2,34 @@ module TableStructure
|
|
2
2
|
module Schema
|
3
3
|
class Column
|
4
4
|
|
5
|
+
class Error < ::TableStructure::Error
|
6
|
+
attr_reader :group_index
|
7
|
+
|
8
|
+
def initialize(error_message, group_index)
|
9
|
+
@group_index = group_index
|
10
|
+
super("#{error_message} [defined position: #{group_index + 1}]")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
DEFAULT_DEFINITION = {
|
15
|
+
name: nil,
|
16
|
+
key: nil,
|
17
|
+
value: nil,
|
18
|
+
size: nil
|
19
|
+
}
|
20
|
+
|
5
21
|
DEFAULT_SIZE = 1
|
6
22
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
@
|
11
|
-
|
23
|
+
attr_reader :size, :group_index
|
24
|
+
|
25
|
+
def initialize(definition, group_index)
|
26
|
+
@group_index = group_index
|
27
|
+
definition = DEFAULT_DEFINITION.merge(definition)
|
28
|
+
validate(definition)
|
29
|
+
@name = definition[:name]
|
30
|
+
@key = definition[:key]
|
31
|
+
@value = definition[:value]
|
32
|
+
@size = determine_size(definition)
|
12
33
|
end
|
13
34
|
|
14
35
|
def name(header_context, table_context)
|
@@ -17,7 +38,7 @@ module TableStructure
|
|
17
38
|
end
|
18
39
|
|
19
40
|
def key
|
20
|
-
@key
|
41
|
+
optimize_size(@key)
|
21
42
|
end
|
22
43
|
|
23
44
|
def value(row_context, table_context)
|
@@ -27,18 +48,23 @@ module TableStructure
|
|
27
48
|
|
28
49
|
private
|
29
50
|
|
30
|
-
def
|
31
|
-
if
|
32
|
-
raise
|
51
|
+
def validate(name:, key:, size:, **)
|
52
|
+
if !key && name.respond_to?(:call) && !size
|
53
|
+
raise Error.new('"size" must be specified, because column size cannot be determined.', @group_index)
|
33
54
|
end
|
34
|
-
if
|
35
|
-
|
36
|
-
raise ::TableStructure::Error.new('"size" must be positive.')
|
37
|
-
end
|
38
|
-
return specified_size
|
55
|
+
if size && size < DEFAULT_SIZE
|
56
|
+
raise Error.new('"size" must be positive.', @group_index)
|
39
57
|
end
|
40
|
-
|
41
|
-
|
58
|
+
end
|
59
|
+
|
60
|
+
def determine_size(name:, key:, size:, **)
|
61
|
+
return size if size
|
62
|
+
[calculate_size(name), calculate_size(key)].max
|
63
|
+
end
|
64
|
+
|
65
|
+
def calculate_size(val)
|
66
|
+
if val.kind_of?(Array)
|
67
|
+
return val.empty? ? DEFAULT_SIZE : val.size
|
42
68
|
end
|
43
69
|
DEFAULT_SIZE
|
44
70
|
end
|
@@ -4,7 +4,7 @@ module TableStructure
|
|
4
4
|
|
5
5
|
DEFAULT_OPTIONS = { result_type: :array }
|
6
6
|
|
7
|
-
attr_reader :column_converters, :result_builders
|
7
|
+
attr_reader :columns, :column_converters, :result_builders
|
8
8
|
|
9
9
|
def initialize(column_definitions, column_converters, result_builders, context, options)
|
10
10
|
@context = context
|
@@ -24,11 +24,15 @@ module TableStructure
|
|
24
24
|
|
25
25
|
private
|
26
26
|
|
27
|
-
def build_columns(
|
28
|
-
|
29
|
-
.map { |
|
30
|
-
.
|
31
|
-
|
27
|
+
def build_columns(definitions)
|
28
|
+
definitions
|
29
|
+
.map { |definition| Utils.evaluate_callable(definition, @context) }
|
30
|
+
.map.with_index do |definition, i|
|
31
|
+
[definition]
|
32
|
+
.flatten
|
33
|
+
.map { |definition| Column.new(definition, i) }
|
34
|
+
end
|
35
|
+
.flatten
|
32
36
|
end
|
33
37
|
|
34
38
|
def default_column_converters
|
data/lib/table_structure.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
module TableStructure
|
2
|
+
class Error < StandardError; end
|
3
|
+
|
2
4
|
require 'table_structure/version'
|
3
5
|
|
4
6
|
require 'table_structure/schema'
|
@@ -11,5 +13,4 @@ module TableStructure
|
|
11
13
|
require 'table_structure/schema/utils'
|
12
14
|
require 'table_structure/writer'
|
13
15
|
|
14
|
-
class Error < StandardError; end
|
15
16
|
end
|
data/table_structure.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["jsmmr@icloud.com"]
|
11
11
|
|
12
12
|
spec.summary = %q{Create and output table structure data.}
|
13
|
-
spec.description = %q{This gem creates and outputs table structure data. Useful for
|
13
|
+
spec.description = %q{This gem creates and outputs table structure data. Useful for creating CSV.}
|
14
14
|
spec.homepage = "https://github.com/jsmmr/ruby_table_structure"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
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.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-08-
|
11
|
+
date: 2019-08-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,8 +52,8 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
-
description: This gem creates and outputs table structure data. Useful for
|
56
|
-
|
55
|
+
description: This gem creates and outputs table structure data. Useful for creating
|
56
|
+
CSV.
|
57
57
|
email:
|
58
58
|
- jsmmr@icloud.com
|
59
59
|
executables: []
|
@@ -101,7 +101,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
101
101
|
- !ruby/object:Gem::Version
|
102
102
|
version: '0'
|
103
103
|
requirements: []
|
104
|
-
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.7.6.2
|
105
106
|
signing_key:
|
106
107
|
specification_version: 4
|
107
108
|
summary: Create and output table structure data.
|