class Athena::Formats::Midos

Constants

FIELD_SEPARATOR
RECORD_SEPARATOR
TO_LATIN1
VALUE_SEPARATOR

Attributes

dbm_parser[R]

Public Instance Methods

convert(record) click to toggle source
# File lib/athena/formats/dbm.rb, line 78
def convert(record)
  rs, fs, vs, crlf_re, iconv =
    RECORD_SEPARATOR, FIELD_SEPARATOR, VALUE_SEPARATOR, CRLF_RE, TO_LATIN1

  dbm = ["ID#{fs}#{record.id}"]

  record.struct.each { |field, struct|
    struct_values = struct[:values]
    struct_values.default = []

    strings = struct[:elements].map { |element|
      values = []

      struct_values[element].each { |value|
        if value
          value = value.strip.gsub(crlf_re, ' ')
          values << value unless value.empty?
        end
      }

      values.empty? ? struct[:empty] : values.join(vs)
    }

    dbm << "#{field.to_s.upcase}#{fs}#{iconv.iconv(struct[:string] % strings)}"
  }

  dbm << rs << CRLF

  dbm.join(CRLF)
end
parse(input, &block) click to toggle source
# File lib/athena/formats/dbm.rb, line 60
def parse(input, &block)
  num = 0

  dbm_parser.parse(input) { |id, doc|
    Athena::Record.new(id, block) { |record|
      config.each { |element, field_config|
        Array(doc[element]).each { |value|
          record.update(element, value, field_config)
        }
      }
    }

    num += 1
  }

  num
end

Private Instance Methods

init_in(*) click to toggle source
Calls superclass method Athena::Formats::Base#init_in
# File lib/athena/formats/dbm.rb, line 111
def init_in(*)
  @__record_element_ok__ = [String, nil]
  super
  @dbm_parser = Nuggets::Midos::Parser.new(:key => record_element)
end