Class: UnitedStates::State::Designation

Inherits:
Object
  • Object
show all
Defined in:
lib/united_states/state/designation.rb

Overview

Represents the various way to designate a state (e.g. name, postal code).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, postal_code:) ⇒ Designation

Returns a new instance of Designation.

Examples:

UnitedStates::State::Designation.new(
  name: 'wyoming', postal_code: 'wy')
# => #<UnitedStates::State::Designation:0x007...@string="wy">>

Raises:



44
45
46
47
# File 'lib/united_states/state/designation.rb', line 44

def initialize(name:, postal_code:)
  @name = UnitedStates::State::Name.new(name)
  @postal_code = UnitedStates::State::PostalCode.new(postal_code)
end

Instance Attribute Details

#nameUnitedStates::State::Name (readonly)



16
17
18
# File 'lib/united_states/state/designation.rb', line 16

def name
  @name
end

#postal_codeUnitedStates::State::PostalCode (readonly)



12
13
14
# File 'lib/united_states/state/designation.rb', line 12

def postal_code
  @postal_code
end

Class Method Details

.from_hash(**hash) ⇒ UnitedStates::State::Designation

Returns a Designation with attributes provided via hash.

Examples:

From Named Parameters

UnitedStates::State::Designation.from_hash(
  name: 'Hawaii', postal_code: 'HI')
# => #<UnitedStates::State::Designation:0x...@string="HI">>

From Variable

hawaii_hash = { name: 'Hawaii', postal_code: 'HI' }
UnitedStates::State::Designation.from_hash(hawaii_hash)
# => #<UnitedStates::State::Designation:0x...@string="HI">>


30
31
32
# File 'lib/united_states/state/designation.rb', line 30

def self.from_hash(**hash)
  new(name: hash[:name], postal_code: hash[:postal_code])
end

Instance Method Details

#==(other) ⇒ Boolean



53
54
55
56
# File 'lib/united_states/state/designation.rb', line 53

def ==(other)
  other.name == name &&
    other.postal_code == postal_code
end