Skip to content

Commit

Permalink
Merge pull request #12 from lyrasis/sci-notation-to-num_string
Browse files Browse the repository at this point in the history
add MsAccess::ScientificNotationToNumberString tranform
  • Loading branch information
kspurgin authored Apr 23, 2021
2 parents 444d375 + 3e42565 commit aa5f75b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
kiba-extend (1.8.0)
kiba-extend (1.8.1)
activesupport
kiba (>= 4.0.0)
kiba-common (>= 1.5.0)
Expand All @@ -20,7 +20,7 @@ GEM
concurrent-ruby (1.1.8)
diff-lcs (1.3)
facets (3.1.0)
i18n (1.8.9)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
kiba (4.0.0)
kiba-common (1.5.0)
Expand Down
1 change: 1 addition & 0 deletions lib/kiba/extend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module Extend
require 'kiba/extend/transforms/explode'
require 'kiba/extend/transforms/filter_rows'
require 'kiba/extend/transforms/merge'
require 'kiba/extend/transforms/ms_access'
require 'kiba/extend/transforms/prepend'
require 'kiba/extend/transforms/rename'
require 'kiba/extend/transforms/replace'
Expand Down
31 changes: 31 additions & 0 deletions lib/kiba/extend/transforms/ms_access.rb
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
2 changes: 1 addition & 1 deletion lib/kiba/extend/version.rb
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
33 changes: 33 additions & 0 deletions spec/kiba/extend/transforms/ms_access_spec.rb
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

0 comments on commit aa5f75b

Please sign in to comment.