-
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 #12 from lyrasis/sci-notation-to-num_string
add MsAccess::ScientificNotationToNumberString tranform
- Loading branch information
Showing
5 changed files
with
68 additions
and
3 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,31 @@ | ||
require 'bigdecimal' | ||
|
||
module Kiba | ||
module Extend | ||
module Transforms | ||
module MsAccess | ||
::MsAccess = Kiba::Extend::Transforms::MsAccess | ||
class ScientificNotationToNumberString | ||
def initialize(fields:) | ||
@fields = fields | ||
end | ||
|
||
def process(row) | ||
@fields.each{ |field| process_field(row, field) } | ||
row | ||
end | ||
|
||
private | ||
|
||
def process_field(row, field) | ||
value = row[field] | ||
return if value.blank? | ||
return unless value.match?(/[Ee][-+]/) | ||
row[field] = BigDecimal(value).to_s.sub(/\.0+$/, '') | ||
#"%f" % value | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module Kiba | ||
module Extend | ||
VERSION = "1.8.1" | ||
VERSION = "1.9.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,33 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe Kiba::Extend::Transforms::MsAccess do | ||
describe 'ScientificNotationToNumberString' do | ||
test_csv = 'tmp/test.csv' | ||
rows = [ | ||
['id', 'width'], | ||
[1, '1.70000000e+01'], | ||
[2, '170'], | ||
[3, ''], | ||
[4, nil], | ||
[5, '1.0e-10'] | ||
] | ||
|
||
before do | ||
generate_csv(test_csv, rows) | ||
end | ||
it 'converts scientific notation value to number string' do | ||
expected = [ | ||
{id: '1', width: '17'}, | ||
{id: '2', width: '170'}, | ||
{id: '3', width: ''}, | ||
{id: '4', width: nil}, | ||
{id: '5', width: '0.0000000001'} | ||
] | ||
result = execute_job(filename: test_csv, | ||
xform: MsAccess::ScientificNotationToNumberString, | ||
xformopt: {fields: %i[width]}) | ||
expect(result).to eq(expected) | ||
end | ||
end | ||
end | ||
|