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
51 def initialize(options = {})
52   @category_length = options[:category_length] || DEFAULT_CATEGORY_LENGTH
53   @padding_length  = options[:padding_length]  || DEFAULT_PADDING_LENGTH
54   @sort            = options[:sort]
55 
56   @chars = Bismas.chars(options)
57 
58   super
59 end
open(*args, &block) click to toggle source
   # File lib/bismas/writer.rb
45 def open(*args, &block)
46   file_method(nil, 'wb', *args, &block)
47 end
write(*args, &block) click to toggle source
   # File lib/bismas/writer.rb
37 def write(*args, &block)
38   new(args.extract_options!, &block).write(*args)
39 end
write_file(*args, &block) click to toggle source
   # File lib/bismas/writer.rb
41 def write_file(*args, &block)
42   file_method(:write, 'wb', *args, &block)
43 end

Public Instance Methods

<<(record, *args)
Alias for: put
[]=(id, record) click to toggle source
   # File lib/bismas/writer.rb
79 def []=(id, record)
80   write_i(id, record)
81 end
put(record, *args) click to toggle source
   # File lib/bismas/writer.rb
69 def put(record, *args)
70   record.is_a?(Hash) ?
71     write_i(nil, record, *args) :
72     write_i(*args.unshift(*record))
73 
74   self
75 end
Also aliased as: <<
write(records, *args) click to toggle source
   # File lib/bismas/writer.rb
61 def write(records, *args)
62   !records.is_a?(Hash) ?
63     records.each { |record| write_i(nil, record, *args) } :
64     records.each { |id, record| write_i(id, record, *args) }
65 
66   self
67 end

Private Instance Methods

write_i(id, record, io = io()) click to toggle source
    # File lib/bismas/writer.rb
 85 def write_i(id, record, io = io())
 86   return if record.empty?
 87 
 88   if key && !record.key?(key)
 89     record = { key => id || auto_id.call }.update(record)
 90   end
 91 
 92   category_format, fs = "%-#{@category_length}s", @chars[:fs]
 93 
 94   io << @chars[:rs]
 95 
 96   (@sort ? Hash[record.sort] : record).each { |k, v| Array(v).each { |w|
 97     io << category_format % k if k
 98     io << w.to_s << fs
 99   } if v }
100 
101   io << @chars[:padding] * @padding_length << fs if @padding_length > 0
102   io << @chars[:newline]
103 end