Class: Addressing::Address

Inherits:
Object
  • Object
show all
Defined in:
lib/addressing/address.rb

Overview

Represents a postal address with attributes for country, administrative areas, postal code, address lines, and recipient information.

Address objects are immutable - use the with_* methods to create modified copies.

Examples:

Creating an address

address = Addressing::Address.new(
  country_code: "US",
  administrative_area: "CA",
  locality: "Mountain View",
  postal_code: "94043",
  address_line1: "1600 Amphitheatre Parkway"
)

Modifying an address

updated = address.with_postal_code("94044")

Constant Summary collapse

FIELDS =
%i[
  country_code administrative_area locality dependent_locality
  postal_code sorting_code address_line1 address_line2 address_line3
  organization given_name additional_name family_name locale
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(country_code: "", administrative_area: "", locality: "", dependent_locality: "", postal_code: "", sorting_code: "", address_line1: "", address_line2: "", address_line3: "", organization: "", given_name: "", additional_name: "", family_name: "", locale: "und") ⇒ Address

Creates a new Address instance.

Parameters:

  • country_code (String) (defaults to: "")

    ISO 3166-1 alpha-2 country code

  • administrative_area (String) (defaults to: "")

    Top-level administrative subdivision (state, province, etc.)

  • locality (String) (defaults to: "")

    City or locality

  • dependent_locality (String) (defaults to: "")

    Dependent locality (neighborhood, suburb, district, etc.)

  • postal_code (String) (defaults to: "")

    Postal code

  • sorting_code (String) (defaults to: "")

    Sorting code (used in some countries)

  • address_line1 (String) (defaults to: "")

    First line of the street address

  • address_line2 (String) (defaults to: "")

    Second line of the street address

  • address_line3 (String) (defaults to: "")

    Third line of the street address

  • organization (String) (defaults to: "")

    Organization name

  • given_name (String) (defaults to: "")

    Given name (first name)

  • additional_name (String) (defaults to: "")

    Additional name (middle name, patronymic)

  • family_name (String) (defaults to: "")

    Family name (last name)

  • locale (String) (defaults to: "und")

    Locale code for the address



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/addressing/address.rb', line 45

def initialize(country_code: "", administrative_area: "", locality: "", dependent_locality: "", postal_code: "", sorting_code: "", address_line1: "", address_line2: "", address_line3: "", organization: "", given_name: "", additional_name: "", family_name: "", locale: "und")
  @country_code = country_code
  @administrative_area = administrative_area
  @locality = locality
  @dependent_locality = dependent_locality
  @postal_code = postal_code
  @sorting_code = sorting_code
  @address_line1 = address_line1
  @address_line2 = address_line2
  @address_line3 = address_line3
  @organization = organization
  @given_name = given_name
  @additional_name = additional_name
  @family_name = family_name
  @locale = locale
end

Instance Method Details

#==(other) ⇒ Boolean

Compares two addresses for equality based on all field values.

Parameters:

  • other (Object)

    The object to compare with

Returns:

  • (Boolean)

    true if all fields are equal



75
76
77
78
79
# File 'lib/addressing/address.rb', line 75

def ==(other)
  return false unless other.is_a?(Address)

  FIELDS.all? { |field| send(field) == other.send(field) }
end

#eql?(other) ⇒ Boolean

Compares two addresses for equality (alias for ==).

Parameters:

  • other (Object)

    The object to compare with

Returns:

  • (Boolean)

    true if all fields are equal



85
86
87
# File 'lib/addressing/address.rb', line 85

def eql?(other)
  self == other
end

#hashInteger

Generates a hash code for the address based on all field values.

Returns:

  • (Integer)

    hash code



92
93
94
# File 'lib/addressing/address.rb', line 92

def hash
  FIELDS.map { |field| send(field) }.hash
end