table_structure 0.3.10 → 0.3.11

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: 54e6d65c5123ccefa563ba83cfa9ef28fe25ee407224c0838afe17158afe5bbc
4
- data.tar.gz: bb6cdd925323a59eb93d77c610a09b2136c6ee96e6cf618e2b0502171f2bdd4d
3
+ metadata.gz: ccde21d51d86d14d5aba71a9924bcc8d3669032b380005019866699cf5a9fd87
4
+ data.tar.gz: 31d7f45528e0972886db3a2f07f53cd030671bbabc79dda03e6728262da56d95
5
5
  SHA512:
6
- metadata.gz: 5c2c95aed368f8e5afa555e4fc335b96d69e4ec307626bd00bc57a71f1b15b0601074f80321d02652a38ae74ece1d548be6277722786b7ba1c431813d8716f2c
7
- data.tar.gz: fcb0d97b7f2d92ce49fab996d5472272db653163eaade94ab08a165b70ab04a9c1a0ffc732783e6375ea8eed65708d2e010a09c28503d103ca9791112d1c8aaa
6
+ metadata.gz: '04844310bb6ebc8eb89aa78d9ee4f350ffa566d581d3acd555bdef70271326d674cb2e8dad9843b243a46daea29f0fe0ac879eeaa15da7b68b34c5c93dc670e4'
7
+ data.tar.gz: aebff92798ac40979500d660e4392eb567bcf4f0efbbc825859fc9a60b5b8be52e458776adab3665243706df995314834512335a7e0aa01f0b526b65d6509f6e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 0.3.11
2
+ Changes:
3
+ - `TableStructure::CSV::Writer`
4
+ - Add `:csv_options` option. This option's value is simply passed to `::CSV.new`'s options.
5
+
1
6
  # 0.3.10
2
7
  Changes:
3
8
  - `TableStructure::Writer`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- table_structure (0.3.10)
4
+ table_structure (0.3.11)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -5,27 +5,51 @@ module TableStructure
5
5
  class Writer
6
6
  DEFAULT_OPTIONS = {
7
7
  bom: false,
8
- header_omitted: false,
9
- header_context: nil
8
+ csv_options: {}
10
9
  }.freeze
11
10
 
12
- FIXED_OPTIONS = {
13
- result_type: :array,
14
- method: :<<
15
- }.freeze
11
+ INNER_WRITER_DEFAULT_OPTIONS = ::TableStructure::Writer::DEFAULT_OPTIONS
12
+ INNER_WRITER_IGNORED_OPTION_KEYS = %i[
13
+ result_type
14
+ method
15
+ ].freeze
16
16
 
17
17
  BOM = "\uFEFF"
18
18
 
19
19
  def initialize(schema, **options)
20
20
  require 'csv'
21
- @options = DEFAULT_OPTIONS.merge(options).merge(FIXED_OPTIONS)
22
- @writer = ::TableStructure::Writer.new(schema, **@options)
21
+ @options = DEFAULT_OPTIONS.merge(select_csv_writer_options(options))
22
+ inner_writer_options = select_inner_writer_options(options)
23
+ @writer = ::TableStructure::Writer.new(schema, **inner_writer_options)
23
24
  end
24
25
 
25
26
  def write(items, to:, **options, &block)
26
- options = @options.merge(options).merge(FIXED_OPTIONS)
27
- to.send(options[:method], BOM) if options[:bom]
28
- @writer.write(items, to: ::CSV.new(to), **options, &block)
27
+ csv_writer_options = @options.merge(select_csv_writer_options(options))
28
+ inner_writer_options = select_inner_writer_options(options)
29
+ if csv_writer_options[:bom]
30
+ to.send(INNER_WRITER_DEFAULT_OPTIONS[:method], BOM)
31
+ end
32
+ csv = ::CSV.new(to, **csv_writer_options[:csv_options])
33
+ @writer.write(items, to: csv, **inner_writer_options, &block)
34
+ end
35
+
36
+ private
37
+
38
+ def select_csv_writer_options(options)
39
+ options
40
+ .select { |k, _v| DEFAULT_OPTIONS.include?(k) }
41
+ end
42
+
43
+ def select_inner_writer_options(options)
44
+ options
45
+ .select { |k, _v| INNER_WRITER_DEFAULT_OPTIONS.include?(k) }
46
+ .reject do |k, v|
47
+ ignored = INNER_WRITER_IGNORED_OPTION_KEYS.include?(k)
48
+ if ignored
49
+ warn "[TableStructure] #{self.class.name} ignores `#{k}:#{v}` option."
50
+ end
51
+ ignored
52
+ end
29
53
  end
30
54
  end
31
55
  end
@@ -14,13 +14,13 @@ module TableStructure
14
14
  @options = options
15
15
  end
16
16
 
17
- def name(header_context, table_context)
18
- name = Utils.evaluate_callable(@name, header_context, table_context)
17
+ def name(context, table_context)
18
+ name = Utils.evaluate_callable(@name, context, table_context)
19
19
  optimize_size(name, @size)
20
20
  end
21
21
 
22
- def value(row_context, table_context)
23
- value = Utils.evaluate_callable(@value, row_context, table_context)
22
+ def value(context, table_context)
23
+ value = Utils.evaluate_callable(@value, context, table_context)
24
24
  optimize_size(value, @size)
25
25
  end
26
26
 
@@ -3,7 +3,7 @@
3
3
  module TableStructure
4
4
  module Schema
5
5
  class Table
6
- # TODO: Following `attr_reader` will be removed after version 1.4.0.
6
+ # TODO: Remove following `attr_reader`.
7
7
  attr_reader :header_column_converters, :row_column_converters, :result_builders
8
8
 
9
9
  def initialize(
@@ -55,11 +55,11 @@ module TableStructure
55
55
  end
56
56
 
57
57
  def header(context: nil)
58
- values(:name, context, @header_column_converters)
58
+ values(:name, context)
59
59
  end
60
60
 
61
61
  def row(context: nil)
62
- values(:value, context, @row_column_converters)
62
+ values(:value, context)
63
63
  end
64
64
 
65
65
  private
@@ -75,10 +75,10 @@ module TableStructure
75
75
  end
76
76
 
77
77
  def size
78
- @size ||= @columns.map(&:size).reduce(0) { |memo, size| memo + size }
78
+ @size ||= @columns.map(&:size).reduce(0, &:+)
79
79
  end
80
80
 
81
- def values(method, context, _converters)
81
+ def values(method, context)
82
82
  @columns
83
83
  .map { |column| column.send(method, context, @context) }
84
84
  .flatten
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TableStructure
4
- VERSION = '0.3.10'
4
+ VERSION = '0.3.11'
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.10
4
+ version: 0.3.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - jsmmr
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-22 00:00:00.000000000 Z
11
+ date: 2019-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler