-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from lyrasis/multi-multi-merge
Multi multi merge
- Loading branch information
Showing
7 changed files
with
373 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
module Kiba | ||
module Extend | ||
class Fieldset | ||
def initialize(fields) | ||
@hash = {} | ||
fields.each{ |field| @hash[field] = [] } | ||
end | ||
|
||
def add_constant_values(field, value) | ||
@hash[field] = [] | ||
value_ct.times{ @hash[field] << value } | ||
end | ||
|
||
def fields | ||
@hash.keys | ||
end | ||
|
||
def hash | ||
@hash | ||
end | ||
|
||
def join_values(delim) | ||
@hash.transform_values!{ |vals| vals.join(delim) } | ||
end | ||
|
||
def populate(rows) | ||
return if rows.empty? | ||
|
||
rows.each{ |row| get_field_values(row) } | ||
remove_valueless_rows | ||
end | ||
|
||
def value_ct | ||
@hash.values.first.length | ||
end | ||
|
||
private | ||
|
||
def get_field_values(row) | ||
fields.each do |field| | ||
fetched = row.fetch(field, nil) | ||
value = fetched.blank? ? nil : fetched | ||
@hash[field] << value | ||
end | ||
end | ||
|
||
def remove_valueless_rows | ||
valueless_indices.each do |index| | ||
@hash.each{ |field, values| values.delete_at(index) } | ||
end | ||
end | ||
|
||
def valueless_indices | ||
indices = [] | ||
@hash.values.first.each_with_index do |element, i| | ||
indices << i if @hash.values.map{ |vals| vals[i] }.compact.empty? | ||
end | ||
indices.sort.reverse | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module Kiba | ||
module Extend | ||
VERSION = "1.11.0" | ||
VERSION = "1.12.0" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe Kiba::Extend::Fieldset do | ||
let(:rows) { [ | ||
{a: 'aa', b: 'bb', c: 'cc', d: 'dd'}, | ||
{a: 'aa', b: 'bee', c: 'cee', d: 'dd'}, | ||
{a: 'aa', b: nil, c: '', d: 'dd'} | ||
] } | ||
let(:fields) { %i[b c] } | ||
let(:fieldset) { Kiba::Extend::Fieldset.new(fields) } | ||
describe '#fields' do | ||
it 'returns an Array of fields collated by the Fieldset' do | ||
expect(fieldset.fields).to eq(fields) | ||
end | ||
end | ||
describe '#populate' do | ||
it 'populates hash with field values from given rows' do | ||
fieldset.populate(rows) | ||
expected = [['bb', 'bee'], ['cc', 'cee']] | ||
expect(fieldset.hash.values).to eq(expected) | ||
end | ||
end | ||
|
||
describe '#add_constant_values' do | ||
it 'populates hash with constant values' do | ||
fieldset.populate(rows) | ||
fieldset.add_constant_values(:f, 'ffff') | ||
expected = [['bb', 'bee'], ['cc', 'cee'], ['ffff', 'ffff']] | ||
expect(fieldset.hash.values).to eq(expected) | ||
end | ||
|
||
it 'adds field, but does not add constant values to it for empty rows' do | ||
rows = [ | ||
{a: 'aa'}, | ||
{a: 'aa', b: ''} | ||
] | ||
fieldset.populate(rows) | ||
fieldset.add_constant_values(:f, 'ffff') | ||
expected = [[], [], []] | ||
expect(fieldset.hash.values).to eq(expected) | ||
end | ||
end | ||
|
||
describe '#join_values' do | ||
it 'joins hash values' do | ||
fieldset.populate(rows) | ||
fieldset.add_constant_values(:f, 'ffff') | ||
fieldset.join_values('|') | ||
expected = ['bb|bee', 'cc|cee', 'ffff|ffff'] | ||
expect(fieldset.hash.values).to eq(expected) | ||
end | ||
end | ||
end |
Oops, something went wrong.