class CnabRb::Format::Line

Public Class Methods

new(layout, attributes = {}) click to toggle source
# File lib/cnab_rb/format/line.rb, line 3
def initialize(layout, attributes = {})
  @layout = layout
  @attributes = attributes
end

Public Instance Methods

decode(text) click to toggle source
# File lib/cnab_rb/format/line.rb, line 17
def decode(text)
  @layout.validate!
  @layout.fields.each do |key, field|
    substr = text[field.pos_start - 1, field.length]
    @attributes[key] = field.decode(substr)
  end
end
encode() click to toggle source
# File lib/cnab_rb/format/line.rb, line 8
def encode
  @layout.validate!
  pieces = @layout.ordered_fields.map do |field_array|
    key, field = field_array
    field.encode(@attributes[key] || field.default)
  end
  pieces.join
end
field_exists(field_name) click to toggle source
# File lib/cnab_rb/format/line.rb, line 49
def field_exists(field_name)
  @attributes.key? field_name.to_sym
end
method_missing(method_name, *args) click to toggle source
# File lib/cnab_rb/format/line.rb, line 25
def method_missing(method_name, *args)
  if @attributes.key? method_name.to_sym
    return @attributes[method_name.to_sym]
  end

  if method_name =~ /(.+)=/
    return @attributes[$1.to_sym] = args[0]
  end

  raise ArgumentError.new("Method `#{method_name}` doesn't exist.")
end
respond_to_missing?(method_name) click to toggle source
# File lib/cnab_rb/format/line.rb, line 37
def respond_to_missing?(method_name)
  if @attributes.key? method_name.to_sym
    return true
  end

  if method_name =~ /(.+)=/
    return @attributes.key? $1.to_sym
  end

  return false
end