Class: ActiveSanction::Parsers::Spreadsheet::Row

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(values:, number:) ⇒ void

Parameters:

  • values (Hash{Symbol => String, nil})
  • number (Integer)


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.

Returns:

  • (Integer)


39
40
41
# File 'lib/active_sanction/parsers/spreadsheet/row.rb', line 39

def number
  @number
end

#values ⇒ Hash{Symbol => String, nil} (readonly)

Returns:

  • (Hash{Symbol => String, nil})


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?

Parameters:

  • other (T.untyped)

Returns:

  • (Boolean)


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.

Parameters:

  • column (Symbol)

Returns:

  • (String, nil)


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>

Returns:

  • (Array<Symbol>)


64
# File 'lib/active_sanction/parsers/spreadsheet/row.rb', line 64

def columns = values.keys

#fetch(column, default = nil) ⇒ T.untyped

Parameters:

  • column (Symbol)
  • default (T.untyped) (defaults to: nil)

Returns:

  • (T.untyped)


58
# File 'lib/active_sanction/parsers/spreadsheet/row.rb', line 58

def fetch(column, default = nil) = values.fetch(column, default)

#hash ⇒ Integer

Returns:

  • (Integer)


78
# File 'lib/active_sanction/parsers/spreadsheet/row.rb', line 78

def hash = [self.class, values, number].hash

#inspect ⇒ String

Returns:

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

Parameters:

  • column (Symbol)

Returns:

  • (Boolean)


61
# File 'lib/active_sanction/parsers/spreadsheet/row.rb', line 61

def null?(column) = self[column].nil?

#to_h ⇒ Hash{Symbol => String, nil}

Returns:

  • (Hash{Symbol => String, nil})


67
# File 'lib/active_sanction/parsers/spreadsheet/row.rb', line 67

def to_h = values