Class: Postal::Address
- Inherits:
-
Object
- Object
- Postal::Address
- Defined in:
- lib/postal_address/address.rb
Constant Summary collapse
- Fields =
[:recipient, :street, :zip, :state, :city, :country, :country_code]
Instance Method Summary collapse
- #country ⇒ Object
- #country_code=(code) ⇒ Object
-
#initialize(attrs = {}) ⇒ Address
constructor
A new instance of Address.
- #to_h ⇒ Object
- #to_html(params = {}) ⇒ Object
- #to_s ⇒ Object
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
#country ⇒ Object
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_h ⇒ Object
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_s ⇒ Object
34 35 36 |
# File 'lib/postal_address/address.rb', line 34 def to_s AddressFormatter::Text.new(to_h).render end |