Class: Address::LineAbbreviator
- Inherits:
-
Object
- Object
- Address::LineAbbreviator
- Defined in:
- app/services/address/line_abbreviator.rb
Overview
Abbreviates a single street address line that exceeds the carrier character limit
by applying postal abbreviations for the given locale, then smart-truncating
at a word boundary if still too long.
Supports: :en (US/CA), :fr (CA-QC / France), :es, :pt, :it, :nl, :de
When no locale is given, all abbreviation rules are applied.
Defined Under Namespace
Classes: Result
Constant Summary collapse
- MAX_LENGTH =
35
Address::MAX_STREET_FIELD_LENGTH
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(value, locale: nil) ⇒ LineAbbreviator
constructor
A new instance of LineAbbreviator.
Constructor Details
#initialize(value, locale: nil) ⇒ LineAbbreviator
Returns a new instance of LineAbbreviator.
31 32 33 34 |
# File 'app/services/address/line_abbreviator.rb', line 31 def initialize(value, locale: nil) @value = value @locale = locale end |
Instance Method Details
#call ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'app/services/address/line_abbreviator.rb', line 36 def call return Result.new(original: nil, abbreviated: nil, changed: false) if @value.blank? original = @value.to_s.squish return Result.new(original: original, abbreviated: original, changed: false) if original.length <= MAX_LENGTH abbreviated = apply_abbreviations(original) Result.new(original: original, abbreviated: abbreviated, changed: abbreviated != original) end |