Class: AtlasEngine::AddressValidation::Token::Synonyms

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
app/models/atlas_engine/address_validation/token/synonyms.rb

Constant Summary collapse

TokenList =

Similar to a Token, and has some of the same methods like position, value, type.

T.type_alias { T::Array[Token] }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens: []) ⇒ Synonyms

Returns a new instance of Synonyms.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/atlas_engine/address_validation/token/synonyms.rb', line 26

def initialize(tokens: [])
  raise ArgumentError, "Synonyms cannot be empty" if tokens.empty?

  @tokens = []
  tokens_by_position = tokens.stable_sort_by(&:position).group_by(&:position)

  while tokens_by_position.values.any?(&:present?)
    current_group = []
    starting_position = tokens_by_position.keys.first

    while tokens_by_position.key?(starting_position)
      token = T.must(tokens_by_position[starting_position]).shift
      current_group << token
      starting_position += T.must(token).position_length
    end

    @tokens << (current_group.one? ? current_group.first : current_group)
    tokens_by_position.compact_blank! # remove positions having no tokens
  end
end

Instance Attribute Details

#tokensObject (readonly)

Returns the value of attribute tokens.



14
15
16
# File 'app/models/atlas_engine/address_validation/token/synonyms.rb', line 14

def tokens
  @tokens
end

Instance Method Details

#inspectObject



48
49
50
# File 'app/models/atlas_engine/address_validation/token/synonyms.rb', line 48

def inspect
  "<syn #{tokens.inspect}/>"
end

#multi_token?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'app/models/atlas_engine/address_validation/token/synonyms.rb', line 63

def multi_token?
  tokens.any?(Array)
end

#offset_rangeObject



22
23
# File 'app/models/atlas_engine/address_validation/token/synonyms.rb', line 22

def offset_range = first_token.offset_range
# rubocop:enable Rails/Delegate

#positionObject



19
# File 'app/models/atlas_engine/address_validation/token/synonyms.rb', line 19

def position = first_token.position

#typeObject



58
59
60
# File 'app/models/atlas_engine/address_validation/token/synonyms.rb', line 58

def type
  "<SYNONYMS>"
end

#valueObject



53
54
55
# File 'app/models/atlas_engine/address_validation/token/synonyms.rb', line 53

def value
  nil
end