table_structure 0.3.0 → 0.3.1

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: 4e966906136bf9907efb5bf1ad6fc1f2181ded3e4c6bbd6f76c19750198ecadd
4
- data.tar.gz: c1f11210056449c18d31c693e758872659d603b1e9d1a28013cf0e35baff8776
3
+ metadata.gz: f880b928161f99c6d4346f06304cb9cfe4a1c44f8ffb307243a70d74ec5e400d
4
+ data.tar.gz: 9d1a30376429abe0906e3e0879eb230e4daf51c5c7de87cf28b63673433df83e
5
5
  SHA512:
6
- metadata.gz: a98e9de6bd5d32941ee5730a8f34294d459a04ebc5e22a547887225421f7f91fcbbce59b026c6af5a96fa6215e38f0be6ed03b6155e04365418576f982d15873
7
- data.tar.gz: b57d5dc8e40f0937ea14c06c08fb54d720bbd3215186b9357fc0f9f28705ea905938f238157999ecb62221ae84fd41d28872c63653557919b2be92a2162bc468
6
+ metadata.gz: 3b5ee4c95815cb891a25ac377ec72359d70394da3ea1aaf58ea71f77618ffaaa8a5443fff95366ab0ca8ecccd871aa33bafbbe134ccb8109645146392dcbc4fa
7
+ data.tar.gz: 422e6db13c3940bdf873535c1f02d3f32211cca6a81ced5d1aebf678504f14daeda50c8de46d47762be33fcad79f90cb924d6d175a3a19c2029406d02d5a5a43
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # 0.3.1
2
+ Changes:
3
+ - `TableStructure::Schema`
4
+ - Make `:result_type` option available for `TableStructure::Write` and `TableStructure::Iterator`.
5
+ - `:result_type` option for `TableStructure::Schema` is deprecated.
6
+
1
7
  # 0.3.0
2
8
  Changes:
3
9
  - `TableStructure::Schema`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- table_structure (0.3.0)
4
+ table_structure (0.3.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -147,10 +147,10 @@ context = {
147
147
  ]
148
148
  }
149
149
 
150
- schema = SampleTableSchema.new(context: context, result_type: :hash) # default is :array
151
- iterator = TableStructure::Iterator.new(schema, header_omitted: true)
150
+ schema = SampleTableSchema.new(context: context)
151
+ iterator = TableStructure::Iterator.new(schema, result_type: :hash, header_omitted: true)
152
152
  ## or
153
- # writer = TableStructure::Writer.new(schema, header_omitted: true)
153
+ # writer = TableStructure::Writer.new(schema, result_type: :hash, header_omitted: true)
154
154
  # iterator = TableStructure::Iterator.new(writer)
155
155
 
156
156
  items = [
@@ -293,7 +293,7 @@ class SampleTableSchema
293
293
  end
294
294
  ```
295
295
 
296
- If you want to convert CSV character code, you do so within block of `write` method.
296
+ If you want to convert CSV character code, you can do so within block of `write` method.
297
297
  ```ruby
298
298
  File.open('sample.csv', 'w') do |f|
299
299
  writer.write(items, to: CSV.new(f)) do |row_values|
@@ -13,8 +13,8 @@ module TableStructure
13
13
  end
14
14
  end
15
15
 
16
- def iterate(items)
17
- Enumerator.new { |y| @writer.write(items, to: y) }
16
+ def iterate(items, **options)
17
+ Enumerator.new { |y| @writer.write(items, to: y, **options) }
18
18
  end
19
19
  end
20
20
  end
@@ -37,9 +37,10 @@ module TableStructure
37
37
  definition
38
38
  elsif Utils.schema_instance?(definition)
39
39
  definition
40
- # elsif Utils.schema_class?(definition)
41
- # # TODO: This doesn't work as expected when result_type: :hash is specified.
42
- # definition.new(context: context, **@options)
40
+ elsif Utils.schema_class?(definition)
41
+ definition.new(context: context)
42
+ else
43
+ raise Error.new('Invalid definition.', i)
43
44
  end
44
45
  end
45
46
  end
@@ -3,26 +3,30 @@
3
3
  module TableStructure
4
4
  module Schema
5
5
  class Table
6
- attr_reader :columns, :column_converters, :result_builders
6
+ RESULT_BUILDERS = {
7
+ hash: ->(values, keys, *) { keys.zip(values).to_h }
8
+ }.freeze
9
+
10
+ attr_reader :columns, :column_converters, :result_builders, :options
7
11
 
8
12
  def initialize(column_definitions, column_converters, result_builders, context, options)
9
13
  @columns = build_columns(column_definitions, context, options)
10
- @column_converters = default_column_converters.merge(column_converters)
11
- @result_builders = default_result_builders(options).merge(result_builders)
14
+ @column_converters = column_converters
15
+ @result_builders = result_builders
12
16
  @context = context
13
17
  @options = options
14
18
  end
15
19
 
16
- def header_values(context)
17
- values(:name, context)
20
+ def header_values(context, result_type = nil) # TODO
21
+ values(:name, result_type, context)
18
22
  end
19
23
 
20
- def row_values(context)
21
- values(:value, context)
24
+ def row_values(context, result_type = nil) # TODO
25
+ values(:value, result_type, context)
22
26
  end
23
27
 
24
28
  def keys
25
- @columns.map(&:key).flatten
29
+ @keys ||= @columns.map(&:key).flatten
26
30
  end
27
31
 
28
32
  private
@@ -34,28 +38,23 @@ module TableStructure
34
38
  .map { |definition| Column.create(definition, options) }
35
39
  end
36
40
 
37
- def default_column_converters
38
- {}
39
- end
40
-
41
- def default_result_builders(options)
42
- result_builders = {}
43
- if options[:result_type] == :hash
44
- result_builders[:to_h] = ->(array, *) { (@keys ||= keys).zip(array).to_h }
45
- end
46
- result_builders
47
- end
48
-
49
- def values(method, context)
50
- columns = @columns
51
- .map { |column| column.send(method, context, @context) }
52
- .flatten
53
- .map { |val| reduce_callables(@column_converters, val, context) }
54
- reduce_callables(@result_builders, columns, context)
55
- end
56
-
57
- def reduce_callables(callables, val, context)
58
- callables.reduce(val) { |val, (_, callable)| callable.call(val, context, @context) }
41
+ def values(method, result_type, context)
42
+ columns =
43
+ @columns
44
+ .map { |column| column.send(method, context, @context) }
45
+ .flatten
46
+ .map do |val|
47
+ @column_converters.reduce(val) do |val, (_, column_converter)|
48
+ column_converter.call(val, context, @context)
49
+ end
50
+ end
51
+
52
+ RESULT_BUILDERS
53
+ .select { |k, _v| k == result_type }
54
+ .merge(@result_builders)
55
+ .reduce(columns) do |columns, (_, result_builder)|
56
+ result_builder.call(columns, keys, context, @context)
57
+ end
59
58
  end
60
59
  end
61
60
  end
@@ -11,7 +11,7 @@ module TableStructure
11
11
  end
12
12
 
13
13
  DEFAULT_OPTIONS = {
14
- result_type: :array,
14
+ result_type: :array, # deprecated: Change to pass as argument of method.
15
15
  key_prefix: nil,
16
16
  key_suffix: nil
17
17
  }.freeze
@@ -32,14 +32,18 @@ module TableStructure
32
32
  )
33
33
  end
34
34
 
35
- def header(context: nil)
35
+ def header(context: nil, result_type: nil)
36
+ # TODO
37
+ result_type ||= @table_structure_schema_table_.options[:result_type]
36
38
  context = self.class.context_builders[:header].call(context)
37
- @table_structure_schema_table_.header_values(context)
39
+ @table_structure_schema_table_.header_values(context, result_type)
38
40
  end
39
41
 
40
- def row(context: nil)
42
+ def row(context: nil, result_type: nil)
43
+ # TODO
44
+ result_type ||= @table_structure_schema_table_.options[:result_type]
41
45
  context = self.class.context_builders[:row].call(context)
42
- @table_structure_schema_table_.row_values(context)
46
+ @table_structure_schema_table_.row_values(context, result_type)
43
47
  end
44
48
 
45
49
  def column_converters
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TableStructure
4
- VERSION = '0.3.0'
4
+ VERSION = '0.3.1'
5
5
  end
@@ -5,6 +5,7 @@ module TableStructure
5
5
  DEFAULT_OPTIONS = {
6
6
  header_omitted: false,
7
7
  header_context: nil,
8
+ result_type: nil, # TODO: :array
8
9
  method: :<<
9
10
  }.freeze
10
11
 
@@ -16,12 +17,18 @@ module TableStructure
16
17
  def write(items, to:, **options)
17
18
  options = @options.merge(options)
18
19
  unless options[:header_omitted]
19
- header = @schema.header(context: options[:header_context])
20
+ header = @schema.header(
21
+ context: options[:header_context],
22
+ result_type: options[:result_type]
23
+ )
20
24
  header = yield header if block_given?
21
25
  to.send(options[:method], header)
22
26
  end
23
27
  enumerize(items).each do |item|
24
- row = @schema.row(context: item)
28
+ row = @schema.row(
29
+ context: item,
30
+ result_type: options[:result_type]
31
+ )
25
32
  row = yield row if block_given?
26
33
  to.send(options[:method], row)
27
34
  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.0
4
+ version: 0.3.1
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-28 00:00:00.000000000 Z
11
+ date: 2019-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,8 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
110
  requirements: []
111
- rubyforge_project:
112
- rubygems_version: 2.7.6.2
111
+ rubygems_version: 3.0.3
113
112
  signing_key:
114
113
  specification_version: 4
115
114
  summary: Creates and outputs table structured data.