Class: Differential::Parser::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/differential/parser/reader.rb

Overview

This class is used to parse incoming datasets. Usage: Instantiate new object with configuration options. Call read to parse individual hash objects into Record objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record_id_key:, value_key:, group_id_key: nil) ⇒ Reader

Params:

record_id_key

The hash key(s) to use to uniquely identify a record (required)

value_key

The hash key used to extract the value of the record.

group_id_key

The hash key(s) to use to identify which group the record belongs to.

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
# File 'lib/differential/parser/reader.rb', line 25

def initialize(record_id_key:, value_key:, group_id_key: nil)
  raise ArgumentError, 'record_id_key is required'  unless record_id_key
  raise ArgumentError, 'value_key is required'      unless value_key

  @record_id_key  = record_id_key
  @value_key      = value_key
  @group_id_key   = group_id_key
end

Instance Attribute Details

#group_id_keyObject (readonly)

Returns the value of attribute group_id_key.



17
18
19
# File 'lib/differential/parser/reader.rb', line 17

def group_id_key
  @group_id_key
end

#record_id_keyObject (readonly)

Returns the value of attribute record_id_key.



17
18
19
# File 'lib/differential/parser/reader.rb', line 17

def record_id_key
  @record_id_key
end

#value_keyObject (readonly)

Returns the value of attribute value_key.



17
18
19
# File 'lib/differential/parser/reader.rb', line 17

def value_key
  @value_key
end

Instance Method Details

#each(hashes) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/differential/parser/reader.rb', line 34

def each(hashes)
  return enum_for(:each) unless block_given?

  array(hashes).each do |hash|
    next unless hash

    record = read(hash)
    yield record
  end
end