Class: FixedWidthFileValidator::RecordFormatter
- Inherits:
-
Object
- Object
- FixedWidthFileValidator::RecordFormatter
- Defined in:
- lib/fixed_width_file_validator/record_formatter.rb
Instance Attribute Summary collapse
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
-
#field_list ⇒ Object
readonly
Returns the value of attribute field_list.
Instance Method Summary collapse
-
#initialize(field_list, encoding = nil) ⇒ RecordFormatter
constructor
A new instance of RecordFormatter.
- #record_width ⇒ Object
- #string_for(record) ⇒ Object
Constructor Details
#initialize(field_list, encoding = nil) ⇒ RecordFormatter
Returns a new instance of RecordFormatter.
5 6 7 8 |
# File 'lib/fixed_width_file_validator/record_formatter.rb', line 5 def initialize(field_list, encoding = nil) @field_list = field_list.sort_by { |a| a[:position] } @encoding = encoding || 'ISO-8859-1' end |
Instance Attribute Details
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
3 4 5 |
# File 'lib/fixed_width_file_validator/record_formatter.rb', line 3 def encoding @encoding end |
#field_list ⇒ Object (readonly)
Returns the value of attribute field_list.
3 4 5 |
# File 'lib/fixed_width_file_validator/record_formatter.rb', line 3 def field_list @field_list end |
Instance Method Details
#record_width ⇒ Object
10 11 12 13 |
# File 'lib/fixed_width_file_validator/record_formatter.rb', line 10 def record_width last_field = @field_list.last last_field[:position] + last_field[:width] - 1 end |
#string_for(record) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/fixed_width_file_validator/record_formatter.rb', line 15 def string_for(record) out = '' last_pos = 1 @field_list.each do |field| current_pos = field[:position] out << ' ' * (current_pos - last_pos) out << formatted_value(record[field[:name]], field[:format], field[:width]) last_pos = current_pos + field[:width] end out end |