table_structure 0.3.15 → 0.3.16

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: 77197fdbfa164e9f532b190b4e94750354595f9e51d155b91ed36ea2f43aeef2
4
- data.tar.gz: 52ab0ab962584d0d7e8e10788a6c949369926ec8869577e797a462e90eb76487
3
+ metadata.gz: c8794a66493988290a6ddeea36604322559a519ba3f0f261af4243fd56687663
4
+ data.tar.gz: 57f9279940767cc9c0612a9ffda469711ac070d086a8780aeef4a23c8dafef7d
5
5
  SHA512:
6
- metadata.gz: 99774b00f0260b64ed8a3ea670cf24d79e222c8b75fab7545932fbb248516561078f2908a081bae925e7baf44502d115711974d5b7458937e6db1e0961be0152
7
- data.tar.gz: ca70303e5f5ec9ae437e9dccd9554b027c48aa68b7cb9c0f0239f6fa1ca0259321c9ea942310ac7a320e8d80ef6b996ef02a1cfb5ac0afa3a317534b351ecd85
6
+ metadata.gz: e906e4e3bdb277218db13fc90b028470f794ae9f346de456a84c524219aeaf63a184d9a8e4802ed3cc0dab55f7891fe0b6a5012144b236d6da1f3a02c3abebe0
7
+ data.tar.gz: c68e1178bce24e496b77ab1b3cb3674120129b86b1e352ef6233f29504b11e7355d93a4975c3e3445fd88e03ed19ac1bf05f4f86dc38331a8e256a2d273356f2
data/.travis.yml CHANGED
@@ -8,4 +8,4 @@ rvm:
8
8
  - 2.5
9
9
  - 2.4
10
10
  - 2.3
11
- before_install: gem install bundler:2.1.1
11
+ before_install: gem install bundler:2.1.2
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # 0.3.16
2
+ Changes:
3
+ - `TableStructure::Schema`
4
+ - Enable to add definitions in a block when initializing the schema.
5
+ - Add `merge` class method.
6
+ - Change `+` class method not to overwrite the definitions (`column_converter`, `context_builder`) by the last one with the same name. If you expect the same behavior as before, use `merge` instead.
7
+
1
8
  # 0.3.15
2
9
  Changes:
3
10
  - `TableStructure::Schema`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- table_structure (0.3.15)
4
+ table_structure (0.3.16)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -32,4 +32,4 @@ DEPENDENCIES
32
32
  table_structure!
33
33
 
34
34
  BUNDLED WITH
35
- 2.1.1
35
+ 2.1.2
data/README.md CHANGED
@@ -268,7 +268,24 @@ context = { pet_num: 0 }
268
268
  schema = SampleTableSchema.new(context: context, nil_definitions_ignored: true)
269
269
  ```
270
270
 
271
- You can also nest schemas.
271
+ You can add definitions when initializing the schema.
272
+ ```ruby
273
+ class UserTableSchema
274
+ include TableStructure::Schema
275
+
276
+ column name: 'ID',
277
+ value: ->(row, _table) { row[:id] }
278
+
279
+ column name: 'Name',
280
+ value: ->(row, *) { row[:name] }
281
+ end
282
+
283
+ schema = UserTableSchema.new do
284
+ column_converter :to_s, ->(val, *) { val.to_s }
285
+ end
286
+ ```
287
+
288
+ You can also nest the schemas.
272
289
  ```ruby
273
290
  class PetTableSchema
274
291
  include TableStructure::Schema
@@ -290,7 +307,7 @@ class QuestionTableSchema
290
307
  }
291
308
  end
292
309
 
293
- class SampleTableSchema
310
+ class UserTableSchema
294
311
  include TableStructure::Schema
295
312
 
296
313
  column name: 'ID',
@@ -316,11 +333,17 @@ context = {
316
333
  ]
317
334
  }
318
335
 
319
- schema = SampleTableSchema.new(context: context)
336
+ schema = UserTableSchema.new(context: context)
320
337
  ```
321
338
 
322
- You can also concatenate schemas.
323
- If there are some definitions of `column_converter` with the same name in the schemas to be concatenated, the one in the schema that is concatenated last will be used.
339
+ You can also concatenate or merge the schema classes.
340
+ Both create a schema class, with a few differences.
341
+ - `+`
342
+ - Similar to nesting the schemas.
343
+ `column_converter` or `context_builder` works only to columns in the schema that they was defined.
344
+ - `merge`
345
+ - If there are some definitions of `column_converter` or `context_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.
346
+
324
347
  ```ruby
325
348
  class UserTableSchema
326
349
  include TableStructure::Schema
@@ -338,7 +361,7 @@ class PetTableSchema
338
361
  columns name: ['Pet 1', 'Pet 2', 'Pet 3'],
339
362
  value: ->(row, *) { row[:pets] }
340
363
 
341
- column_converter :same_name, ->(val, *) { 'This definition will not be used.' }
364
+ column_converter :same_name, ->(val, *) { "pet: #{val}" }
342
365
  end
343
366
 
344
367
  class QuestionTableSchema
@@ -353,7 +376,7 @@ class QuestionTableSchema
353
376
  end
354
377
  }
355
378
 
356
- column_converter :same_name, ->(val, *) { 'This definition will be used.' }
379
+ column_converter :same_name, ->(val, *) { "question: #{val}" }
357
380
  end
358
381
 
359
382
  context = {
@@ -364,7 +387,9 @@ context = {
364
387
  ]
365
388
  }
366
389
 
367
- schema = (UserTableSchema + PetTableSchema + QuestionTableSchema).new(context: context)
390
+ concatenated_schema = (UserTableSchema + PetTableSchema + QuestionTableSchema).new(context: context)
391
+
392
+ merged_schema = UserTableSchema.merge(PetTableSchema, QuestionTableSchema).new(context: context)
368
393
  ```
369
394
 
370
395
  You can also use `context_builder`.
@@ -404,7 +429,7 @@ class SampleTableSchema
404
429
  end
405
430
  ```
406
431
 
407
- If you want to convert CSV character code, you can do so within block of `write` method.
432
+ If you want to convert CSV character code, you can do so in a block of `write` method.
408
433
  ```ruby
409
434
  File.open('sample.csv', 'w') do |f|
410
435
  writer.write(items, to: CSV.new(f)) do |row_values|
@@ -3,30 +3,48 @@
3
3
  module TableStructure
4
4
  module Schema
5
5
  module ClassMethods
6
- def +(schema)
7
- self_schema = self
8
- Class.new do
9
- include ::TableStructure::Schema
6
+ def +(other)
7
+ unless ::TableStructure::Schema::Utils.schema_class?(other)
8
+ raise ::TableStructure::Error, "Must be a schema class. #{other}"
9
+ end
10
+
11
+ self_class = self
12
+
13
+ ::TableStructure::Schema.create_class do
14
+ columns self_class
15
+ columns other
16
+ end
17
+ end
18
+
19
+ def merge(*others)
20
+ others.each do |other|
21
+ unless ::TableStructure::Schema::Utils.schema_class?(other)
22
+ raise ::TableStructure::Error, "Must be a schema class. #{other}"
23
+ end
24
+ end
25
+
26
+ schema_classes = [self, *others]
10
27
 
11
- @__column_definitions__ = [
12
- self_schema.column_definitions,
13
- schema.column_definitions
14
- ].flatten
28
+ ::TableStructure::Schema.create_class do
29
+ @__column_definitions__ =
30
+ schema_classes
31
+ .map(&:column_definitions)
32
+ .flatten
15
33
 
16
34
  @__context_builders__ =
17
- {}
18
- .merge!(self_schema.context_builders)
19
- .merge!(schema.context_builders)
35
+ schema_classes
36
+ .map(&:context_builders)
37
+ .reduce({}, &:merge!)
20
38
 
21
39
  @__column_converters__ =
22
- {}
23
- .merge!(self_schema.column_converters)
24
- .merge!(schema.column_converters)
40
+ schema_classes
41
+ .map(&:column_converters)
42
+ .reduce({}, &:merge!)
25
43
 
26
44
  @__result_builders__ =
27
- {}
28
- .merge!(self_schema.result_builders)
29
- .merge!(schema.result_builders)
45
+ schema_classes
46
+ .map(&:result_builders)
47
+ .reduce({}, &:merge!)
30
48
  end
31
49
  end
32
50
  end
@@ -11,6 +11,16 @@ module TableStructure
11
11
  klass.extend(ClassMethods)
12
12
  end
13
13
 
14
+ def self.create_class(&block)
15
+ raise ::TableStructure::Error, 'No block given.' unless block
16
+
17
+ schema_module = self
18
+ Class.new do
19
+ include schema_module
20
+ class_eval(&block)
21
+ end
22
+ end
23
+
14
24
  Definition = Struct.new(
15
25
  'Definition',
16
26
  :name,
@@ -30,7 +40,8 @@ module TableStructure
30
40
  key_prefix: nil,
31
41
  key_suffix: nil,
32
42
  nil_definitions_ignored: false,
33
- **deprecated_options
43
+ **deprecated_options,
44
+ &block
34
45
  )
35
46
  unless deprecated_options.empty?
36
47
  caller_location = caller_locations(1, 1)
@@ -47,12 +58,30 @@ module TableStructure
47
58
  nil_definitions_ignored: nil_definitions_ignored
48
59
  }.merge!(self.class.options).merge!(deprecated_options)
49
60
 
50
- context_builders = ContextBuilders.new({}.merge!(self.class.context_builders))
51
- column_converters = ColumnConverters.new({}.merge!(self.class.column_converters))
52
- result_builders = ResultBuilders.new({}.merge!(self.class.result_builders))
61
+ schema_classes = [self.class]
53
62
 
54
- context = context_builders.build_for_table(context)
55
- columns = Column::Factory.create(name, self.class.column_definitions, context, options)
63
+ if block_given?
64
+ schema_classes << ::TableStructure::Schema.create_class(&block)
65
+ end
66
+
67
+ context_builders = ContextBuilders.new(
68
+ schema_classes.map(&:context_builders).reduce({}, &:merge!)
69
+ )
70
+
71
+ column_converters = ColumnConverters.new(
72
+ schema_classes.map(&:column_converters).reduce({}, &:merge!)
73
+ )
74
+
75
+ result_builders = ResultBuilders.new(
76
+ schema_classes.map(&:result_builders).reduce({}, &:merge!)
77
+ )
78
+
79
+ columns = Column::Factory.create(
80
+ name,
81
+ schema_classes.map(&:column_definitions).reduce([], &:concat),
82
+ context_builders.build_for_table(context),
83
+ options
84
+ )
56
85
 
57
86
  @_definition_ =
58
87
  Definition.new(
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TableStructure
4
- VERSION = '0.3.15'
4
+ VERSION = '0.3.16'
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.3.15
4
+ version: 0.3.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - jsmmr
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-27 00:00:00.000000000 Z
11
+ date: 2019-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler