Class: FlatKit::Xsv::Record
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.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# 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 |
Instance Attribute Details
#ordered_fields ⇒ Object (readonly)
Returns the value of attribute ordered_fields.
7 8 9 |
# File 'lib/flat_kit/xsv/record.rb', line 7 def ordered_fields @ordered_fields end |
Class Method Details
.format_name ⇒ Object
9 10 11 |
# 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) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# 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 |
Instance Method Details
#[](key) ⇒ Object
39 40 41 42 43 44 45 46 |
# 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 ⇒ Object Also known as: to_hash
48 49 50 |
# File 'lib/flat_kit/xsv/record.rb', line 48 def complete_structured_data @complete_structured_data ||= data.to_hash end |
#to_a ⇒ Object
53 54 55 56 57 58 59 60 61 |
# 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_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.
72 73 74 75 |
# 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 |