Class: Pile::Record
Overview
Individual record in list of contributors, as an array of values coupled with its header.
Instance Attribute Summary collapse
-
#csv ⇒ CSV
see
add_record_to_csv. -
#header ⇒ Pile::Header
The header associated with this record.
-
#values ⇒ Array<Object>
below for helper methods that operate on a record’s values.
Class Method Summary collapse
-
.from_csv_row(row, header) ⇒ Object
Construct a ‘Header’ from a CSV-formatted line.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](i) ⇒ Object
Retrieve a value in the record by its position, or by the column name.
-
#[]=(i, v) ⇒ Object
Set a value in the record by its position, or by the column name.
-
#each ⇒ Object
Enumerate the record after converting to an array with
to_a. - #eql?(other) ⇒ Boolean
-
#initialize(header, *values) ⇒ Record
constructor
A new instance of Record.
-
#method_missing(method, *args, &block) ⇒ Object
Send everything that the
headerobject recognized to it. -
#to_a ⇒ Object
Enumerate each value.
-
#write_record(csv = nil) ⇒ Object
Write this record to its CSV object, if present.
Constructor Details
#initialize(header, *values) ⇒ Record
Returns a new instance of Record.
31 32 33 34 |
# File 'lib/pile/record.rb', line 31 def initialize header, *values @header = header @values = values end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Send everything that the header object recognized to it. Can be used for column_index, etc.
38 39 40 |
# File 'lib/pile/record.rb', line 38 def method_missing method, *args, &block header.send method, *args, &block end |
Instance Attribute Details
#csv ⇒ CSV
see add_record_to_csv
25 26 27 |
# File 'lib/pile/record.rb', line 25 def csv @csv end |
#header ⇒ Pile::Header
Returns The header associated with this record.
19 20 21 |
# File 'lib/pile/record.rb', line 19 def header @header end |
#values ⇒ Array<Object>
below for helper methods that operate on a record’s values.
22 23 24 |
# File 'lib/pile/record.rb', line 22 def values @values end |
Class Method Details
.from_csv_row(row, header) ⇒ Object
Construct a ‘Header’ from a CSV-formatted line.
14 15 16 |
# File 'lib/pile/record.rb', line 14 def self.from_csv_row row, header self.new header, *row.parse_csv(converters: [:integer]) end |
Instance Method Details
#==(other) ⇒ Object
65 66 67 |
# File 'lib/pile/record.rb', line 65 def ==(other) self.header == other.header && self.values == other.values && self.csv == other.csv end |
#[](i) ⇒ Object
Retrieve a value in the record by its position, or by the column name. Aliases are recognized.
44 45 46 |
# File 'lib/pile/record.rb', line 44 def [](i) values[column_index i] end |
#[]=(i, v) ⇒ Object
Set a value in the record by its position, or by the column name. Aliases are recognized.
50 51 52 |
# File 'lib/pile/record.rb', line 50 def []=(i, v) values[column_index i] = v end |
#each ⇒ Object
Enumerate the record after converting to an array with to_a.
74 75 76 |
# File 'lib/pile/record.rb', line 74 def each to_a.each end |
#eql?(other) ⇒ Boolean
69 70 71 |
# File 'lib/pile/record.rb', line 69 def eql?(other) self.header.eql?(other.header) && self.values.eql?(other.values) && self.csv.eql?(other.csv) end |
#to_a ⇒ Object
Enumerate each value.
79 80 81 |
# File 'lib/pile/record.rb', line 79 def to_a values end |
#write_record(csv = nil) ⇒ Object
Write this record to its CSV object, if present.
58 59 60 61 62 63 |
# File 'lib/pile/record.rb', line 58 def write_record csv = nil csv ||= self.csv raise 'Record#add_record_to_csv: no associated CSV object.' unless csv csv << values end |