Class: ActiveSanction::Parsers::Spreadsheet::Row
- Inherits:
-
Object
- Object
- ActiveSanction::Parsers::Spreadsheet::Row
- Extended by:
- T::Sig
- Defined in:
- lib/active_sanction/parsers/spreadsheet/row.rb
Overview
One row of a sheet: column name to value, with every cell already resolved to the string it displays as -- a shared string looked up, a date serial rendered, a blank cell nil.
row[:name_of_individual_or_entity] # => "MOHAMMAD HASSAN AKHUND"
row[:date_of_birth] # => "1963-01-30"
row[:alias_strength] # => nil, the cell being empty
row.number # => 2, the sheet's own row number
Deliberately not a Hash, for the reason DelimitedTable::Row is not one:
a Hash would answer row[:date_of_brith] with nil and let a typo in an
adapter look like an empty column all the way into a snapshot. #[]
raises on a name the sheet never had, and #fetch is there for the
genuinely optional case.
It is a separate class from the delimited table's row rather than a shared one because the two disagree about what locates a record. A CSV row is at a line of a file; a sheet row is at a row number the spreadsheet itself assigns, which is what is showing in the corner of the window when somebody opens the published file to check a record -- and which survives the workbook being re-saved with different XML.
Instance Attribute Summary collapse
-
#number ⇒ Integer
readonly
The sheet's own row number, 1-based and counting the header row -- the number a reader of the published file will see beside the record.
- #values ⇒ Hash{Symbol => String, nil} readonly
Instance Method Summary collapse
- #==(other) ⇒ Boolean (also: #eql?)
-
#[](column) ⇒ String?
Raises on a column the sheet never had, which is almost always a typo in an adapter rather than a question about the data.
- #columns ⇒ Array<Symbol>
- #fetch(column, default = nil) ⇒ T.untyped
- #hash ⇒ Integer
- #initialize(values:, number:) ⇒ void constructor
- #inspect ⇒ String
- #null?(column) ⇒ Boolean
- #to_h ⇒ Hash{Symbol => String, nil}
Constructor Details
#initialize(values:, number:) ⇒ void
42 43 44 45 46 |
# File 'lib/active_sanction/parsers/spreadsheet/row.rb', line 42 def initialize(values:, number:) @values = T.let(values.freeze, T::Hash[Symbol, T.nilable(String)]) @number = T.let(number, Integer) freeze end |
Instance Attribute Details
#number ⇒ Integer (readonly)
The sheet's own row number, 1-based and counting the header row -- the number a reader of the published file will see beside the record.
39 40 41 |
# File 'lib/active_sanction/parsers/spreadsheet/row.rb', line 39 def number @number end |
#values ⇒ Hash{Symbol => String, nil} (readonly)
34 35 36 |
# File 'lib/active_sanction/parsers/spreadsheet/row.rb', line 34 def values @values end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
70 71 72 73 74 |
# File 'lib/active_sanction/parsers/spreadsheet/row.rb', line 70 def ==(other) return false unless other.instance_of?(self.class) values == other.values && number == other.number end |
#[](column) ⇒ String?
Raises on a column the sheet never had, which is almost always a typo in an adapter rather than a question about the data.
51 52 53 54 55 |
# File 'lib/active_sanction/parsers/spreadsheet/row.rb', line 51 def [](column) values.fetch(column) do raise MissingKey, "no column #{column.inspect} in this sheet. Read: #{columns.join(", ")}" end end |
#columns ⇒ Array<Symbol>
64 |
# File 'lib/active_sanction/parsers/spreadsheet/row.rb', line 64 def columns = values.keys |
#fetch(column, default = nil) ⇒ T.untyped
58 |
# File 'lib/active_sanction/parsers/spreadsheet/row.rb', line 58 def fetch(column, default = nil) = values.fetch(column, default) |
#hash ⇒ Integer
78 |
# File 'lib/active_sanction/parsers/spreadsheet/row.rb', line 78 def hash = [self.class, values, number].hash |
#inspect ⇒ String
81 82 83 84 |
# File 'lib/active_sanction/parsers/spreadsheet/row.rb', line 81 def inspect filled = values.compact "#<#{self.class} row=#{number} #{filled.map { |name, value| "#{name}=#{value.inspect}" }.join(" ")}>" end |
#null?(column) ⇒ Boolean
61 |
# File 'lib/active_sanction/parsers/spreadsheet/row.rb', line 61 def null?(column) = self[column].nil? |
#to_h ⇒ Hash{Symbol => String, nil}
67 |
# File 'lib/active_sanction/parsers/spreadsheet/row.rb', line 67 def to_h = values |