table_structure 0.3.5 → 0.3.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0044f8cb21e559ae23eed1f0eb6f28c1b0bc9dab9ed804088d17ff85f4040a4f
4
- data.tar.gz: 97ef79ee4a4d71d1b6c20e56841a5e450f116777050e285e17ba11c2ddd43f1b
3
+ metadata.gz: c4e634ccdf9dc91b29a3080cc07dec5c621df20819e36f11ed6de4f862e92c6b
4
+ data.tar.gz: ba5d565b93e427c146b2e9bbb1ef2c3260e10558b596e57bee45546774c970ce
5
5
  SHA512:
6
- metadata.gz: 8cff983c441e44cfb7d6d0fc525aeca0b210ea50806421b607d3b53c4c0947a04eca0030f6da6cfdb91e05975dc14cb8805d08b314d48b73ee3013b944d99ed0
7
- data.tar.gz: 3310d0d8446fc608a2956d45fb6bea2e514a774f1f83e19b828cde4d11f2fc69d5ad9ff75546f811f3d5992f2e81bccf6dd2dfc4208b078095750162bc68d0ad
6
+ metadata.gz: f7c7de8a1c0cac9520e794c7c28ee8e7a07dae95498800b075f23e147004b7d53e4031c09d8215570bd83e54c9588a976f078d8ff67028cbd8cbc9990d9227e6
7
+ data.tar.gz: 4c012c0c4818641483766febaebc65a3a53e9378fe3cb6e9a78f9cd0c28c396e5e77a00ed3bd888bc291991ea740da44e51db35381a2709ffd4f079962abe608
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 0.3.6
2
+ Changes:
3
+ - `TableStructure::Schema`
4
+ - Improve performance when `context_builder` is not defined.
5
+
1
6
  # 0.3.5
2
7
  Changes:
3
8
  - `TableStructure::Schema`
@@ -29,7 +34,7 @@ Changes:
29
34
  # 0.3.2
30
35
  Changes:
31
36
  - `TableStructure::Writer`
32
- - When `result_type: :hash` option is specified and `column(s)` key of the schema is undefined, index number is used as the key.
37
+ - When `result_type: :hash` option is specified and `column(s)` `:key` of the schema is undefined, index number is used as the key.
33
38
 
34
39
  # 0.3.1
35
40
  Changes:
@@ -49,7 +54,7 @@ Changes:
49
54
  - DSL
50
55
  - `column(s)`
51
56
  - Add `:omitted` option.
52
- - Support nested schema.
57
+ - Support nested schemas.
53
58
 
54
59
  # 0.2.0
55
60
  Changes:
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- table_structure (0.3.5)
4
+ table_structure (0.3.6)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -2,8 +2,12 @@
2
2
 
3
3
  [![Build Status](https://travis-ci.org/jsmmr/ruby_table_structure.svg?branch=master)](https://travis-ci.org/jsmmr/ruby_table_structure)
4
4
 
5
- `TableStructure` has two major functions.
6
- The functions are `TableStructure::Schema` that defines the schema of a table using DSL and ` TableStructure::Writer` that converts and outputs data with the schema.
5
+ - `TableStructure::Schema`
6
+ - Defines columns of a table using DSL.
7
+ - `TableStructure::Writer`
8
+ - Converts data with the schema, and outputs it.
9
+ - `TableStructure::Iterator`
10
+ - Converts data with the schema, and enumerates it.
7
11
 
8
12
  ## Installation
9
13
 
@@ -132,7 +136,7 @@ class SampleTableSchema
132
136
  end
133
137
  }
134
138
 
135
- ## When nesting schemas, same key must not exist in parent and child schemas.
139
+ ## When nesting schemas, :key must be unique in parent and child schemas.
136
140
  ## This can also be avoided by specifying :key_prefix or :key_suffix option.
137
141
  # columns ->(table) { NestedSchema.new(context: table, key_prefix: 'foo_', key_suffix: '_bar') }
138
142
 
@@ -237,11 +241,11 @@ class SampleTableSchema
237
241
  value: ->(row, *) { row[:name] }
238
242
 
239
243
  columns ->(table) { PetsSchema.new(context: table) }
240
- ## Same as above code
244
+ ## or
241
245
  # columns PetsSchema
242
246
 
243
247
  columns ->(table) { QuestionsSchema.new(context: table) }
244
- ## Same as above code.
248
+ ## or
245
249
  # columns QuestionsSchema
246
250
 
247
251
  column_converter :to_s, ->(val, *) { val.to_s }
@@ -259,7 +263,7 @@ schema = SampleTableSchema.new(context: context)
259
263
  ```
260
264
 
261
265
  You can also use `context_builder`.
262
- This may be useful when `column` definition lambda is complicated.
266
+ This may be useful when `column(s)` lambda is complicated.
263
267
  ```ruby
264
268
  class SampleTableSchema
265
269
  include TableStructure::Schema
@@ -16,6 +16,8 @@ module TableStructure
16
16
  require 'table_structure/schema/definition/error'
17
17
  require 'table_structure/schema/definition/validator'
18
18
  require 'table_structure/schema/table'
19
+ require 'table_structure/schema/table/context_builder'
20
+ require 'table_structure/schema/table/key_decorator'
19
21
  require 'table_structure/schema/column'
20
22
  require 'table_structure/schema/column/attrs'
21
23
  require 'table_structure/schema/column/schema'
@@ -25,8 +25,8 @@ module TableStructure
25
25
 
26
26
  @name = name
27
27
  @columns = create_columns(name, column_definitions, context, options)
28
- @header_context_builder = context_builders[:header]
29
- @row_context_builder = context_builders[:row]
28
+ @header_context_builder = Table::ContextBuilder.new(:header, context_builders[:header])
29
+ @row_context_builder = Table::ContextBuilder.new(:row, context_builders[:row])
30
30
  @header_converters = select_column_converters(:header, column_converters)
31
31
  @row_converters = select_column_converters(:row, column_converters)
32
32
  @result_builders = result_builders
@@ -16,49 +16,37 @@ module TableStructure
16
16
  options
17
17
  )
18
18
  @columns = columns
19
- @header_context_builder = header_context_builder
20
- @row_context_builder = row_context_builder
21
19
  @header_converters = header_converters
22
20
  @row_converters = row_converters
23
21
  @result_builders = result_builders
24
22
  @context = context
25
23
  @options = options
24
+
25
+ if header_context_builder.available?
26
+ singleton_class.include header_context_builder
27
+ end
28
+ if row_context_builder.available?
29
+ singleton_class.include row_context_builder
30
+ end
26
31
  end
27
32
 
28
33
  def header(context: nil)
29
- if @header_context_builder
30
- context = @header_context_builder.call(context)
31
- end
32
34
  values(:name, context, @header_converters)
33
35
  end
34
36
 
35
37
  def row(context: nil)
36
- context = @row_context_builder.call(context) if @row_context_builder
37
38
  values(:value, context, @row_converters)
38
39
  end
39
40
 
40
41
  private
41
42
 
42
43
  def keys
43
- @keys ||= obtain_keys
44
- end
45
-
46
- def obtain_keys
47
- keys = @columns.map(&:keys).flatten
48
- has_key_options? ? decorate_keys(keys) : keys
49
- end
50
-
51
- def has_key_options?
52
- @options[:key_prefix] || @options[:key_suffix]
53
- end
54
-
55
- def decorate_keys(keys)
56
- keys.map do |key|
57
- next key unless key
58
-
59
- decorated_key = "#{@options[:key_prefix]}#{key}#{@options[:key_suffix]}"
60
- decorated_key = decorated_key.to_sym if key.is_a?(Symbol)
61
- decorated_key
44
+ @keys ||= begin
45
+ keys = @columns.map(&:keys).flatten
46
+ KeyDecorator.new(
47
+ prefix: @options[:key_prefix],
48
+ suffix: @options[:key_suffix]
49
+ ).decorate(keys)
62
50
  end
63
51
  end
64
52
 
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TableStructure
4
+ module Schema
5
+ class Table
6
+ class ContextBuilder < Module
7
+ def initialize(name, callable)
8
+ @available = !callable.nil?
9
+
10
+ return unless @available
11
+
12
+ define_method(name) do |context: nil|
13
+ super(context: callable.call(context))
14
+ end
15
+ end
16
+
17
+ def available?
18
+ @available
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TableStructure
4
+ module Schema
5
+ class Table
6
+ class KeyDecorator
7
+ def initialize(prefix: nil, suffix: nil)
8
+ @prefix = prefix
9
+ @suffix = suffix
10
+ end
11
+
12
+ def decorate(keys)
13
+ return keys unless has_any_options?
14
+
15
+ keys.map do |key|
16
+ next key unless key
17
+
18
+ decorated_key = "#{@prefix}#{key}#{@suffix}"
19
+ decorated_key = decorated_key.to_sym if key.is_a?(Symbol)
20
+ decorated_key
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def has_any_options?
27
+ @prefix || @suffix
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TableStructure
4
- VERSION = '0.3.5'
4
+ VERSION = '0.3.6'
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.5
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - jsmmr
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-16 00:00:00.000000000 Z
11
+ date: 2019-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,6 +86,8 @@ files:
86
86
  - lib/table_structure/schema/dsl/option.rb
87
87
  - lib/table_structure/schema/dsl/result_builder.rb
88
88
  - lib/table_structure/schema/table.rb
89
+ - lib/table_structure/schema/table/context_builder.rb
90
+ - lib/table_structure/schema/table/key_decorator.rb
89
91
  - lib/table_structure/schema/utils.rb
90
92
  - lib/table_structure/version.rb
91
93
  - lib/table_structure/writer.rb