Class: Postal::Address

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

Constant Summary collapse

Fields =
[:recipient, :street, :zip, :state, :city, :country, :country_code]

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Address

Returns a new instance of Address.



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

def initialize(attrs={})
  attrs.each do |field, value|
    self.public_send(:"#{field}=", value) if self.respond_to?(:"#{field}=")
  end if attrs
end

Instance Method Details

#countryObject



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

def country
  @country ||= Postal.country_names[country_code]
end

#country_code=(code) ⇒ Object



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

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

#to_hObject



30
31
32
# File 'lib/postal_address/address.rb', line 30

def to_h
  Fields.each_with_object({}) { |field, hash| hash[field] = public_send(field) }
end

#to_html(params = {}) ⇒ Object



38
39
40
# File 'lib/postal_address/address.rb', line 38

def to_html(params={})
  AddressFormatter::HTML.new(to_h).render(params)
end

#to_sObject



34
35
36
# File 'lib/postal_address/address.rb', line 34

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