table_structure 0.3.8 → 0.3.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +12 -2
- data/lib/table_structure/csv/writer.rb +32 -0
- data/lib/table_structure/schema/definition/compiler.rb +6 -0
- data/lib/table_structure/schema/definition/validator.rb +1 -1
- data/lib/table_structure/schema/table.rb +1 -0
- data/lib/table_structure/version.rb +1 -1
- data/lib/table_structure.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 15634b3115faa6adf71706af3ee278aa08b196408e4bcf895de38d1dd9401dff
|
4
|
+
data.tar.gz: 678ca7958dbb6edfea148639c4e9ec51e336226112f6e7f5d9ca65a511cadf41
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 605978ebfae00094056ad2dd53e4ff3c26a4caf3575b8efcf54545998ea36bcfc5cc93eb779c7bc43df277f562337cf6b22b381b392e2552d32636e7b65e0459
|
7
|
+
data.tar.gz: 99f37c9c2d3b3feaed7332c0c9a4e6d76c3eb3f3960ccced2b2db13abd973f3f68914ce26465f36df72da5a200005bec8a644bd6216f9ce5c1b3c629a92f1a4e
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -80,7 +80,7 @@ writer = TableStructure::Writer.new(schema)
|
|
80
80
|
# writer = TableStructure::Writer.new(schema, header_omitted: true)
|
81
81
|
```
|
82
82
|
|
83
|
-
Writes the items converted by the schema to array
|
83
|
+
Writes the items converted by the schema to array:
|
84
84
|
```ruby
|
85
85
|
items = [
|
86
86
|
{
|
@@ -98,7 +98,9 @@ items = [
|
|
98
98
|
]
|
99
99
|
|
100
100
|
## When using `find_each` method of Rails
|
101
|
-
# items =
|
101
|
+
# items = Enumerator.new { |y| Item.find_each { |item| y << item } }
|
102
|
+
## or
|
103
|
+
# items = ->(y) { Item.find_each { |item| y << item } }
|
102
104
|
|
103
105
|
table = []
|
104
106
|
writer.write(items, to: table)
|
@@ -127,6 +129,14 @@ end
|
|
127
129
|
```
|
128
130
|
[Sample with docker](https://github.com/jsmmr/ruby_table_structure_sample)
|
129
131
|
|
132
|
+
You can also use `TableStructure::CSV::Writer` instead:
|
133
|
+
```ruby
|
134
|
+
writer = TableStructure::CSV::Writer.new(schema)
|
135
|
+
File.open('sample.csv', 'w') do |f|
|
136
|
+
writer.write(items, to: f, bom: true)
|
137
|
+
end
|
138
|
+
```
|
139
|
+
|
130
140
|
#### TableStructure::Iterator
|
131
141
|
Specifying `result_type: :hash` option works well.
|
132
142
|
To use this option, define `column(s)` with `:key`.
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TableStructure
|
4
|
+
module CSV
|
5
|
+
class Writer
|
6
|
+
DEFAULT_OPTIONS = {
|
7
|
+
bom: false,
|
8
|
+
header_omitted: false,
|
9
|
+
header_context: nil
|
10
|
+
}.freeze
|
11
|
+
|
12
|
+
FIXED_OPTIONS = {
|
13
|
+
result_type: :array,
|
14
|
+
method: :<<
|
15
|
+
}.freeze
|
16
|
+
|
17
|
+
BOM = "\uFEFF"
|
18
|
+
|
19
|
+
def initialize(schema, **options)
|
20
|
+
require 'csv'
|
21
|
+
@options = DEFAULT_OPTIONS.merge(options).merge(FIXED_OPTIONS)
|
22
|
+
@writer = ::TableStructure::Writer.new(schema, **@options)
|
23
|
+
end
|
24
|
+
|
25
|
+
def write(items, to:, **options)
|
26
|
+
options = @options.merge(options).merge(FIXED_OPTIONS)
|
27
|
+
to.send(options[:method], BOM) if options[:bom]
|
28
|
+
@writer.write(items, to: ::CSV.new(to), **options)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -34,6 +34,7 @@ module TableStructure
|
|
34
34
|
omitted = definition.delete(:omitted)
|
35
35
|
next if Utils.evaluate_callable(omitted, context)
|
36
36
|
|
37
|
+
definition = evaluate_attrs(definition, context)
|
37
38
|
validator.validate(definition)
|
38
39
|
definition[:size] = determine_size(definition)
|
39
40
|
definition
|
@@ -54,6 +55,11 @@ module TableStructure
|
|
54
55
|
|
55
56
|
private
|
56
57
|
|
58
|
+
def evaluate_attrs(definition, context)
|
59
|
+
size = Utils.evaluate_callable(definition[:size], context)
|
60
|
+
definition.merge(size: size)
|
61
|
+
end
|
62
|
+
|
57
63
|
def determine_size(name:, key:, size:, **)
|
58
64
|
return size if size
|
59
65
|
|
@@ -23,7 +23,7 @@ module TableStructure
|
|
23
23
|
raise Error.new('"size" must be positive.', @name, @index)
|
24
24
|
end
|
25
25
|
if key && size && [key].flatten.size < size
|
26
|
-
raise Error.new('"key" size must be
|
26
|
+
raise Error.new('"key" size must not be less than specified "size".', @name, @index)
|
27
27
|
end
|
28
28
|
|
29
29
|
true
|
data/lib/table_structure.rb
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.3.
|
4
|
+
version: 0.3.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jsmmr
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-10-
|
11
|
+
date: 2019-10-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- bin/console
|
72
72
|
- bin/setup
|
73
73
|
- lib/table_structure.rb
|
74
|
+
- lib/table_structure/csv/writer.rb
|
74
75
|
- lib/table_structure/iterator.rb
|
75
76
|
- lib/table_structure/schema.rb
|
76
77
|
- lib/table_structure/schema/column.rb
|