Class: ActiveSanction::Parsers::DelimitedTable::Row
- Inherits:
-
Object
- Object
- ActiveSanction::Parsers::DelimitedTable::Row
- Extended by:
- T::Sig
- Defined in:
- lib/active_sanction/parsers/delimited_table/row.rb
Overview
One parsed record: column name to value, with the publisher's null sentinel already resolved to nil.
row[:sdn_name] # => "AEROCARIBBEAN AIRLINES"
row[:title] # => nil, because OFAC wrote "-0- " there
row.line # => 2
Deliberately not a Hash. A Hash would answer row[:sdn_nme] with nil
and let a typo in an adapter look like an empty column all the way into
a snapshot; #[] here raises on a name the table never declared, and
#fetch is available for the genuinely optional case.
Instance Attribute Summary collapse
- #line ⇒ Integer readonly
-
#values ⇒ Hash{Symbol => String, nil}
readonly
Column name to value, with the publisher's null sentinel already resolved to nil.
Instance Method Summary collapse
- #==(other) ⇒ Boolean (also: #eql?)
-
#[](column) ⇒ String?
Raises on an undeclared column, 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:, line:) ⇒ void constructor
- #inspect ⇒ String
-
#null?(column) ⇒ Boolean
True when the publisher left the column blank or wrote its null sentinel there.
- #to_h ⇒ Hash{Symbol => String, nil}
Constructor Details
#initialize(values:, line:) ⇒ void
32 33 34 35 36 |
# File 'lib/active_sanction/parsers/delimited_table/row.rb', line 32 def initialize(values:, line:) @values = T.let(values.freeze, T::Hash[Symbol, T.nilable(String)]) @line = T.let(line, Integer) freeze end |
Instance Attribute Details
#line ⇒ Integer (readonly)
29 30 31 |
# File 'lib/active_sanction/parsers/delimited_table/row.rb', line 29 def line @line end |
#values ⇒ Hash{Symbol => String, nil} (readonly)
Column name to value, with the publisher's null sentinel already resolved to nil.
26 27 28 |
# File 'lib/active_sanction/parsers/delimited_table/row.rb', line 26 def values @values end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
63 64 65 66 67 |
# File 'lib/active_sanction/parsers/delimited_table/row.rb', line 63 def ==(other) return false unless other.instance_of?(self.class) values == other.values && line == other.line end |
#[](column) ⇒ String?
Raises on an undeclared column, which is almost always a typo in an adapter rather than a question about the data.
41 42 43 44 45 |
# File 'lib/active_sanction/parsers/delimited_table/row.rb', line 41 def [](column) values.fetch(column) do raise MissingKey, "no column #{column.inspect} in this table. Declared: #{columns.join(", ")}" end end |
#columns ⇒ Array<Symbol>
57 |
# File 'lib/active_sanction/parsers/delimited_table/row.rb', line 57 def columns = values.keys |
#fetch(column, default = nil) ⇒ T.untyped
48 |
# File 'lib/active_sanction/parsers/delimited_table/row.rb', line 48 def fetch(column, default = nil) = values.fetch(column, default) |
#hash ⇒ Integer
71 |
# File 'lib/active_sanction/parsers/delimited_table/row.rb', line 71 def hash = [self.class, values, line].hash |
#inspect ⇒ String
74 75 76 77 |
# File 'lib/active_sanction/parsers/delimited_table/row.rb', line 74 def inspect filled = values.compact "#<#{self.class} line=#{line} #{filled.map { |k, v| "#{k}=#{v.inspect}" }.join(" ")}>" end |
#null?(column) ⇒ Boolean
True when the publisher left the column blank or wrote its null sentinel there. Both arrive as nil, because "-0- " and "" mean the same thing in a file that uses both.
54 |
# File 'lib/active_sanction/parsers/delimited_table/row.rb', line 54 def null?(column) = self[column].nil? |
#to_h ⇒ Hash{Symbol => String, nil}
60 |
# File 'lib/active_sanction/parsers/delimited_table/row.rb', line 60 def to_h = values |