table_structure 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 06f36abaa1bc4478901ea51a27e0e22b00ecf70ecbfd18b4c785c2e86e08ca1d
4
- data.tar.gz: 26990e2f8ac30764a3d571dbf8a35e422ce163989f9eda5e92aa3c2b9e5cf498
3
+ metadata.gz: bebf267d2cae044dd2f493b213950954c88b384d2b3e6dd15c09b7947b104eb7
4
+ data.tar.gz: cbe61559035246eb4dc80edf4e34b1b67a0e1f8db16749368e81d23e48bd2b81
5
5
  SHA512:
6
- metadata.gz: 288ea6944e5efa303a3c39e4502dce7b0d2994504796a7de0b69b28d8b0cd91f878670db9846a51cbb57d16797bb8124e50f2162db00ec89b49d83de3a345030
7
- data.tar.gz: 72d983aef1de40282befe38d230806b1fb3deec4ecf08801822867d43ea656a6a307ad53c46ae87f2661bcc90ef5c6f0b3d8c5ea515eb80ddeb78e559b552585
6
+ metadata.gz: cd6f1e4299eda55eb1959c38248911c9fc052dee5cb541cc3b1517cb00947f069e5896ca9d69e792fa2258f6bb636aaee251efedb11af96cd55241cf44ab3dd1
7
+ data.tar.gz: 0e52a02afa1dc1c669233edc2e900cdfae5e5085bd6138bd2055aa57f4fadfdf0c587228f5eb560b5ba857555d34bbdc4477c1128fd193f144625e8f099a2cf7
@@ -1,3 +1,8 @@
1
+ # 0.4.3
2
+ Changes:
3
+ - `TableStructure::Schema`
4
+ - Change to be able to use a block with `columns` DSL.
5
+
1
6
  # 0.4.2
2
7
  Changes:
3
8
  - `TableStructure::Schema`
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- table_structure (0.4.2)
4
+ table_structure (0.4.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -44,19 +44,19 @@ class SampleTableSchema
44
44
  column name: 'Name',
45
45
  value: ->(row, *) { row[:name] }
46
46
 
47
- columns name: ['Pet 1', 'Pet 2', 'Pet 3'],
47
+ column name: ['Pet 1', 'Pet 2', 'Pet 3'],
48
48
  value: ->(row, *) { row[:pets] }
49
49
 
50
- columns ->(table) {
50
+ columns do |table|
51
51
  table[:questions].map do |question|
52
52
  {
53
53
  name: question[:id],
54
54
  value: ->(row, *) { row[:answers][question[:id]] }
55
55
  }
56
56
  end
57
- }
57
+ end
58
58
 
59
- column_converter :to_s do |val, _row, _table|
59
+ column_builder :to_s do |val, _row, _table|
60
60
  val.to_s
61
61
  end
62
62
  end
@@ -159,7 +159,6 @@ Define a schema:
159
159
  class SampleTableSchema
160
160
  include TableStructure::Schema
161
161
 
162
- # If `:header` is set to `false`, `:name` is optional.
163
162
  column name: 'ID',
164
163
  key: :id,
165
164
  value: ->(row, *) { row[:id] }
@@ -168,11 +167,11 @@ class SampleTableSchema
168
167
  key: :name,
169
168
  value: ->(row, *) { row[:name] }
170
169
 
171
- columns name: ['Pet 1', 'Pet 2', 'Pet 3'],
170
+ column name: ['Pet 1', 'Pet 2', 'Pet 3'],
172
171
  key: %i[pet1 pet2 pet3],
173
172
  value: ->(row, *) { row[:pets] }
174
173
 
175
- columns ->(table) {
174
+ columns do |table|
176
175
  table[:questions].map do |question|
177
176
  {
178
177
  name: question[:id],
@@ -180,7 +179,7 @@ class SampleTableSchema
180
179
  value: ->(row, *) { row[:answers][question[:id]] }
181
180
  }
182
181
  end
183
- }
182
+ end
184
183
  end
185
184
  ```
186
185
 
@@ -275,7 +274,7 @@ class UserTableSchema
275
274
  end
276
275
 
277
276
  schema = UserTableSchema.new do
278
- column_converter :to_s do |val, *|
277
+ column_builder :to_s do |val|
279
278
  val.to_s
280
279
  end
281
280
  end
@@ -314,14 +313,14 @@ class SampleTableSchema
314
313
  column name: 'Name',
315
314
  value: ->(row, *) { row[:name] }
316
315
 
317
- columns ->(table) {
316
+ columns do |table|
318
317
  if table[:pet_num].positive?
319
318
  {
320
319
  name: (1..table[:pet_num]).map { |num| "Pet #{num}" },
321
320
  value: ->(row, *) { row[:pets] }
322
321
  }
323
322
  end
324
- }
323
+ end
325
324
  end
326
325
 
327
326
  context = { pet_num: 0 }
@@ -346,7 +345,7 @@ class SampleTableSchema
346
345
  TableContext.new(**context)
347
346
  end
348
347
 
349
- context_builder :row, :value do |context|
348
+ context_builder :row do |context|
350
349
  RowContext.new(**context)
351
350
  end
352
351
 
@@ -356,17 +355,17 @@ class SampleTableSchema
356
355
  column name: 'Name',
357
356
  value: ->(row, *) { row.name }
358
357
 
359
- columns name: ['Pet 1', 'Pet 2', 'Pet 3'],
358
+ column name: ['Pet 1', 'Pet 2', 'Pet 3'],
360
359
  value: ->(row, *) { row.increase_pets }
361
360
 
362
- columns ->(table) {
361
+ columns do |table|
363
362
  table.questions.map do |question|
364
363
  {
365
364
  name: question[:id],
366
365
  value: ->(row, *) { row.answers[question[:id]] }
367
366
  }
368
367
  end
369
- }
368
+ end
370
369
  end
371
370
  ```
372
371
 
@@ -392,13 +391,13 @@ class SampleTableSchema
392
391
 
393
392
  columns UserTableSchema
394
393
 
395
- columns ->(table) {
394
+ columns do |table|
396
395
  UserTableSchema.new(context: table, name_prefix: 'Friend ', key_prefix: 'friend_') do
397
396
  context_builder :row do |context|
398
397
  context[:friend]
399
398
  end
400
399
  end
401
- }
400
+ end
402
401
  end
403
402
 
404
403
  items = [
@@ -420,9 +419,9 @@ You can also concatenate or merge the schema classes.
420
419
  Both create a schema class, with a few differences.
421
420
  - `+`
422
421
  - Similar to nesting the schemas.
423
- `column_converter` works only to columns in the schema that they was defined.
422
+ `column_builder` works only to columns in the schema that they was defined.
424
423
  - `merge`
425
- - If there are some definitions of `column_converter` with the same name in the schemas to be merged, the one in the schema that is merged last will work to all columns.
424
+ - If there are some definitions of `column_builder` with the same name in the schemas to be merged, the one in the schema that is merged last will work to all columns.
426
425
 
427
426
  ```ruby
428
427
  class UserTableSchema
@@ -438,10 +437,10 @@ end
438
437
  class PetTableSchema
439
438
  include TableStructure::Schema
440
439
 
441
- columns name: ['Pet 1', 'Pet 2', 'Pet 3'],
440
+ column name: ['Pet 1', 'Pet 2', 'Pet 3'],
442
441
  value: ->(row, *) { row[:pets] }
443
442
 
444
- column_converter :same_name do |val, *|
443
+ column_builder :same_name do |val|
445
444
  "pet: #{val}"
446
445
  end
447
446
  end
@@ -449,16 +448,16 @@ end
449
448
  class QuestionTableSchema
450
449
  include TableStructure::Schema
451
450
 
452
- columns ->(table) {
451
+ columns do |table|
453
452
  table[:questions].map do |question|
454
453
  {
455
454
  name: question[:id],
456
455
  value: ->(row, *) { row[:answers][question[:id]] }
457
456
  }
458
457
  end
459
- }
458
+ end
460
459
 
461
- column_converter :same_name do |val, *|
460
+ column_builder :same_name do |val|
462
461
  "question: #{val}"
463
462
  end
464
463
  end
@@ -4,13 +4,11 @@ module TableStructure
4
4
  module Schema
5
5
  module ClassMethods
6
6
  def +(other)
7
- unless ::TableStructure::Schema::Utils.schema_class?(other)
8
- raise ::TableStructure::Error, "Must be a schema class. #{other}"
9
- end
7
+ raise ::TableStructure::Error, "Must be a schema class. [#{other}]" unless Utils.schema_class?(other)
10
8
 
11
9
  self_class = self
12
10
 
13
- ::TableStructure::Schema.create_class do
11
+ Schema.create_class do
14
12
  columns self_class
15
13
  columns other
16
14
  end
@@ -18,33 +16,16 @@ module TableStructure
18
16
 
19
17
  def merge(*others)
20
18
  others.each do |other|
21
- unless ::TableStructure::Schema::Utils.schema_class?(other)
22
- raise ::TableStructure::Error, "Must be a schema class. #{other}"
23
- end
19
+ raise ::TableStructure::Error, "Must be a schema class. [#{other}]" unless Utils.schema_class?(other)
24
20
  end
25
21
 
26
- schema_classes = [self, *others]
27
-
28
- ::TableStructure::Schema.create_class do
29
- @__column_definitions__ =
30
- schema_classes
31
- .map(&:column_definitions)
32
- .flatten
33
-
34
- @__context_builders__ =
35
- schema_classes
36
- .map(&:context_builders)
37
- .reduce({}, &:merge!)
38
-
39
- @__column_builders__ =
40
- schema_classes
41
- .map(&:column_builders)
42
- .reduce({}, &:merge!)
22
+ schema_class = CompositeClass.new.compose(self, *others)
43
23
 
44
- @__row_builders__ =
45
- schema_classes
46
- .map(&:row_builders)
47
- .reduce({}, &:merge!)
24
+ Schema.create_class do
25
+ @__column_definitions__ = schema_class.column_definitions
26
+ @__context_builders__ = schema_class.context_builders
27
+ @__column_builders__ = schema_class.column_builders
28
+ @__row_builders__ = schema_class.row_builders
48
29
  end
49
30
  end
50
31
  end
@@ -18,7 +18,7 @@ module TableStructure
18
18
  optional_builders[:_name_prepender_] =
19
19
  ::TableStructure::Utils::TypedProc.new(
20
20
  types: :header
21
- ) do |val, *|
21
+ ) do |val|
22
22
  val.nil? ? val : "#{name_prefix}#{val}"
23
23
  end
24
24
  end
@@ -27,7 +27,7 @@ module TableStructure
27
27
  optional_builders[:_name_appender_] =
28
28
  ::TableStructure::Utils::TypedProc.new(
29
29
  types: :header
30
- ) do |val, *|
30
+ ) do |val|
31
31
  val.nil? ? val : "#{val}#{name_suffix}"
32
32
  end
33
33
  end
@@ -15,7 +15,7 @@ module TableStructure
15
15
  def column_definitions
16
16
  @classes
17
17
  .map(&:column_definitions)
18
- .reduce([], &:concat)
18
+ .flatten
19
19
  end
20
20
 
21
21
  def context_builders
@@ -13,7 +13,7 @@ module TableStructure
13
13
  end
14
14
 
15
15
  def validate(name:, key:, size:, **)
16
- raise Error.new('"key" must not be lambda.', @name, @index) if key.respond_to?(:call)
16
+ raise Error.new('"key" must not be callable.', @name, @index) if key.respond_to?(:call)
17
17
  if !key && name.respond_to?(:call) && !size
18
18
  raise Error.new('"size" must be defined, because column size cannot be determined.', @name, @index)
19
19
  end
@@ -5,12 +5,22 @@ module TableStructure
5
5
  module DSL
6
6
  module ColumnDefinition
7
7
  def column(definition)
8
+ unless definition.is_a?(Hash)
9
+ warn "[TableStructure] Use `columns` instead of `column`.", uplevel: 1
10
+ end
8
11
  column_definitions << definition
9
12
  nil
10
13
  end
11
14
 
12
- def columns(definition)
13
- column(definition)
15
+ def columns(definition = nil, &block)
16
+ if definition.is_a?(Hash)
17
+ warn "[TableStructure] Use `column` instead of `columns`.", uplevel: 1
18
+ end
19
+ if Utils.callable?(definition)
20
+ warn "[TableStructure] Use `block` instead of `lambda` or `proc`.", uplevel: 1
21
+ end
22
+ column_definitions << (block || definition)
23
+ nil
14
24
  end
15
25
 
16
26
  def column_definitions
@@ -12,18 +12,14 @@ module TableStructure
12
12
  return unless @schema.contain_name_callable?
13
13
  return unless @builders.key?(:header)
14
14
 
15
- proc do |context|
16
- @builders[:header].call(context)
17
- end
15
+ proc { |context| @builders[:header].call(context) }
18
16
  end
19
17
 
20
18
  def create_data_builder
21
19
  return unless @schema.contain_value_callable?
22
20
  return unless @builders.key?(:row)
23
21
 
24
- proc do |context|
25
- @builders[:row].call(context)
26
- end
22
+ proc { |context| @builders[:row].call(context) }
27
23
  end
28
24
  end
29
25
  end
@@ -5,7 +5,7 @@ module TableStructure
5
5
  DEFAULT_ROW_BUILDERS = {
6
6
  _to_hash_: Utils::TypedProc.new(
7
7
  types: :hash
8
- ) do |values, keys, *|
8
+ ) do |values, keys|
9
9
  keys.map.with_index { |key, i| [key || i, values[i]] }.to_h
10
10
  end
11
11
  }.freeze
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TableStructure
4
- VERSION = '0.4.2'
4
+ VERSION = '0.4.3'
5
5
  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.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - jsmmr
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-26 00:00:00.000000000 Z
11
+ date: 2020-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler