class Bismas::Writer

Constants

DEFAULT_IO

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method Bismas::Base.new
# File lib/bismas/writer.rb, line 51
def initialize(options = {})
  @category_length = options[:category_length] || DEFAULT_CATEGORY_LENGTH
  @padding_length  = options[:padding_length]  || DEFAULT_PADDING_LENGTH
  @sort            = options[:sort]

  @chars = Bismas.chars(options)

  super
end
open(*args, &block) click to toggle source
# File lib/bismas/writer.rb, line 45
def open(*args, &block)
  file_method(nil, 'wb', *args, &block)
end
write(*args, &block) click to toggle source
# File lib/bismas/writer.rb, line 37
def write(*args, &block)
  new(args.extract_options!, &block).write(*args)
end
write_file(*args, &block) click to toggle source
# File lib/bismas/writer.rb, line 41
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/bismas/writer.rb, line 79
def []=(id, record)
  write_i(id, record)
end
put(record, *args) click to toggle source
# File lib/bismas/writer.rb, line 69
def put(record, *args)
  record.is_a?(Hash) ?
    write_i(nil, record, *args) :
    write_i(*args.unshift(*record))

  self
end
Also aliased as: <<
write(records, *args) click to toggle source
# File lib/bismas/writer.rb, line 61
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

write_i(id, record, io = io()) click to toggle source
# File lib/bismas/writer.rb, line 85
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

  category_format, fs = "%-#{@category_length}s", @chars[:fs]

  io << @chars[:rs]

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

  io << @chars[:padding] * @padding_length << fs if @padding_length > 0
  io << @chars[:newline]
end