Class: FlatFile::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/flat_file/record.rb

Overview

A record abstracts on line or ‘record’ of a fixed width field. The methods available are the kes of the hash passed to the constructor. For example the call:

h = Hash['first_name','Andy','status','Supercool!']
r = Record.new(h)

would respond to r.first_name, and r.status yielding ‘Andy’ and ‘Supercool!’ respectively.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, fields = Hash.new, line_number = -1,) {|block, _self| ... } ⇒ Record

Create a new Record from a hash of fields

Yields:

  • (block, _self)

Yield Parameters:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/flat_file/record.rb', line 18

def initialize(klass,fields=Hash.new,line_number = -1,&block)
  @fields = Hash.new()
  @klass = klass
  @line_number = line_number

  klass_fields = klass.get_subclass_variable('fields')

  klass_fields.each do |f|
    @fields.store(f.name, "")
  end

  @fields.merge!(fields)

  @fields.each_key do |k|
    @fields.delete(k) unless klass.has_field?(k)
  end

  yield(block, self)if block_given?

  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, params = nil) ⇒ Object

Catches method calls and returns field values or raises an Error.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/flat_file/record.rb', line 53

def method_missing(method,params=nil)
  if(method.to_s.match(/^(.*)=$/))
    if(fields.has_key?($1.to_sym))
      @fields.store($1.to_sym,params)
    else
      raise Exception.new("Unknown method: #{ method }")
    end
  else
    if(fields.has_key? method)
      @fields.fetch(method)
    else
      raise Exception.new("Unknown method: #{ method }")
    end
  end
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



13
14
15
# File 'lib/flat_file/record.rb', line 13

def fields
  @fields
end

#klassObject (readonly)

Returns the value of attribute klass.



14
15
16
# File 'lib/flat_file/record.rb', line 14

def klass
  @klass
end

#line_numberObject (readonly)

Returns the value of attribute line_number.



15
16
17
# File 'lib/flat_file/record.rb', line 15

def line_number
  @line_number
end

Instance Method Details

#debug_stringObject

Produces a multiline string, one field per line suitable for debugging purposes.



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/flat_file/record.rb', line 84

def debug_string
  str = ""
  klass.fields.each do |f|
    if f.is_padding?
      str << "#{f.name}: \n"
    else
      str << "#{f.name}: #{send(f.name.to_sym)}\n"
    end
  end

  str
end

#map_in(model) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/flat_file/record.rb', line 40

def map_in(model)
  @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)
    else
      model.send("#{f.name}=", send(f.name)) if f.aggressive or model.send(f.name).blank?
      model.send("#{f.name}=", f.default) if model.send(f.name).blank?
    end
  end
end

#to_sObject

Returns a string representation of the record suitable for writing to a flat file on disk or other media. The fields are parepared according to the file definition, and any formatters attached to the field definitions.



72
73
74
75
76
77
78
79
80
81
# File 'lib/flat_file/record.rb', line 72

def to_s
  klass.fields.map { |field_def|
    field_name = field_def.name.to_s
    v = @fields[ field_name.to_sym ].to_s

    field_def.pass_through_formatters(
      field_def.is_padding? ? "" : v
    )
  }.pack(klass.pack_format)
end