Class: FlatKit::Record
- Inherits:
-
Object
- Object
- FlatKit::Record
- Includes:
- Comparable
- Defined in:
- lib/flat_kit/record.rb
Overview
Public: The base class that all record classes should inherit from.
Its goal is to be an efficient comparator of data that can be inflated from a source structure to a fully realized hash.
All records need to be able to be initialized from a data structure that it is handed to it by the Reader intance within the same Format.
Records are generally not going to be created outside of this library, they are tied to a specific format and provide a common interface that can be used for:
* comparison between records from different source / destinations formats
* conversion to a different format
Given that - the way to create a record is either from another Record instance:
Record.from_record(other) # create a record from another record
or the way a Reader will do it
Record.new(...) # generally only used by a Reader instance to
# yield new reocrds
When Implementing a new Format, the corresponding Record class for that Format must:
* implement `#[](key)` which will be used to lookup the values of the
comparable fields.
* implement `#to_hash` which is used when conversions
* implement `.from_record` which is used in conversion
# the initialize method must call super(data:, compare_fields:) to
initializa the root data structures
Direct Known Subclasses
Instance Attribute Summary collapse
-
#compare_fields ⇒ Object
readonly
Returns the value of attribute compare_fields.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #[](key) ⇒ Object
- #format_name ⇒ Object
-
#initialize(data:, compare_fields:) ⇒ Record
constructor
A new instance of Record.
- #to_hash ⇒ Object
Constructor Details
#initialize(data:, compare_fields:) ⇒ Record
Returns a new instance of Record.
44 45 46 47 |
# File 'lib/flat_kit/record.rb', line 44 def initialize(data:, compare_fields:) @data = data @compare_fields = compare_fields end |
Instance Attribute Details
#compare_fields ⇒ Object (readonly)
Returns the value of attribute compare_fields.
42 43 44 |
# File 'lib/flat_kit/record.rb', line 42 def compare_fields @compare_fields end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
42 43 44 |
# File 'lib/flat_kit/record.rb', line 42 def data @data end |
Class Method Details
.from_record ⇒ Object
83 84 85 |
# File 'lib/flat_kit/record.rb', line 83 def self.from_record raise NotImplementedError, "#{self.class} must implement #{self.class}.from_record" end |
Instance Method Details
#<=>(other) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/flat_kit/record.rb', line 53 def <=>(other) compare_result = nil compare_fields.each do |field| my_val = self[field] other_val = other[field] compare_result = if my_val.nil? && other_val.nil? 0 elsif my_val.nil? -1 elsif other_val.nil? 1 else my_val <=> (other_val) end return compare_result unless compare_result.zero? end compare_result end |
#[](key) ⇒ Object
75 76 77 |
# File 'lib/flat_kit/record.rb', line 75 def [](key) raise NotImplementedError, "#{self.class} must implement #[](key)" end |
#format_name ⇒ Object
49 50 51 |
# File 'lib/flat_kit/record.rb', line 49 def format_name self.class.format_name end |
#to_hash ⇒ Object
79 80 81 |
# File 'lib/flat_kit/record.rb', line 79 def to_hash raise NotImplementedError, "#{self.class} must implement #to_hash" end |