Class: HFLR::RecordTemplate
- Inherits:
-
Object
- Object
- HFLR::RecordTemplate
- Defined in:
- lib/hflr/record_template.rb
Constant Summary collapse
- UnfilledChar =
' '- MissingOutput =
"ZZZZZZZZZZZZZZZZZZZZZ"
Instance Attribute Summary collapse
-
#field_pattern ⇒ Object
readonly
Returns the value of attribute field_pattern.
-
#record_structure ⇒ Object
readonly
Returns the value of attribute record_structure.
-
#record_type ⇒ Object
readonly
Returns the value of attribute record_type.
-
#record_type_label ⇒ Object
readonly
Returns the value of attribute record_type_label.
-
#strip_whitespace ⇒ Object
Returns the value of attribute strip_whitespace.
Class Method Summary collapse
-
.add_extra_columns(names, extra) ⇒ Object
If the name exists already do not replace it, but add extra columns not to be mapped by the unpack field patterns and ensure the record_type variable is added.
-
.create(record_layouts, record_type_symbols, first_column_location, extra_columns = []) ⇒ Object
Layouts is a hash of variables by record type record_type_symbols maps record type names to their labels in the data :household=>“H”,:person=>“P” Returns a set of record templates, one for each record type.
- .get_pattern(layout, first_column_location = 0) ⇒ Object
Instance Method Summary collapse
- #build_line(record) ⇒ Object
- #build_record(line) ⇒ Object
-
#initialize(record_type, record_type_label, record_structure, field_pattern, field_widths) ⇒ RecordTemplate
constructor
A new instance of RecordTemplate.
Constructor Details
#initialize(record_type, record_type_label, record_structure, field_pattern, field_widths) ⇒ RecordTemplate
Returns a new instance of RecordTemplate.
11 12 13 14 15 16 17 |
# File 'lib/hflr/record_template.rb', line 11 def initialize(record_type, record_type_label, record_structure,field_pattern, field_widths) @record_type = record_type @record_type_label = record_type_label @record_structure = record_structure @field_pattern = field_pattern @field_widths = field_widths end |
Instance Attribute Details
#field_pattern ⇒ Object (readonly)
Returns the value of attribute field_pattern.
8 9 10 |
# File 'lib/hflr/record_template.rb', line 8 def field_pattern @field_pattern end |
#record_structure ⇒ Object (readonly)
Returns the value of attribute record_structure.
8 9 10 |
# File 'lib/hflr/record_template.rb', line 8 def record_structure @record_structure end |
#record_type ⇒ Object (readonly)
Returns the value of attribute record_type.
8 9 10 |
# File 'lib/hflr/record_template.rb', line 8 def record_type @record_type end |
#record_type_label ⇒ Object (readonly)
Returns the value of attribute record_type_label.
8 9 10 |
# File 'lib/hflr/record_template.rb', line 8 def record_type_label @record_type_label end |
#strip_whitespace ⇒ Object
Returns the value of attribute strip_whitespace.
9 10 11 |
# File 'lib/hflr/record_template.rb', line 9 def strip_whitespace @strip_whitespace end |
Class Method Details
.add_extra_columns(names, extra) ⇒ Object
If the name exists already do not replace it, but add extra columns not to be mapped by the unpack field patterns and ensure the record_type variable is added. Since ‘record_type’ may not be in the metadata we don’t want to map it to a specific column location but do want it included always.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/hflr/record_template.rb', line 44 def self.add_extra_columns(names, extra) new_names = names.dup # names are not case sensitive extra.each{|n|new_names << n unless names.map{|m| m.to_s.upcase}.include? n.to_s.upcase} # No matter what, include 'record_type' unless new_names.map{|n| n.to_s.upcase}.include?("RECORD_TYPE") new_names << :record_type end return new_names end |
.create(record_layouts, record_type_symbols, first_column_location, extra_columns = []) ⇒ Object
Layouts is a hash of variables by record type record_type_symbols maps record type names to their labels in the data :household=>“H”,:person=>“P” Returns a set of record templates, one for each record type
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/hflr/record_template.rb', line 22 def self.create(record_layouts, record_type_symbols, first_column_location, extra_columns=[]) extra_columns = empty_extra_columns(record_layouts.keys) if extra_columns.is_a? Array templates = {} self.check_record_layouts(record_layouts) record_layouts.keys.each do |record_type| record_label = record_type_symbols == :none ? :none : record_type_symbols[record_type] templates[record_type] = self.create_template_class(record_type, record_label, record_layouts[record_type], first_column_location, extra_columns[record_type]) end return templates end |
.get_pattern(layout, first_column_location = 0) ⇒ Object
56 57 58 59 60 |
# File 'lib/hflr/record_template.rb', line 56 def self.get_pattern(layout, first_column_location=0) layout.map {|l| '@' + (l.start - first_column_location).to_s + 'A' + l.len.to_s}.to_s end |
Instance Method Details
#build_line(record) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/hflr/record_template.rb', line 74 def build_line(record) line = format_fields(record).pack(@field_pattern) line[0] = @record_type_label unless @record_type_label == :none line.tr!("\0",UnfilledChar) return line end |
#build_record(line) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/hflr/record_template.rb', line 62 def build_record(line) rec = line.unpack(@field_pattern) rec.map{|f| f.strip!} if @strip_whitespace begin data = self.record_structure.new(*rec) data[:record_type] = @record_type rescue Exception=>msg raise "On record type #{self.record_type} problem with structure " + msg.to_s end return data end |