Skip to content

Commit

Permalink
Fixing map_in to ignore pad fields.
Browse files Browse the repository at this point in the history
Tests ajusted to test for this.
  • Loading branch information
alibby committed Nov 30, 2007
1 parent 3069b42 commit dfc3bf1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
42 changes: 31 additions & 11 deletions lib/flat_file.rb
Expand Up @@ -45,29 +45,50 @@
# fd.add_formatter { |v| v.trim }
# .
# .
# .
# }
# end
#
# Filters and Formatters
#
# Filters are applied only when data is read from a file using
# each_record. Formatters are applied any time a Record
# is converted to a string.
# Filters touch data when on the way in to the flat filer
# via each_record or create_record.
#
# Formatters are used when a record is converted into a
# string using to_s.
#
# Structurally, filters and formatters can be lambdas, code
# blocks, or symbols referencing methods.
#
# There's an expectaiton on the part of formatters of the
# type of a field value. This means that the programmer
# needs to set the value of a field as a type that the formatter
# won't bork on.
#
# A good argument can be made to change filtering to happen any
# time a field value is assigned. I've decided to not take this
# route because it'll make writing filters more complex. Take
# for example a date field. The filter is likely to parse a
# date into a Date object. The formatter is likely to convert
# it back to the format desired for a record in a data file.
# route because it'll make writing filters more complex.
#
# If the conversion was done every time a field value is assigned
# An example of this might be a date field. If you've built up
# a date field where a string read from a file is marshalled into
# a Date object. If you assign a string to that field and then
# attempt to export to a file you may run into problems. This is
# because your formatters may not be resiliant enough to handle
# unepxected types.
#
# Until we build this into the system, write resiliant formatters
# OR take risks. Practially speaking, if your system is stable
# with respect to input/ output you're probably going to be fine.
#
# If the filter were run every time a field value is assigned
# to a record, then the filter will need to check the value being
# passed to it and then make a filtering decision based on that.
# This seemed pretty unattractive to me. So it's expected that
# when creating records with new_record, that you assign field
# values in the format that the formatter expect them to be in.
#
# Essentially, robustness needed either be in the filter or formatter,
# due to lazyness, I chose formatter.
#
# Generally this is just anything that can have to_s called
# on it, but if the filter does anything special, be cognizent
# of that when assigning values into fields.
Expand Down Expand Up @@ -234,8 +255,7 @@ def initialize(klass,fields=Hash.new,line_number = -1,&block)
end

def map_in(model)
klass_fields = @klass.get_subclass_variable('fields')
klass_fields.each do |f|
@klass.non_pad_fields.each do |f|
next unless(model.respond_to? "#{f.name}=")
if f.map_in_proc
f.map_in_proc.call(model,self)
Expand Down
13 changes: 7 additions & 6 deletions spec/flat_filer_spec.rb
Expand Up @@ -18,24 +18,25 @@ class PersonFile < FlatFile
:filter => proc { |v| v.to_i },
:formatter => proc { |v| v.to_f.to_s }

add_field :ignore, :width => 6, :padding => true
pad :auto_name, :width => 3
add_field :ignore, :width => 3, :padding => true
end


describe FlatFile do
@@data = <<EOF
1234567890123456789012345678901234567890
f_name l_name age pad---
Captain Stubing 4
No Phone 5
Has Phone 11111111116
Captain Stubing 4 xxx
No Phone 5 xxx
Has Phone 11111111116 xxx
EOF

@@lines = @@data.split("\n")

before :all do
Struct.new("Person", :f_name, :l_name, :phone, :age)
Struct.new("Person", :f_name, :l_name, :phone, :age, :ignore)
@ff = PersonFile.new
end

Expand Down Expand Up @@ -87,6 +88,7 @@ class PersonFile < FlatFile
person = Struct::Person.new('A','Hole','5555555555','4')
rec = @ff.create_record(@@lines[4])
rec.map_in(person)
person.ignore.should eql(nil)
person.f_name.should eql("Has")
end

Expand All @@ -101,7 +103,6 @@ class PersonFile < FlatFile
count.should == num_lines

end

end


0 comments on commit dfc3bf1

Please sign in to comment.