Class: Athena::Formats::DBM

Inherits:
Base
  • Object
show all
Defined in:
lib/athena/formats/dbm.rb

Constant Summary collapse

CRLF =
"\015\012"
ICONV_TO_LATIN1 =
Iconv.new('latin1//TRANSLIT//IGNORE', 'utf-8')
VALUE_SEPARATOR =
'|'
RECORD_SEPARATOR =
'&&&'

Instance Method Summary collapse

Methods inherited from Base

#deferred?, formats, #parse, valid_format?, #wrap

Instance Method Details

#convert(record) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/athena/formats/dbm.rb', line 44

def convert(record)
  dbm = ["ID:#{record.id}"]

  record.struct.each { |field, struct|
    strings = struct[:elements].inject([]) { |array, element|
      values = (struct[:values][element] || []).map { |v|
        (v || '').strip.gsub(/(?:\r?\n)+/, ' ')
      }.reject { |v| v.empty? }

      array << (values.empty? ? struct[:empty] : values.join(VALUE_SEPARATOR))
    }

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

  dbm << RECORD_SEPARATOR

  dbm.join(CRLF) << CRLF << CRLF
end