class FlatKit::Xsv::Record

Attributes

ordered_fields[R]

Public Class Methods

format_name() click to toggle source
# File lib/flat_kit/xsv/record.rb, line 9
def self.format_name
  ::FlatKit::Xsv::Format.format_name
end
from_record(record, ordered_fields: nil) click to toggle source
# File lib/flat_kit/xsv/record.rb, line 13
def self.from_record(record, ordered_fields: nil)
  if record.instance_of?(FlatKit::Xsv::Record) then
    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
new(data:, compare_fields: :none, ordered_fields: :auto, complete_structured_data: nil) click to toggle source
Calls superclass method FlatKit::Record::new
# File lib/flat_kit/xsv/record.rb, line 23
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?) then
    raise FlatKit::Error,
      "#{self.class} requires initialization from data: or complete_structured_data:"
  end

  resolve_ordered_fields
end

Public Instance Methods

[](key) click to toggle source
# File lib/flat_kit/xsv/record.rb, line 39
def [](key)
  return nil unless @compare_fields.include?(key)
  if data.nil? && !@complete_structured_data.nil? then
    @complete_structured_data[key]
  else
    data[key]
  end
end
complete_structured_data() click to toggle source
# File lib/flat_kit/xsv/record.rb, line 48
def complete_structured_data
  @complete_structured_data ||= data.to_hash
end
Also aliased as: to_hash
to_a() click to toggle source
# File lib/flat_kit/xsv/record.rb, line 53
def to_a
  return data.fields unless data.nil?

  Array.new.tap do |a|
    @ordered_fields.each do |field|
      a << @complete_structured_data[field]
    end
  end
end
to_hash()
to_s() click to toggle source

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.

# File lib/flat_kit/xsv/record.rb, line 72
def to_s
  return data.to_csv unless data.nil?
  CSV.generate_line(to_a)
end

Private Instance Methods

resolve_ordered_fields() click to toggle source
# File lib/flat_kit/xsv/record.rb, line 79
def resolve_ordered_fields
  if (@ordered_fields == :auto) || (@ordered_fields.nil? || @ordered_fields.empty?) then
    if @data.nil? || @data.empty? then
      @ordered_fields = complete_structured_data.keys
    else
      @ordered_fields = @data.headers
    end
  end
end