class Midos::Reader

Constants

DEFAULT_IO

Attributes

records[R]

Public Class Methods

parse(*args, &block) click to toggle source
# File lib/midos/reader.rb, line 36
def parse(*args, &block)
  reader = new(args.extract_options!).parse(*args, &block)
  block ? reader : reader.records
end
parse_file(*args, &block) click to toggle source
# File lib/midos/reader.rb, line 41
def parse_file(*args, &block)
  file_method(:parse, 'r', *args, &block)
end
transform(value, nl, vs, ovs = nil) click to toggle source
# File lib/midos/reader.rb, line 45
def transform(value, nl, vs, ovs = nil)
  rnl, rvs = replacements_for(*[nl, ovs].compact)

  value.gsub!(nl, "\n")
  value.gsub!(rnl, nl)

  !value.index(vs) ?
    replace!(value, rvs, ovs) || value :
    value.split(vs).each { |v| replace!(v, rvs, ovs) }
end

Private Class Methods

replace!(value, rvs, ovs) click to toggle source
# File lib/midos/reader.rb, line 58
def replace!(value, rvs, ovs)
  value.gsub!(rvs, ovs) if rvs
end

Public Instance Methods

parse(io = io(), &block) click to toggle source
# File lib/midos/reader.rb, line 76
def parse(io = io(), &block)
  unless block
    records, block = @records, amend_block { |id, record|
      records[id] = record
    }
  end

  id, record = nil, {}

  io.each { |line|
    line = line.chomp(le)

    if line == rs
      block[key ? id : auto_id.call, record]
      id, record = nil, {}
    else
      k, v = line.split(fs, 2)
      record[k] = k == key ? id = v : transform(v) if k && v
    end
  }

  self
end
reset() click to toggle source
Calls superclass method Midos::Base#reset
# File lib/midos/reader.rb, line 66
def reset
  super
  @records = {}
end
vs=(vs) click to toggle source
# File lib/midos/reader.rb, line 71
def vs=(vs)
  @vs = vs.is_a?(Regexp) ? (@ovs = nil; vs) :
    %r{\s*#{Regexp.escape(@ovs = vs)}\s*}
end

Private Instance Methods

amend_block(&block) click to toggle source
# File lib/midos/reader.rb, line 106
def amend_block(&block)
  return block unless $VERBOSE && k = @key

  r, i = block.binding.eval('_ = records, io')

  l = i.respond_to?(:lineno)
  s = i.respond_to?(:path) ? i.path :
    Object.instance_method(:inspect).bind(i).call

  lambda { |id, *args|
    if (r ||= block.binding.eval('records')).key?(id)
      warn "Duplicate record in #{s}#{":#{i.lineno}" if l}: »#{k}:#{id}«"
    end

    block[id, *args]
  }
end
transform(value) click to toggle source
# File lib/midos/reader.rb, line 102
def transform(value)
  self.class.transform(value, nl, vs, @ovs)
end