Class: ActiveSanction::Parsers::DelimitedTable::Row

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

Instance Method Summary collapse

Constructor Details

#initialize(values:, line:) ⇒ void

Parameters:

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


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)

Returns:

  • (Integer)


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.

Returns:

  • (Hash{Symbol => String, 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?

Parameters:

  • other (T.untyped)

Returns:

  • (Boolean)


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.

Parameters:

  • column (Symbol)

Returns:

  • (String, nil)


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>

Returns:

  • (Array<Symbol>)


57
# File 'lib/active_sanction/parsers/delimited_table/row.rb', line 57

def columns = values.keys

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

Parameters:

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

Returns:

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

Returns:

  • (Integer)


71
# File 'lib/active_sanction/parsers/delimited_table/row.rb', line 71

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

#inspect ⇒ String

Returns:

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

Parameters:

  • column (Symbol)

Returns:

  • (Boolean)


54
# File 'lib/active_sanction/parsers/delimited_table/row.rb', line 54

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

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

Returns:

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


60
# File 'lib/active_sanction/parsers/delimited_table/row.rb', line 60

def to_h = values