Class: HFLR::RecordTemplate

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

Constant Summary collapse

UNFILLED_CHAR =
' '
MISSING_OUTPUT =
"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.



10
11
12
13
14
15
16
# File 'lib/hflr/record_template.rb', line 10

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.



7
8
9
# File 'lib/hflr/record_template.rb', line 7

def field_pattern
  @field_pattern
end

#record_structureObject (readonly)

Returns the value of attribute record_structure.



7
8
9
# File 'lib/hflr/record_template.rb', line 7

def record_structure
  @record_structure
end

#record_typeObject (readonly)

Returns the value of attribute record_type.



7
8
9
# File 'lib/hflr/record_template.rb', line 7

def record_type
  @record_type
end

#record_type_labelObject (readonly)

Returns the value of attribute record_type_label.



7
8
9
# File 'lib/hflr/record_template.rb', line 7

def record_type_label
  @record_type_label
end

#strip_whitespaceObject

Returns the value of attribute strip_whitespace.



8
9
10
# File 'lib/hflr/record_template.rb', line 8

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hflr/record_template.rb', line 21

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



73
74
75
76
77
# File 'lib/hflr/record_template.rb', line 73

def build_line(record)
  line = format_fields(record).pack(@field_pattern)
  line.tr!("\0", UNFILLED_CHAR)
  line
end

#build_record(line) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hflr/record_template.rb', line 61

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
  data
end