Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  Bump version to 0.4.1
  Update document
  Fix the case of infinite loop
  Run rubocop -a
  Add header step option
  Add header step option
  • Loading branch information
jsmmr committed Apr 12, 2020
2 parents 2e86189 + 1c200a7 commit 90722bb
Show file tree
Hide file tree
Showing 13 changed files with 394 additions and 459 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
@@ -1,7 +1,15 @@
# 0.4.1
Changes:
- `TableStructure::Iterator`
- `TableStructure::Writer`
- `TableStructure::CSV::Writer`
- Add `header: { step: n }` option. Header rows are output at intervals of step number.
- e.g. `TableStructure::Iterator.new(schema, header: { step: 10 })`

# 0.4.0
Changes:
- Remove deprecated methods, arguments and options.
- It is recommended that you update to 0.3.23 first. if you get warnings or errors, fix them and then update to 0.4.0.
- It is recommended that you update to `0.3.23` first. If you get warnings or errors, fix them and then update to `0.4.0`.

# 0.3.23
Changes:
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
table_structure (0.4.0)
table_structure (0.4.1)

GEM
remote: https://rubygems.org/
Expand Down
1 change: 0 additions & 1 deletion lib/table_structure.rb
Expand Up @@ -30,7 +30,6 @@ class Error < StandardError; end
require 'table_structure/table/column_converter'
require 'table_structure/table/context_builder'
require 'table_structure/table/row_builder'
require 'table_structure/table/iterator'
require 'table_structure/writer'
require 'table_structure/csv/writer'
require 'table_structure/iterator'
Expand Down
2 changes: 1 addition & 1 deletion lib/table_structure/csv/writer.rb
Expand Up @@ -9,7 +9,7 @@ def initialize(
schema,
bom: false,
csv_options: {},
header: { context: nil }
header: { context: nil, step: nil }
)
require 'csv'

Expand Down
57 changes: 37 additions & 20 deletions lib/table_structure/iterator.rb
Expand Up @@ -2,39 +2,56 @@

module TableStructure
class Iterator
class HeaderOptions
attr_reader :enabled, :context, :step
alias enabled? enabled

def initialize(options)
@enabled = !!options
if options.is_a?(Hash)
@context = options[:context]
@step = options[:step]
end
end
end

def initialize(
schema,
header: { context: nil },
header: { context: nil, step: nil },
row_type: :array
)
@schema = schema
@options = {
header: header,
row_type: row_type
}
@table = Table.new(schema, row_type: row_type)
@header_options = HeaderOptions.new(header)
end

def iterate(items, &block)
unless items.respond_to?(:each)
raise ::TableStructure::Error, "Must be enumerable. #{items}"
end

enum =
Table::Iterator
.new(
Table.new(@schema, row_type: @options[:row_type]),
header: @options[:header]
)
.iterate(items)

if block_given?
enum =
enum
.lazy
.map { |row| block.call(row) }
table_enum = ::Enumerator.new do |y|
body_enum = @table.body(items)

if @header_options.enabled?
header_row = @table.header(context: @header_options.context)
y << header_row

if @header_options.step&.positive?
loop do
@header_options.step.times { y << body_enum.next }
y << header_row
end
else
body_enum.each { |row| y << row }
end
else
body_enum.each { |row| y << row }
end
end

enum
table_enum = table_enum.lazy.map(&block) if block_given?

table_enum
end
end
end
22 changes: 0 additions & 22 deletions lib/table_structure/table/iterator.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/table_structure/version.rb
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module TableStructure
VERSION = '0.4.0'
VERSION = '0.4.1'
end
2 changes: 1 addition & 1 deletion lib/table_structure/writer.rb
Expand Up @@ -4,7 +4,7 @@ module TableStructure
class Writer
def initialize(
schema,
header: { context: nil },
header: { context: nil, step: nil },
method: :<<,
row_type: :array
)
Expand Down
216 changes: 216 additions & 0 deletions spec/support/shared_contexts.rb
Expand Up @@ -42,3 +42,219 @@
]
end
end

RSpec.shared_context 'table_structured_array' do
let(:header_row) do
[
'ID',
'Name',
'Pet 1',
'Pet 2',
'Pet 3',
'Q1',
'Q2',
'Q3'
]
end

let(:body_row_taro) do
[
1,
'太郎',
'cat',
'dog',
nil,
'yes',
'no',
'yes'
]
end

let(:body_row_hanako) do
[
2,
'花子',
'rabbit',
'turtle',
'squirrel',
'yes',
'yes',
'no'
]
end

let(:body_row_jiro) do
[
3,
'次郎',
'tiger',
'elephant',
'doragon',
'no',
'yes',
nil
]
end
end

RSpec.shared_context 'table_structured_array_with_stringified' do
let(:header_row) do
[
'ID',
'Name',
'Pet 1',
'Pet 2',
'Pet 3',
'Q1',
'Q2',
'Q3'
]
end

let(:body_row_taro) do
[
'1',
'太郎',
'cat',
'dog',
'',
'yes',
'no',
'yes'
]
end

let(:body_row_hanako) do
%w[
2
花子
rabbit
turtle
squirrel
yes
yes
no
]
end

let(:body_row_jiro) do
[
'3',
'次郎',
'tiger',
'elephant',
'doragon',
'no',
'yes',
''
]
end
end

RSpec.shared_context 'table_structured_hash' do
let(:header_row) do
{
id: 'ID',
name: 'Name',
pet1: 'Pet 1',
pet2: 'Pet 2',
pet3: 'Pet 3',
q1: 'Q1',
q2: 'Q2',
q3: 'Q3'
}
end

let(:body_row_taro) do
{
id: 1,
name: '太郎',
pet1: 'cat',
pet2: 'dog',
pet3: nil,
q1: 'yes',
q2: 'no',
q3: 'yes'
}
end

let(:body_row_hanako) do
{
id: 2,
name: '花子',
pet1: 'rabbit',
pet2: 'turtle',
pet3: 'squirrel',
q1: 'yes',
q2: 'yes',
q3: 'no'
}
end

let(:body_row_jiro) do
{
id: 3,
name: '次郎',
pet1: 'tiger',
pet2: 'elephant',
pet3: 'doragon',
q1: 'no',
q2: 'yes',
q3: nil
}
end
end

RSpec.shared_context 'table_structured_hash_with_index_keys' do
let(:header_row) do
{
0 => 'ID',
1 => 'Name',
2 => 'Pet 1',
3 => 'Pet 2',
4 => 'Pet 3',
5 => 'Q1',
6 => 'Q2',
7 => 'Q3'
}
end

let(:body_row_taro) do
{
0 => 1,
1 => '太郎',
2 => 'cat',
3 => 'dog',
4 => nil,
5 => 'yes',
6 => 'no',
7 => 'yes'
}
end

let(:body_row_hanako) do
{
0 => 2,
1 => '花子',
2 => 'rabbit',
3 => 'turtle',
4 => 'squirrel',
5 => 'yes',
6 => 'yes',
7 => 'no'
}
end

let(:body_row_jiro) do
{
0 => 3,
1 => '次郎',
2 => 'tiger',
3 => 'elephant',
4 => 'doragon',
5 => 'no',
6 => 'yes',
7 => nil
}
end
end
4 changes: 2 additions & 2 deletions spec/table_structure/csv/writer_spec.rb
Expand Up @@ -6,7 +6,7 @@
end

let(:inner_writer_options) do
{ header: { context: {} } }
{ header: { context: {}, step: 10 } }
end

let(:csv_writer) do
Expand All @@ -28,7 +28,7 @@
writer = double('TableStructure::Writer')

expect(TableStructure::Writer).to receive(:new)
.with(schema, header: { context: {} })
.with(schema, header: { context: {}, step: 10 })
.and_return(writer)

expect(writer).to receive(:write)
Expand Down

0 comments on commit 90722bb

Please sign in to comment.