Class: AtlasEngine::AddressValidation::Token::Synonyms
- Inherits:
-
Object
- Object
- AtlasEngine::AddressValidation::Token::Synonyms
- 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
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Instance Method Summary collapse
-
#initialize(tokens: []) ⇒ Synonyms
constructor
A new instance of Synonyms.
- #inspect ⇒ Object
- #multi_token? ⇒ Boolean
- #offset_range ⇒ Object
- #position ⇒ Object
- #type ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(tokens: []) ⇒ Synonyms
Returns a new instance of Synonyms.
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
#tokens ⇒ Object (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
#inspect ⇒ Object
48 49 50 |
# File 'app/models/atlas_engine/address_validation/token/synonyms.rb', line 48 def inspect "<syn #{tokens.inspect}/>" end |
#multi_token? ⇒ Boolean
63 64 65 |
# File 'app/models/atlas_engine/address_validation/token/synonyms.rb', line 63 def multi_token? tokens.any?(Array) end |
#offset_range ⇒ Object
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 |
#position ⇒ Object
19 |
# File 'app/models/atlas_engine/address_validation/token/synonyms.rb', line 19 def position = first_token.position |
#type ⇒ Object
58 59 60 |
# File 'app/models/atlas_engine/address_validation/token/synonyms.rb', line 58 def type "<SYNONYMS>" end |
#value ⇒ Object
53 54 55 |
# File 'app/models/atlas_engine/address_validation/token/synonyms.rb', line 53 def value nil end |