Class: Pile::Record

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pile/record.rb

Overview

Individual record in list of contributors, as an array of values coupled with its header.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, *values) ⇒ Record

Returns a new instance of Record.

Parameters:

  • header (Pile::Header)

    The header associated with this record, defining the structure of the record, and by what names (e.g. ‘id’ and ‘name’) values can be indexed.

  • values (Array<Object>)

    The values in the row of the 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

#csvCSV

see add_record_to_csv

Returns:

  • (CSV)

    An optional CSV object, which some helper methods use; e.g.



25
26
27
# File 'lib/pile/record.rb', line 25

def csv
  @csv
end

#headerPile::Header

Returns The header associated with this record.

Returns:

  • (Pile::Header)

    The header associated with this record.



19
20
21
# File 'lib/pile/record.rb', line 19

def header
  @header
end

#valuesArray<Object>

below for helper methods that operate on a record’s values.

Returns:

  • (Array<Object>)

    The values associated with this record. See



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

#eachObject

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

Returns:

  • (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_aObject

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.

Parameters:

  • csv (CSV) (defaults to: nil)

    (nil) If present, the values will be written to the passed CSV object rather than the header’s.



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