In Files

Class/Module Index [+]

Quicksearch

LTSV

ltsv - A parser / dumper for Labelled Tab-Separated Values(LTSV)

Copyright (C) 2013 TOYODA Naoto.

Constants

VERSION

Public Class Methods

dump(value) click to toggle source

Dumping the value given into a new String. Each special character will be escaped with backslash(‘'), and the expression should be contained in a single line.

Arguments:

  • value: a target to dump. It should respond to :to_hash.

Returns:

# File lib/ltsv.rb, line 71
def dump(value)
  raise ArgumentError, "dump should take an argument of hash" unless
    value.respond_to? :to_hash

  hash = value.to_hash

  hash.inject('') do |s, kv|
    s << "\t" if s.bytesize > 0

    (k, v) = kv
    value = escape v
    s << k.to_s << ':' << value
  end
end
load(io_or_string, options = {}) click to toggle source

Parsing the content of the given stream or path. If you specified a stream as the first argument, this method behaves as same as load.

Arguments:

  • io_or_string: a target to parse. Possible values are: String, IO. If you give the string value, it stands the path of the file to parse. If you give the IO value, it stands the stream which provides the contents to parse. Note : If you give the IO value, this method behaves like parse.

Options:

  • symbolize_keys : Whether the label will be available as symbol or not. Default value is true.

  • encoding : The encoding of the stream given as the first argument. Default value: Encoding.default_external

Returns:

  • An Array of Hash : Each hash stands for a line of the io or the file.

# File lib/ltsv.rb, line 51
def load(io_or_string, options = {})
  encoding_opt = options.delete :encoding
  encoding =
    encoding_opt ? Encoding.find(encoding_opt) : Encoding.default_external

  case io_or_string
  when String
    File.open(io_or_string, "r:#{encoding}"){|f|parse_io(f, options)}
  when IO
    parse_io(io_or_string, options)
  end
end
parse(io_or_string, options = {}) click to toggle source

Parsing given stream or string. If you specified a stream as the first argument, this method behaves as same as load.

Arguments:

  • io_or_string: a target to parse. Possible values are: String, IO. If you give the string value, it stands the content to parse. If you give the IO value, it stands the stream which provides the contents to parse.

Options:

  • symbolize_keys : Whether the label will be available as symbol or not. Default value is true.

  • encoding : The encoding of the stream given as the first argument. It is effective only when the first argument is an instance of IO. Default value: Encoding.default_external

Returns:

  • An instance of Hash : When you give the string as the first argument.

  • An Array of Hash : When you give the IO as the first argument.

# File lib/ltsv.rb, line 26
def parse(io_or_string, options = {})
  case io_or_string
  when String
    parse_string(io_or_string, options)
  when IO
    parse_io(io_or_string, options)
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.