Module: LTSV

Defined in:
lib/ltsv.rb,
lib/ltsv/version.rb

Defined Under Namespace

Classes: Error, MalformedDataError

Constant Summary collapse

ROW_SEPARATOR =
"\n".freeze
COLUMN_DELIMITER =
"\t".freeze
LABEL_END =
':'.freeze
VERSION =
'0.0.1.2'

Class Method Summary collapse

Class Method Details

.for_stringable(stringable) ⇒ void Also known as: for_io

This method returns an undefined value.

Parameters:

  • stringable (String, IO, StringIO, #each_line)

Yield Returns:

  • (Hash)

    row - label<Symbol> => value<String>



39
40
41
42
43
44
45
46
# File 'lib/ltsv.rb', line 39

def for_stringable(stringable)
  return to_enum(__callee__, stringable) unless block_given?

  stringable.each_line ROW_SEPARATOR do |line|
    yield parse_line(line)
  end
  nil
end

.foreach(path, &block) ⇒ void

This method returns an undefined value.

Parameters:

  • path (String, Pathname)

Yield Returns:

  • (Hash)

    row - label<Symbol> => value<String>



53
54
55
56
57
58
59
60
# File 'lib/ltsv.rb', line 53

def foreach(path, &block)
  return to_enum(__callee__, path) unless block_given?

  open path do |f|
    for_io f, &block
  end
  nil
end

.line_from_hash(hash) ⇒ String

Parameters:

  • hash (Hash)

Returns:

  • (String)


64
65
66
67
68
69
70
71
72
# File 'lib/ltsv.rb', line 64

def line_from_hash(hash)
  hash.each_pair.map do |label, value|
    label, value = label.to_s, value.to_s
    raise MalformedDataError, "`#{label}' is an invalid label"  unless valid_label?(label)
    raise MalformedDataError, "`#{value}' is an invalid value" unless valid_value?(value)
    
    "#{label}#{LABEL_END}#{value}"
  end.join COLUMN_DELIMITER
end

.parse(str) ⇒ Array<Hash>

Returns row, row, row … row

Parameters:

  • str (String)

Returns:

  • (Array<Hash>)

    row, row, row ... row



32
33
34
# File 'lib/ltsv.rb', line 32

def parse(str)
  for_stringable(str).to_a
end

.parse_line(line) ⇒ Hash

Returns row - label<Symbol> => value<String>

Parameters:

  • line (String)

Returns:

  • (Hash)

    row - label<Symbol> => value<String>



20
21
22
23
24
25
26
27
28
# File 'lib/ltsv.rb', line 20

def parse_line(line)
  columns = line.chomp.split COLUMN_DELIMITER
  {}.tap {|hash|
    columns.each do |column|
      label, value = *column.split(LABEL_END, 2)
      hash[label.to_sym] = value
    end
  }
end

.string_from_hashes(hashes) ⇒ String

Parameters:

  • hashes (Array<Hash>)

Returns:

  • (String)


76
77
78
# File 'lib/ltsv.rb', line 76

def string_from_hashes(hashes)
  hashes.map{ |h| line_from_hash h }.join ROW_SEPARATOR
end

.valid_label?(label) ⇒ Boolean

Parameters:

  • label (String, Symbol)

Returns:

  • (Boolean)


81
82
83
# File 'lib/ltsv.rb', line 81

def valid_label?(label)
  [COLUMN_DELIMITER, LABEL_END, ROW_SEPARATOR].none? { |special| label.include? special }
end

.valid_value?(value) ⇒ Boolean

Parameters:

  • value (String)

Returns:

  • (Boolean)


86
87
88
# File 'lib/ltsv.rb', line 86

def valid_value?(value)
  [COLUMN_DELIMITER, ROW_SEPARATOR].none? { |special| value.include? special }
end