Class: Midos::Writer

Inherits:
Base
  • Object
show all
Defined in:
lib/midos/writer.rb

Direct Known Subclasses

Thesaurus

Defined Under Namespace

Classes: Thesaurus, ThesaurusX

Constant Summary collapse

DEFAULT_IO =
$stdout

Instance Attribute Summary

Attributes inherited from Base

#auto_id, #fs, #io, #key, #le, #nl, #rs, #vs

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#reset

Constructor Details

#initialize(options = {}) ⇒ Writer

Returns a new instance of Writer.



66
67
68
69
# File 'lib/midos/writer.rb', line 66

def initialize(options = {})
  @sort = options[:sort]
  super
end

Class Method Details

.open(*args, &block) ⇒ Object



47
48
49
# File 'lib/midos/writer.rb', line 47

def open(*args, &block)
  file_method(nil, 'wb', *args, &block)
end

.transform(value, vs, nl) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/midos/writer.rb', line 51

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) ⇒ Object



39
40
41
# File 'lib/midos/writer.rb', line 39

def write(*args, &block)
  new(args.extract_options!, &block).write(*args)
end

.write_file(*args, &block) ⇒ Object



43
44
45
# File 'lib/midos/writer.rb', line 43

def write_file(*args, &block)
  file_method(:write, 'wb', *args, &block)
end

Instance Method Details

#[]=(id, record) ⇒ Object



94
95
96
# File 'lib/midos/writer.rb', line 94

def []=(id, record)
  write_i(id, record)
end

#put(record, *args) ⇒ Object Also known as: <<



84
85
86
87
88
89
90
# File 'lib/midos/writer.rb', line 84

def put(record, *args)
  record.is_a?(Hash) ?
    write_i(nil, record, *args) :
    write_i(*args.unshift(*record))

  self
end

#vs=(vs) ⇒ Object



71
72
73
74
# File 'lib/midos/writer.rb', line 71

def vs=(vs)
  vs.is_a?(String) ? @vs = vs : raise(TypeError,
    "wrong argument type #{vs.class} (expected String)")
end

#write(records, *args) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/midos/writer.rb', line 76

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