Class: RightmoveBLM::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/rightmove_blm/row.rb

Overview

A row in a BLM document.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index:, data:, separator:, definition:, attributes: nil) ⇒ Row

Returns a new instance of Row.



12
13
14
15
16
17
18
19
# File 'lib/rightmove_blm/row.rb', line 12

def initialize(index:, data:, separator:, definition:, attributes: nil)
  @index = index
  @data = data
  @separator = separator
  @definition = definition
  @attributes = attributes
  @fields = attributes.keys unless attributes.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *_arguments) ⇒ Object (private)



63
64
65
66
67
68
# File 'lib/rightmove_blm/row.rb', line 63

def method_missing(method, *_arguments)
  return super if attributes.nil?
  return attributes[method] unless attributes[method].nil?

  super
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



6
7
8
# File 'lib/rightmove_blm/row.rb', line 6

def index
  @index
end

Class Method Details

.from_attributes(attributes, index:) ⇒ Object



8
9
10
# File 'lib/rightmove_blm/row.rb', line 8

def self.from_attributes(attributes, index:)
  new(index: index, data: nil, separator: nil, definition: attributes.keys, attributes: attributes)
end

Instance Method Details

#attributesObject



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

def attributes
  return nil unless valid?

  @attributes ||= fields.each_with_index.to_h do |field, field_index|
    [definition[field_index].to_sym, field.strip]
  end
end

#errorsObject



31
32
33
34
35
36
37
# File 'lib/rightmove_blm/row.rb', line 31

def errors
  return [] if valid?

  errors = []
  errors << field_size_mismatch_message unless field_size_valid?
  errors
end

#to_hObject



21
22
23
# File 'lib/rightmove_blm/row.rb', line 21

def to_h
  attributes
end

#valid?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
# File 'lib/rightmove_blm/row.rb', line 25

def valid?
  return false unless field_size_valid?

  true
end