Class: ULS::Records::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/uls/records/base.rb

Overview

The base class for all ULS records we’ll be parsing. This handles some of the helper methods for defining attributes and actually performing the parsing after the record is defined.

Direct Known Subclasses

Entity, FormPrimary, FormSecondary

Constant Summary collapse

DEFAULT_ATTRIBUTE_OPTIONS =
{ type: :string }.freeze
FIELD_SEPARATOR =
'|'.freeze
BLANK_RE =

h/t Rails .blank? implementation

/\A[[:space:]]*\z/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line = nil) ⇒ Base

Returns a new instance of Base.



29
30
31
32
33
# File 'lib/uls/records/base.rb', line 29

def initialize(line = nil)
  return if line.nil?

  from_row(line)
end

Class Method Details

.fieldsObject



24
25
26
# File 'lib/uls/records/base.rb', line 24

def fields
  @fields ||= []
end

.uls_field_accessor(symbol, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/uls/records/base.rb', line 14

def uls_field_accessor(symbol, options = {})
  field = { attribute: symbol }
  field.merge!(options)
  field = DEFAULT_ATTRIBUTE_OPTIONS.merge(field)

  fields << field

  attr_accessor symbol
end

Instance Method Details

#from_row(line) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/uls/records/base.rb', line 35

def from_row(line)
  values = line.split(FIELD_SEPARATOR)
  values.shift


  values.each_with_index do |value, index|
    break if index >= self.class.fields.size

    field = self.class.fields[index]
    setter = "#{field[:attribute]}="
    value = convert(value, field[:type])

    send(setter, value)
  end
end