Class: Postal::Address

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

Constant Summary collapse

Fields =
%i(recipient street zip state city country country_code).freeze

Instance Method Summary collapse

Constructor Details

#initialize(**attrs) {|_self| ... } ⇒ Address

Returns a new instance of Address.

Yields:

  • (_self)

Yield Parameters:



7
8
9
10
# File 'lib/postal_address/address.rb', line 7

def initialize(**attrs, &block)
  attrs.each { |k,v| public_send("#{k}=", v) if respond_to?("#{k}=") }
  yield self if block_given?
end

Instance Method Details

#countryObject



16
17
18
# File 'lib/postal_address/address.rb', line 16

def country
  Postal.country_names[country_code]
end

#country_code=(code) ⇒ Object



12
13
14
# File 'lib/postal_address/address.rb', line 12

def country_code=(code)
  @country_code = Postal.sanitize(code)
end

#to_hObject



24
25
26
# File 'lib/postal_address/address.rb', line 24

def to_h
  Fields.zip(values).to_h
end

#to_html(**params) ⇒ Object



32
33
34
# File 'lib/postal_address/address.rb', line 32

def to_html(**params)
  AddressFormatter::HTML.new(to_h).render(params)
end

#to_s(**params) ⇒ Object



28
29
30
# File 'lib/postal_address/address.rb', line 28

def to_s(**params)
  AddressFormatter::Text.new(to_h).render(params)
end

#valuesObject



20
21
22
# File 'lib/postal_address/address.rb', line 20

def values
  Fields.map(&method(:public_send))
end