Class: Stockboy::Reader Abstract

Inherits:
Object
  • Object
show all
Extended by:
DSL
Defined in:
lib/stockboy/reader.rb

Overview

This class is abstract.

Abstract class for defining data readers

Interface

A reader must implement a parse method for extracting an array of records from raw data. At this stage no data transformation is performed, only extracting field tokens for each record, based on the specific data serialization.

String keys should be preferred, since these may be specified by the user; external inputs should not be symbolized (because symbols are never GC’d). Frozen strings for keys are a good idea, of course.

Examples:

reader.parse("name,email\nArthur Dent,[email protected]")
# => [{"name" => "Arthur Dent", "email" => "[email protected]"}]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Reader

Initialize a new reader

Parameters:

  • opts (Hash) (defaults to: {})


33
34
35
# File 'lib/stockboy/reader.rb', line 33

def initialize(opts={})
  @encoding = opts.delete(:encoding)
end

Instance Attribute Details

#encodingObject (readonly)

Returns the value of attribute encoding.



27
28
29
# File 'lib/stockboy/reader.rb', line 27

def encoding
  @encoding
end

Instance Method Details

#parse(data) ⇒ Array<Hash>

Take raw input (String) and extract an array of records

Returns:

  • (Array<Hash>)

Raises:

  • (NoMethodError)


41
42
43
# File 'lib/stockboy/reader.rb', line 41

def parse(data)
  raise NoMethodError, "#{self.class}#parse needs implementation"
end