Module: ACH::FieldIdentifiers

Included in:
ACHFile, Records::Record
Defined in:
lib/ach/field_identifiers.rb

Instance Method Summary collapse

Instance Method Details

#const_field(name, val) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/ach/field_identifiers.rb', line 48

def const_field(name, val)
  fields << name
  
  # to_ach
  define_method  "#{name}_to_ach" do
    val
  end
end

#field(name, klass, stringify = nil, default = nil, validate = nil, msg = '') ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ach/field_identifiers.rb', line 3

def field(name, klass, stringify = nil, default = nil, validate = nil, msg ='')
  fields << name
  
  # getter
  define_method name do
    instance_variable_get( "@#{name}" )
  end
  
  # setter (includes validations)
  define_method "#{name}=" do | val |
    if validate.kind_of?(Regexp)
      unless val =~ validate
        raise RuntimeError, "#{val} does not match Regexp #{validate}"
      end
    elsif validate.respond_to?(:call) # Proc with value as argument
      unless validate.call(val)
        raise RuntimeError, "#{val} does not pass validation Proc"
      end
    end
    
    instance_variable_set( "@#{name}", val )
  end
  
  # to_ach
  define_method  "#{name}_to_ach" do
    val = instance_variable_get( "@#{name}" )
    
    if val.nil?
      if default.kind_of?(Proc)
        val = default.call
      elsif default
        val = default
      else
        raise RuntimeError, "val is nil"
      end
    end
    
    if stringify.nil?
      return val
    else
      stringify.call(val)
    end
  end
end

#left_justify(val, length) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/ach/field_identifiers.rb', line 57

def left_justify(val, length)
  val_length = val.length
  if val_length > length
    val = val[0..(length - 1)]
  else
    val = val + (' ' * (length - val_length))
  end
end

#routing_field(sym) ⇒ Object

A routing number, usually, a string consisting of exactly nine digits. Represented by ‘bTTTTAAAAC’.



68
69
70
71
# File 'lib/ach/field_identifiers.rb', line 68

def routing_field(sym)
  field sym, String, lambda {|f| ' ' + f}, nil, /\A\d{9}\Z/,
    'A string consisting of exactly nine digits'
end

#spaceless_routing_field(sym) ⇒ Object

A routing number without leading space



74
75
76
77
# File 'lib/ach/field_identifiers.rb', line 74

def spaceless_routing_field(sym)
  field sym, String, lambda {|f| f}, nil, /\A\d{9}\Z/,
    'A string consisting of exactly nine digits'
end