Module: ACH::FieldIdentifiers
- Included in:
- ACHFile, Records::Record
- Defined in:
- lib/ach/field_identifiers.rb
Constant Summary collapse
- ENCODING_OPTIONS =
these are used to convert non ascii characters to UTF-8
{ :invalid => :replace, # Replace invalid byte sequences :undef => :replace, # Replace anything not defined in ASCII :replace => '', # Use a blank for those replacements }
Instance Method Summary collapse
- #const_field(name, val) ⇒ Object
-
#field(name, klass, stringify = nil, default = nil, validate = nil, msg = '') ⇒ Object
NOTE: the msg parameter is unused and should be removed when the API can change.
-
#left_justify(val, length) ⇒ Object
Left justify value and truncate to length if needed.
-
#spaceless_routing_field(sym) ⇒ Object
A routing number without leading space.
Instance Method Details
#const_field(name, val) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/ach/field_identifiers.rb', line 68 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
NOTE: the msg parameter is unused and should be removed when the API can change
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/ach/field_identifiers.rb', line 13 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 val.nil? && default # Leave value as nil, so that default is used. elsif validate.kind_of?(Regexp) unless val =~ validate raise InvalidError, "#{val} does not match Regexp #{validate} for field #{name}" end elsif validate.respond_to?(:call) # Proc with value as argument unless validate.call(val) raise InvalidError, "#{val} does not pass validation Proc for field #{name}" 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 of #{name} is nil" end end if val.kind_of?(String) if RUBY_VERSION < '1.9' val = Iconv.conv('ASCII//IGNORE', 'UTF8', val) else val = val.encode(Encoding.find('ASCII'), **ENCODING_OPTIONS) end end if stringify stringify.call(val) else val end end end |
#left_justify(val, length) ⇒ Object
Left justify value and truncate to length if needed
78 79 80 |
# File 'lib/ach/field_identifiers.rb', line 78 def left_justify(val, length) val[0..(length - 1)].ljust(length) end |
#spaceless_routing_field(sym) ⇒ Object
A routing number without leading space
83 84 85 |
# File 'lib/ach/field_identifiers.rb', line 83 def spaceless_routing_field(sym) field sym, String, nil, nil, /\A\d{9}\z/ end |