table_structure 0.3.17 → 0.3.18

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: 15d536a6af5a6f759f6e2460568aa1822edc3b15812b4d84c821a5d9750e4275
4
- data.tar.gz: d12f7463aa3b5ee12cfe8023ef77f0ef0ccf1f2d59692f392696ceeef47dc948
3
+ metadata.gz: b76264ce64e9ba5706af0a933cda2a9bbd64d7c4f49a88ac8cf57c2c96a87d7e
4
+ data.tar.gz: e78fa085177724b1fc7a6a5561455588611159226d170840fb4a8ac908feb7aa
5
5
  SHA512:
6
- metadata.gz: 5f8008644303fdea9135a1d1a6d7e26cac50c388062982723c7e9564589c96d3eb70346a2a12a216c2fc2c13abe52e4f0d8b7860fbc652ff1878cce70b505b74
7
- data.tar.gz: 719615ac3af76a63c0fa616f4fc2e8195d806dd4e773987ce0f4274eff58647ddab6b460b6a13fbfb72d7709d93e26ca30f427202132e8ab4ddd13cedf7f9c3d
6
+ metadata.gz: a767059b75a19a19ad4eff6fb16134ac4cc345bb5dd0878f7d434db274e9ae37c0a708b73e52611eea83deb0e78edd1ed891bcccef666674726c67f43bec699c
7
+ data.tar.gz: 94c598813253ca9ae1d20d5853567cfb3d45ef4bb161c69b879988f144265449291b3a3130ca894c8a0aac2545eb13e77e1433ba48fa539cc6541d3cddaa7804
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 0.3.18
2
+ Changes:
3
+ - Minor improvements.
4
+
1
5
  # 0.3.17
2
6
  Changes:
3
7
  - `TableStructure::Schema`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- table_structure (0.3.17)
4
+ table_structure (0.3.18)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -219,10 +219,10 @@ enum.each do |item|
219
219
  end
220
220
 
221
221
  enum.map(&:itself)
222
- # => [{:id=>"1", :name=>"Taro", :pet1=>"🐱", :pet2=>"🐶", :pet3=>"", :q1=>"⭕️", :q2=>"❌", :q3=>"⭕️"}, {:id=>"2", :name=>"Hanako", :pet1=>"🐇", :pet2=>"🐢", :pet3=>"🐿", :q1=>"⭕️", :q2=>"⭕️", :q3=>"❌"}]
222
+ # => [{:id=>1, :name=>"Taro", :pet1=>"🐱", :pet2=>"🐶", :pet3=>nil, :q1=>"⭕️", :q2=>"❌", :q3=>"⭕️"}, {:id=>2, :name=>"Hanako", :pet1=>"🐇", :pet2=>"🐢", :pet3=>"🐿", :q1=>"⭕️", :q2=>"⭕️", :q3=>"❌"}]
223
223
 
224
224
  enum.lazy.select { |item| item[:q1] == '⭕️' }.take(1).force
225
- # => [{:id=>"1", :name=>"Taro", :pet1=>"🐱", :pet2=>"🐶", :pet3=>"", :q1=>"⭕️", :q2=>"❌", :q3=>"⭕️"}]
225
+ # => [{:id=>1, :name=>"Taro", :pet1=>"🐱", :pet2=>"🐶", :pet3=>nil, :q1=>"⭕️", :q2=>"❌", :q3=>"⭕️"}]
226
226
  ```
227
227
 
228
228
  ### Advanced
@@ -24,11 +24,12 @@ module TableStructure
24
24
  require 'table_structure/schema/column_converters'
25
25
  require 'table_structure/schema/context_builders'
26
26
  require 'table_structure/schema/row_builders'
27
- require 'table_structure/schema/table'
28
27
  require 'table_structure/schema/keys_generator'
28
+ require 'table_structure/schema/table'
29
29
  require 'table_structure/schema/columns/attributes'
30
30
  require 'table_structure/schema/columns/schema'
31
31
  require 'table_structure/schema/utils'
32
+ require 'table_structure/table/iterator'
32
33
  require 'table_structure/writer'
33
34
  require 'table_structure/csv/writer'
34
35
  require 'table_structure/iterator'
@@ -7,13 +7,15 @@ module TableStructure
7
7
  schema = schema_or_writer
8
8
  @writer = Writer.new(schema, **options)
9
9
  elsif schema_or_writer.is_a?(Writer)
10
+ warn "[TableStructure] Pass Writer as an argument has been deprecated. Pass Schema instead. #{caller_locations(1, 1)}"
10
11
  @writer = schema_or_writer
11
12
  else
12
- raise ::TableStructure::Error, "Must be either a schema or a writer. #{schema_or_writer}"
13
+ raise ::TableStructure::Error, "Must be either Schema or Writer. #{schema_or_writer}"
13
14
  end
14
15
  end
15
16
 
16
17
  def iterate(items, **options, &block)
18
+ # TODO: Change not to use Writer.
17
19
  Enumerator.new { |y| @writer.write(items, to: y, **options, &block) }
18
20
  end
19
21
  end
@@ -45,7 +45,7 @@ module TableStructure
45
45
  unless deprecated_options.empty?
46
46
  caller_location = caller_locations(1, 1)
47
47
  deprecated_options.keys.each do |k|
48
- warn "[TableStructure] Specify :#{k} option on the writer or the iterator. #{caller_location}"
48
+ warn "[TableStructure] Specify :#{k} option on Writer or Iterator. #{caller_location}"
49
49
  end
50
50
  end
51
51
 
@@ -96,9 +96,8 @@ module TableStructure
96
96
  )
97
97
  end
98
98
 
99
- # TODO: Specify options using keyword arguments.
100
- def create_table(**options)
101
- options = @_definition_.options.merge(options)
99
+ def create_table(row_type: :array, **deprecated_options)
100
+ options = @_definition_.options.merge(deprecated_options)
102
101
 
103
102
  if options.key?(:result_type)
104
103
  warn '[TableStructure] `:result_type` option has been deprecated. Use `:row_type` option instead.'
@@ -134,7 +133,7 @@ module TableStructure
134
133
  .extend_methods_for(table, **column_converters_options)
135
134
 
136
135
  row_builders_options = {
137
- row_type: options[:row_type]
136
+ row_type: options[:row_type] || row_type
138
137
  }
139
138
 
140
139
  @_definition_
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TableStructure
4
+ module Table
5
+ class Iterator
6
+ def initialize(table, header: { context: nil })
7
+ @table = table
8
+ @header_options = header
9
+ end
10
+
11
+ def iterate(items)
12
+ ::Enumerator.new do |y|
13
+ if @header_options
14
+ header_context = @header_options.is_a?(Hash) ? @header_options[:context] : nil
15
+ y << @table.header(context: header_context)
16
+ end
17
+ @table
18
+ .body(items)
19
+ .each { |row| y << row }
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TableStructure
4
- VERSION = '0.3.17'
4
+ VERSION = '0.3.18'
5
5
  end
@@ -2,37 +2,88 @@
2
2
 
3
3
  module TableStructure
4
4
  class Writer
5
- DEFAULT_OPTIONS = {
5
+ def initialize(
6
+ schema,
6
7
  header_omitted: false,
7
8
  header_context: nil,
8
- method: :<<
9
- }.freeze
9
+ method: :<<,
10
+ row_type: :array,
11
+ **deprecated_options
12
+ )
13
+ if deprecated_options.key?(:result_type)
14
+ warn '[TableStructure] `:result_type` option has been deprecated. Use `:row_type` option instead.'
15
+ row_type = deprecated_options[:result_type]
16
+ end
10
17
 
11
- def initialize(schema, **options)
12
18
  @schema = schema
13
- @options = DEFAULT_OPTIONS.merge(options)
19
+ @options = {
20
+ header_omitted: header_omitted,
21
+ header_context: header_context,
22
+ method: method,
23
+ row_type: row_type
24
+ }
14
25
  end
15
26
 
16
- def write(items, to:, **options)
17
- options = @options.merge(options)
18
- @schema.create_table(**options) do |table|
19
- unless options[:header_omitted]
20
- header = table.header(
21
- context: options[:header_context]
22
- )
23
- header = yield header if block_given?
24
- to.send(options[:method], header)
27
+ def write(
28
+ items,
29
+ to:,
30
+ method: @options[:method],
31
+ header_omitted: @options[:header_omitted],
32
+ header_context: @options[:header_context],
33
+ row_type: @options[:row_type],
34
+ **deprecated_options,
35
+ &block
36
+ )
37
+ if deprecated_options.key?(:result_type)
38
+ warn '[TableStructure] `:result_type` option has been deprecated. Use `:row_type` option instead.'
39
+ row_type = deprecated_options[:result_type]
40
+ end
41
+
42
+ items = enumerize(items)
43
+
44
+ header_options =
45
+ if header_omitted
46
+ false
47
+ else
48
+ { context: header_context }
25
49
  end
26
- table.body(enumerize(items)).each do |row|
27
- row = yield row if block_given?
28
- to.send(options[:method], row)
50
+
51
+ @schema.create_table(row_type: row_type) do |table|
52
+ output = Output.new(to, method: method)
53
+
54
+ enum =
55
+ Table::Iterator
56
+ .new(
57
+ table,
58
+ header: header_options
59
+ )
60
+ .iterate(items)
61
+
62
+ if block_given?
63
+ enum =
64
+ enum
65
+ .lazy
66
+ .map { |row| block.call(row) }
29
67
  end
68
+
69
+ enum.each { |row| output.write(row) }
30
70
  end
31
71
  nil
32
72
  end
33
73
 
34
74
  private
35
75
 
76
+ class Output
77
+ def initialize(output, method: :<<)
78
+ @output = output
79
+ @method = method
80
+ end
81
+
82
+ def write(values)
83
+ @output.send(@method, values)
84
+ end
85
+ end
86
+
36
87
  def enumerize(items)
37
88
  if items.respond_to?(:each)
38
89
  items
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.17
4
+ version: 0.3.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - jsmmr
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-25 00:00:00.000000000 Z
11
+ date: 2020-02-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -97,6 +97,7 @@ files:
97
97
  - lib/table_structure/schema/row_builders.rb
98
98
  - lib/table_structure/schema/table.rb
99
99
  - lib/table_structure/schema/utils.rb
100
+ - lib/table_structure/table/iterator.rb
100
101
  - lib/table_structure/version.rb
101
102
  - lib/table_structure/writer.rb
102
103
  - table_structure.gemspec