table_structure 0.3.12 → 0.3.13
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/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +50 -10
- data/lib/table_structure/schema.rb +6 -4
- data/lib/table_structure/schema/definition.rb +5 -1
- data/lib/table_structure/schema/table.rb +6 -0
- data/lib/table_structure/version.rb +1 -1
- data/lib/table_structure/writer.rb +12 -12
- 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: 5e9bf971b78fd68cbce2023d18033fcd397b1be4de329a5ba616df1c77baa62c
|
4
|
+
data.tar.gz: ad9e3d5a67d51239038d7fd711602fc4b518188adfd5fd5d1b1a67c3c3e703cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5f93b81fcff429a3899be7ce8e6a0b67bd638cfefea73e425097a08fcc5ddccee031c4f9c112c491cd4cf2dc1e761e3deaefbc1ba12bbab80a5b6ad37c88946
|
7
|
+
data.tar.gz: 36b8fa006be7530c21c747dc4c931e662161f60eac6e3c8b8a5dabe429c5bd5ce7bd8837dee05311fcba9f7b8e95fd4305be798e71ed38f5a512b459d3d535d8
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -98,6 +98,7 @@ items = [
|
|
98
98
|
]
|
99
99
|
|
100
100
|
## When using `find_each` method of Rails
|
101
|
+
# items = Item.enum_for(:find_each)
|
101
102
|
# items = Enumerator.new { |y| Item.find_each { |item| y << item } }
|
102
103
|
|
103
104
|
table = []
|
@@ -183,9 +184,6 @@ context = {
|
|
183
184
|
|
184
185
|
schema = SampleTableSchema.new(context: context)
|
185
186
|
iterator = TableStructure::Iterator.new(schema, result_type: :hash, header_omitted: true)
|
186
|
-
## or
|
187
|
-
# writer = TableStructure::Writer.new(schema, result_type: :hash, header_omitted: true)
|
188
|
-
# iterator = TableStructure::Iterator.new(writer)
|
189
187
|
```
|
190
188
|
|
191
189
|
Enumerate the items converted by the schema:
|
@@ -242,6 +240,33 @@ context = { admin: true }
|
|
242
240
|
schema = SampleTableSchema.new(context: context)
|
243
241
|
```
|
244
242
|
|
243
|
+
You can also omit columns by specifying `nil_definitions_ignored: true`.
|
244
|
+
If this option is set to `true` and `column(s)` difinition returns `nil`, the difinition is ignored.
|
245
|
+
```ruby
|
246
|
+
class SampleTableSchema
|
247
|
+
include TableStructure::Schema
|
248
|
+
|
249
|
+
column name: 'ID',
|
250
|
+
value: ->(row, *) { row[:id] }
|
251
|
+
|
252
|
+
column name: 'Name',
|
253
|
+
value: ->(row, *) { row[:name] }
|
254
|
+
|
255
|
+
columns ->(table) {
|
256
|
+
if table[:pet_num].positive?
|
257
|
+
{
|
258
|
+
name: (1..table[:pet_num]).map { |num| "Pet #{num}" },
|
259
|
+
value: ->(row, *) { row[:pets] }
|
260
|
+
}
|
261
|
+
end
|
262
|
+
}
|
263
|
+
end
|
264
|
+
|
265
|
+
context = { pet_num: 0 }
|
266
|
+
|
267
|
+
schema = SampleTableSchema.new(context: context, nil_definitions_ignored: true)
|
268
|
+
```
|
269
|
+
|
245
270
|
You can also nest schemas.
|
246
271
|
```ruby
|
247
272
|
class PetTableSchema
|
@@ -340,13 +365,28 @@ end
|
|
340
365
|
```
|
341
366
|
|
342
367
|
You can also use only `TableStructure::Schema`.
|
343
|
-
```
|
344
|
-
schema
|
345
|
-
table
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
368
|
+
```erb
|
369
|
+
<% @schema.create_table(result_type: :array) do |table| %>
|
370
|
+
<table>
|
371
|
+
<thead>
|
372
|
+
<tr>
|
373
|
+
<% table.header.each do |val| %>
|
374
|
+
<th><%= val %></th>
|
375
|
+
<% end %>
|
376
|
+
</tr>
|
377
|
+
</thead>
|
378
|
+
|
379
|
+
<tbody>
|
380
|
+
<% table.rows(@items).each do |row| %>
|
381
|
+
<tr>
|
382
|
+
<% row.each do |val| %>
|
383
|
+
<td><%= val %></td>
|
384
|
+
<% end %>
|
385
|
+
</tr>
|
386
|
+
<% end %>
|
387
|
+
</tbody>
|
388
|
+
</table>
|
389
|
+
<% end %>
|
350
390
|
```
|
351
391
|
|
352
392
|
## Contributing
|
@@ -1,7 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'forwardable'
|
4
|
+
|
3
5
|
module TableStructure
|
4
6
|
module Schema
|
7
|
+
extend Forwardable
|
8
|
+
|
5
9
|
def self.included(klass)
|
6
10
|
klass.extend(DSL::ColumnConverter)
|
7
11
|
klass.extend(DSL::ColumnDefinition)
|
@@ -18,6 +22,8 @@ module TableStructure
|
|
18
22
|
nil_definitions_ignored: false
|
19
23
|
}.freeze
|
20
24
|
|
25
|
+
def_delegators :@table_structure_schema_definition_, :create_table
|
26
|
+
|
21
27
|
def initialize(context: nil, name: self.class.name, **options)
|
22
28
|
column_definitions = [].concat(self.class.column_definitions)
|
23
29
|
context_builders = {}.merge!(self.class.context_builders)
|
@@ -35,9 +41,5 @@ module TableStructure
|
|
35
41
|
options
|
36
42
|
)
|
37
43
|
end
|
38
|
-
|
39
|
-
def create_table(**options)
|
40
|
-
@table_structure_schema_definition_.create_table(options)
|
41
|
-
end
|
42
44
|
end
|
43
45
|
end
|
@@ -15,18 +15,18 @@ module TableStructure
|
|
15
15
|
|
16
16
|
def write(items, to:, **options)
|
17
17
|
options = @options.merge(options)
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
18
|
+
@schema.create_table(options) do |table|
|
19
|
+
unless options[:header_omitted]
|
20
|
+
header = table.header(
|
21
|
+
context: options[:header_context]
|
22
|
+
)
|
23
|
+
header = yield header if block_given?
|
24
|
+
to.send(options[:method], header)
|
25
|
+
end
|
26
|
+
table.rows(enumerize(items)).each do |row|
|
27
|
+
row = yield row if block_given?
|
28
|
+
to.send(options[:method], row)
|
29
|
+
end
|
30
30
|
end
|
31
31
|
nil
|
32
32
|
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.13
|
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-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|