Class: HFLR::RecordTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/hflr/record_template.rb

Constant Summary collapse

UnfilledChar =
' '
MissingOutput =
"ZZZZZZZZZZZZZZZZZZZZZ"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_patternObject (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_structureObject (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_typeObject (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_labelObject (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_whitespaceObject

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

.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.each_pair do |record_type, vars|    
    record_label = record_type_symbols == :none ? :none : record_type_symbols[record_type]
    templates[record_type] = 
    self.create_template_class(record_type,
      record_label,
    vars, 
    first_column_location, 
    extra_columns[record_type]) 
  end
  return templates  
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