table_structure 0.1.4 → 0.2.0
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 -1
- data/README.md +72 -0
- data/lib/table_structure.rb +1 -0
- data/lib/table_structure/iterator.rb +21 -0
- data/lib/table_structure/version.rb +1 -1
- 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: e3c3fdd974ce55df18db48df989c744dd8bc13b28ec4c754d01a815cfde31b93
|
4
|
+
data.tar.gz: 624360f1775a9c3f7be02453c347adb55870b656eb0e79e6f4cb9c0c84037939
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 044f11d274989855569f1931d98555e7f7cd153e1bc3fe271685860e310b40065bdbff5e87f576025f5401a5db9bb67f0f082d7a0798b2d8e83e040e717cbb1e
|
7
|
+
data.tar.gz: 110bd8d5cf2cfb215cee9ca0806ec6cbc0ee3074a7b4c944cd1bce0ecc7e031b567e908fe9ff1b3acaed10aaae8ebee44e82f200cf0922b8effae15d4c541608
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -106,6 +106,78 @@ response.headers['Content-Disposition'] = 'attachment; filename="sample.csv"'
|
|
106
106
|
response_body = Enumerator.new { |y| writer.write(items, to: CSV.new(y)) }
|
107
107
|
```
|
108
108
|
|
109
|
+
#### TableStructure::Iterator
|
110
|
+
Specifying `result_type: :hash` option of `TableStructure::Schema` works well.
|
111
|
+
To use this option, define `column(s)` with `:key`.
|
112
|
+
```ruby
|
113
|
+
class SampleTableSchema
|
114
|
+
include TableStructure::Schema
|
115
|
+
|
116
|
+
# If header is required, :name must also be defined.
|
117
|
+
column key: :id,
|
118
|
+
value: ->(row, *) { row[:id] }
|
119
|
+
|
120
|
+
column key: :name,
|
121
|
+
value: ->(row, *) { row[:name] }
|
122
|
+
|
123
|
+
columns key: %i[pet1 pet2 pet3],
|
124
|
+
value: ->(row, *) { row[:pets] }
|
125
|
+
|
126
|
+
columns ->(table) {
|
127
|
+
table[:questions].map do |question|
|
128
|
+
{
|
129
|
+
key: question[:id].downcase.to_sym,
|
130
|
+
value: ->(row, *) { row[:answers][question[:id]] }
|
131
|
+
}
|
132
|
+
end
|
133
|
+
}
|
134
|
+
|
135
|
+
column_converter :to_s, ->(val, *) { val.to_s }
|
136
|
+
end
|
137
|
+
|
138
|
+
context = {
|
139
|
+
questions: [
|
140
|
+
{ id: 'Q1', text: 'Do you like sushi?' },
|
141
|
+
{ id: 'Q2', text: 'Do you like yakiniku?' },
|
142
|
+
{ id: 'Q3', text: 'Do you like ramen?' }
|
143
|
+
]
|
144
|
+
}
|
145
|
+
|
146
|
+
schema = SampleTableSchema.new(context: context, result_type: :hash) # default is :array
|
147
|
+
iterator = TableStructure::Iterator.new(schema, header_omitted: true)
|
148
|
+
## or
|
149
|
+
# writer = TableStructure::Writer.new(schema, header_omitted: true)
|
150
|
+
# iterator = TableStructure::Iterator.new(writer)
|
151
|
+
|
152
|
+
items = [
|
153
|
+
{
|
154
|
+
id: 1,
|
155
|
+
name: 'Taro',
|
156
|
+
pets: ['🐱', '🐶'],
|
157
|
+
answers: { 'Q1' => '⭕️', 'Q2' => '❌', 'Q3' => '⭕️' }
|
158
|
+
},
|
159
|
+
{
|
160
|
+
id: 2,
|
161
|
+
name: 'Hanako',
|
162
|
+
pets: ['🐇', '🐢', '🐿', '🦒'],
|
163
|
+
answers: { 'Q1' => '⭕️', 'Q2' => '⭕️', 'Q3' => '❌' }
|
164
|
+
}
|
165
|
+
]
|
166
|
+
|
167
|
+
enum = iterator.iterate(items)
|
168
|
+
|
169
|
+
## Enumerator methods is available
|
170
|
+
enum.each do |item|
|
171
|
+
# ...
|
172
|
+
end
|
173
|
+
|
174
|
+
enum.map(&:itself)
|
175
|
+
# => [{:id=>"1", :name=>"Taro", :pet1=>"🐱", :pet2=>"🐶", :pet3=>"", :q1=>"⭕️", :q2=>"❌", :q3=>"⭕️"}, {:id=>"2", :name=>"Hanako", :pet1=>"🐇", :pet2=>"🐢", :pet3=>"🐿", :q1=>"⭕️", :q2=>"⭕️", :q3=>"❌"}]
|
176
|
+
|
177
|
+
enum.lazy.select { |item| item[:q1] == '⭕️' }.take(1).force
|
178
|
+
# => [{:id=>"1", :name=>"Taro", :pet1=>"🐱", :pet2=>"🐶", :pet3=>"", :q1=>"⭕️", :q2=>"❌", :q3=>"⭕️"}]
|
179
|
+
```
|
180
|
+
|
109
181
|
### Advanced
|
110
182
|
|
111
183
|
You can also use `context_builder`.
|
data/lib/table_structure.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TableStructure
|
4
|
+
class Iterator
|
5
|
+
def initialize(schema_or_writer, **options)
|
6
|
+
case
|
7
|
+
when schema_or_writer.is_a?(Schema)
|
8
|
+
schema = schema_or_writer
|
9
|
+
@writer = Writer.new(schema, options)
|
10
|
+
when schema_or_writer.is_a?(Writer)
|
11
|
+
@writer = schema_or_writer
|
12
|
+
else
|
13
|
+
raise ::TableStructure::Error.new('First argument must be either Schema or Writer.')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def iterate(items)
|
18
|
+
Enumerator.new { |y| @writer.write(items, to: y) }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
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.
|
4
|
+
version: 0.2.0
|
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-22 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/iterator.rb
|
74
75
|
- lib/table_structure/schema.rb
|
75
76
|
- lib/table_structure/schema/column.rb
|
76
77
|
- lib/table_structure/schema/definition.rb
|