Class: FlatKit::Xsv::Record
Overview
Internal: Class that exposes the data from an XSV format record to the flatkit API
Instance Attribute Summary collapse
-
#ordered_fields ⇒ Object
readonly
Returns the value of attribute ordered_fields.
Attributes inherited from Record
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #complete_structured_data ⇒ Object (also: #to_hash)
-
#initialize(data:, compare_fields: :none, ordered_fields: :auto, complete_structured_data: nil) ⇒ Record
constructor
A new instance of Record.
- #to_a ⇒ Object
-
#to_s ⇒ Object
convert to a csv line,.
Methods inherited from Record
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_fields ⇒ Object (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_name ⇒ Object
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_data ⇒ Object 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_a ⇒ Object
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_s ⇒ Object
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 |