Class: FlatKit::Xsv::Record

Inherits:
Record
  • Object
show all
Defined in:
lib/flat_kit/xsv/record.rb

Overview

Internal: Class that exposes the data from an XSV format record to the flatkit API

Instance Attribute Summary collapse

Attributes inherited from Record

#compare_fields, #data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Record

#<=>, #format_name

Constructor Details

#initialize(data:, compare_fields: :none, ordered_fields: :auto, complete_structured_data: nil) ⇒ Record

Returns a new instance of Record.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/flat_kit/xsv/record.rb', line 27

def initialize(data:, compare_fields: :none,
               ordered_fields: :auto,
               complete_structured_data: nil)
  super(data: data, compare_fields: compare_fields)

  @complete_structured_data = complete_structured_data
  @ordered_fields = ordered_fields

  if data.nil? && (complete_structured_data.nil? || complete_structured_data.empty?)
    raise FlatKit::Error,
          "#{self.class} requires initialization from data: or complete_structured_data:"
  end

  resolve_ordered_fields
end

Instance Attribute Details

#ordered_fieldsObject (readonly)

Returns the value of attribute ordered_fields.



11
12
13
# File 'lib/flat_kit/xsv/record.rb', line 11

def ordered_fields
  @ordered_fields
end

Class Method Details

.format_nameObject



13
14
15
# File 'lib/flat_kit/xsv/record.rb', line 13

def self.format_name
  ::FlatKit::Xsv::Format.format_name
end

.from_record(record) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/flat_kit/xsv/record.rb', line 17

def self.from_record(record)
  if record.instance_of?(FlatKit::Xsv::Record)
    new(data: record.data, compare_fields: record.compare_fields)
  else
    new(data: nil, compare_fields: record.compare_fields,
        ordered_fields: nil,
        complete_structured_data: record.to_hash)
  end
end

Instance Method Details

#[](key) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/flat_kit/xsv/record.rb', line 43

def [](key)
  return nil unless @compare_fields.include?(key)

  if data.nil? && !@complete_structured_data.nil?
    @complete_structured_data[key]
  else
    data[key]
  end
end

#complete_structured_dataObject Also known as: to_hash



53
54
55
# File 'lib/flat_kit/xsv/record.rb', line 53

def complete_structured_data
  @complete_structured_data ||= data.to_hash
end

#to_aObject



58
59
60
61
62
63
64
65
66
# File 'lib/flat_kit/xsv/record.rb', line 58

def to_a
  return data.fields unless data.nil?

  [].tap do |a|
    @ordered_fields.each do |field|
      a << @complete_structured_data[field]
    end
  end
end

#to_sObject

convert to a csv line,

First we use data if it is there since that should be a CSV::Row

Next, if that doesn’t work - iterate over the ordered fields and use the yield the values from complete_structured_data in that order

And finally, if that doesn’twork, then just use complete structured data values in that order.



77
78
79
80
81
# File 'lib/flat_kit/xsv/record.rb', line 77

def to_s
  return data.to_csv unless data.nil?

  CSV.generate_line(to_a)
end