class Midos::Writer

Constants

DEFAULT_IO

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method Midos::Base.new
# File lib/midos/writer.rb, line 65
def initialize(options = {})
  @sort = options[:sort]
  super
end
open(*args, &block) click to toggle source
# File lib/midos/writer.rb, line 46
def open(*args, &block)
  file_method(nil, 'wb', *args, &block)
end
transform(value, vs, nl) click to toggle source
# File lib/midos/writer.rb, line 50
def transform(value, vs, nl)
  rvs, rnl = replacements_for(vs, nl)

  value = !value.is_a?(Array) ?
    value.to_s.gsub(vs, rvs) :
    value.map { |v| v.gsub(vs, rvs) }.join(vs)

  value.gsub!(nl, rnl)
  value.gsub!(/\r\n?|\n/, nl)

  value
end
write(*args, &block) click to toggle source
# File lib/midos/writer.rb, line 38
def write(*args, &block)
  new(args.extract_options!, &block).write(*args)
end
write_file(*args, &block) click to toggle source
# File lib/midos/writer.rb, line 42
def write_file(*args, &block)
  file_method(:write, 'wb', *args, &block)
end

Public Instance Methods

<<(record, *args)
Alias for: put
[]=(id, record) click to toggle source
# File lib/midos/writer.rb, line 93
def []=(id, record)
  write_i(id, record)
end
put(record, *args) click to toggle source
# File lib/midos/writer.rb, line 83
def put(record, *args)
  record.is_a?(Hash) ?
    write_i(nil, record, *args) :
    write_i(*args.unshift(*record))

  self
end
Also aliased as: <<
vs=(vs) click to toggle source
# File lib/midos/writer.rb, line 70
def vs=(vs)
  vs.is_a?(String) ? @vs = vs : raise(TypeError,
    "wrong argument type #{vs.class} (expected String)")
end
write(records, *args) click to toggle source
# File lib/midos/writer.rb, line 75
def write(records, *args)
  !records.is_a?(Hash) ?
    records.each { |record| write_i(nil, record, *args) } :
    records.each { |id, record| write_i(id, record, *args) }

  self
end

Private Instance Methods

transform(value) click to toggle source
# File lib/midos/writer.rb, line 114
def transform(value)
  self.class.transform(value, vs, nl)
end
write_i(id, record, io = io()) click to toggle source
# File lib/midos/writer.rb, line 99
def write_i(id, record, io = io())
  return if record.empty?

  if key && !record.key?(key)
    record = { key => id || auto_id.call }.update(record)
  end

  (@sort ? Hash[record.sort] : record).each { |k, v|
    k ? io << k << fs << transform(v) << le :
     Array(v).each { |w| io << w.to_s << le } if v
  }

  io << rs << le << le
end