Class: Alf::Reader::Rash

Inherits:
Alf::Reader show all
Defined in:
lib/alf/reader/rash.rb

Overview

Specialization of the Reader contract for .rash files.

A .rash file/stream contains one ruby hash literal on each line. This reader simply decodes each of them in turn with Kernel.eval, providing a state-less reader (that is, tuples are not all loaded in memory at once).

Constant Summary

Constants inherited from Alf::Reader

DEFAULT_OPTIONS

Instance Attribute Summary

Attributes inherited from Alf::Reader

#input, #options, #path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Alf::Reader

by_mime_type, coerce, #each, #initialize, reader, register, #to_s

Methods included from Support::Registry

#each, #listen, #listeners, #register, #registered

Constructor Details

This class inherits a constructor from Alf::Reader

Class Method Details

.mime_typeObject


12
13
14
# File 'lib/alf/reader/rash.rb', line 12

def self.mime_type
  nil
end

Instance Method Details

#line2tuple(line) ⇒ Object

Converts a line previously read from the input stream to a tuple.

The line is simply ignored is this method return nil. Errors should be properly handled by raising exceptions. This method MUST be implemented by subclasses unless each is overriden.


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/alf/reader/rash.rb', line 17

def line2tuple(line)
  return nil if line.strip.empty?
  begin
    h = Kernel.eval(line)
    raise "Tuple expected, got `#{h.inspect}`" unless TupleLike===h
  rescue Exception => ex
    $stderr << "Skipping `#{line.strip}`: #{ex.message}\n"
    nil
  else
    return h
  end
end