Skip to content

Commit

Permalink
forwarding line number when coerced data raise error
Browse files Browse the repository at this point in the history
  • Loading branch information
Rynaro committed Sep 5, 2019
1 parent 2207539 commit 3651e59
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 12 deletions.
1 change: 0 additions & 1 deletion lib/estratto/data/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def initialize(data, formats = {})
def coerce
raise TypeCoercionNotFound
end

end
end
end
10 changes: 7 additions & 3 deletions lib/estratto/data/coercer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@
module Estratto
module Data
class InvalidCoercionType < StandardError; end

class DataCoercionError < StandardError; end
class Coercer
attr_reader :data, :type, :formats
attr_reader :data, :index, :type, :formats

def initialize(data:, type: 'String', formats: {})
def initialize(data:, index:, type: 'String', formats: {})
@data = data
@index = index
@type = type
@formats = formats
end

def build
target_coercer.coerce
rescue StandardError => error
raise DataCoercionError,
"Error when coercing #{data} on line #{index}, raising: #{error.message}"
end

def target_coercer
Expand Down
4 changes: 2 additions & 2 deletions lib/estratto/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ def initialize(file_path, layout)
end

def perform
@data ||= raw_content.map do |line|
@data ||= raw_content.map.with_index do |line, index|
register_layout = layout.register_fields_for(line[layout.prefix_range])
next if register_layout.nil?
Register.new(line, register_layout).refine
Register.new(line, index, register_layout).refine
end.compact
end

Expand Down
6 changes: 4 additions & 2 deletions lib/estratto/register.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

module Estratto
class Register
attr_reader :line, :register_layout
attr_reader :line, :index, :register_layout

def initialize(line, register_layout)
def initialize(line, index, register_layout)
@line = line
@index = index
@register_layout = register_layout
end

Expand All @@ -22,6 +23,7 @@ def refine

def coerced_data(range, type, formats)
Estratto::Data::Coercer.new(
index: index,
data: line[Estratto::Helpers::Range.for(range)],
type: type,
formats: formats || {}
Expand Down
20 changes: 17 additions & 3 deletions spec/lib/estratto/data/coercer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
RSpec.describe Estratto::Data::Coercer do
subject { described_class.new(data: data, type: type, formats: formats) }
subject { described_class.new(data: data, index: index, type: type, formats: formats) }
let(:data) { 'STAR PLATINUM' }
let(:index) { 1 }
let(:type) { 'String' }
let(:formats) { {} }

describe '#build' do
it do
expect(subject.build).to eq('STAR PLATINUM')
context 'valid coercion build' do
it do
expect(subject.build).to eq('STAR PLATINUM')
end
end

context 'invalid coercion build' do
let(:data) { '20190910' }
let(:type) { 'Date' }
it do
expect{ subject.build }.to raise_error(
Estratto::Data::DataCoercionError,
'Error when coercing 20190910 on line 1, raising: invalid date'
)
end
end
end

Expand Down
4 changes: 3 additions & 1 deletion spec/lib/estratto/register_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RSpec.describe Estratto::Register do
subject { described_class.new(line, layout) }
subject { described_class.new(line, index, layout) }
let(:line) { '42FIREFOX 57192 0.6' }

let(:layout) do
Expand All @@ -10,6 +10,8 @@
]
end

let(:index) { 1 }

describe '#refine' do
it do
expect(subject.refine).to eq(
Expand Down

0 comments on commit 3651e59

Please sign in to comment.