Class: ActiveShipping::Location
- Inherits:
-
Object
- Object
- ActiveShipping::Location
- Defined in:
- lib/active_shipping/location.rb
Constant Summary collapse
- ADDRESS_TYPES =
%w(residential commercial po_box)
- ATTRIBUTE_ALIASES =
{ name: [:name], country: [:country_code, :country], postal_code: [:postal_code, :zip, :postal], province: [:province_code, :state_code, :territory_code, :region_code, :province, :state, :territory, :region], city: [:city, :town], address1: [:address1, :address, :street], address2: [:address2], address3: [:address3], phone: [:phone, :phone_number], fax: [:fax, :fax_number], email: [:email], address_type: [:address_type], company_name: [:company, :company_name], }.freeze
Instance Attribute Summary collapse
-
#address1 ⇒ Object
readonly
Returns the value of attribute address1.
-
#address2 ⇒ Object
readonly
Returns the value of attribute address2.
-
#address3 ⇒ Object
readonly
Returns the value of attribute address3.
-
#address_type ⇒ Object
Returns the value of attribute address_type.
-
#city ⇒ Object
readonly
Returns the value of attribute city.
-
#company_name ⇒ Object
(also: #company)
readonly
Returns the value of attribute company_name.
-
#country ⇒ Object
readonly
Returns the value of attribute country.
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#fax ⇒ Object
readonly
Returns the value of attribute fax.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#phone ⇒ Object
readonly
Returns the value of attribute phone.
-
#postal_code ⇒ Object
(also: #zip, #postal)
readonly
Returns the value of attribute postal_code.
-
#province ⇒ Object
(also: #state, #territory, #region)
readonly
Returns the value of attribute province.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #address2_and_3 ⇒ Object
- #commercial? ⇒ Boolean
- #country_code(format = :alpha2) ⇒ Object
-
#initialize(options = {}) ⇒ Location
constructor
A new instance of Location.
- #inspect ⇒ Object
- #po_box? ⇒ Boolean
- #prettyprint ⇒ Object
- #residential? ⇒ Boolean
- #to_hash ⇒ Object
- #to_s ⇒ Object
- #unknown? ⇒ Boolean
-
#zip_plus_4 ⇒ Object
Returns the postal code as a properly formatted Zip+4 code, e.g.
Constructor Details
#initialize(options = {}) ⇒ Location
Returns a new instance of Location.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/active_shipping/location.rb', line 43 def initialize( = {}) @country = if [:country].nil? || [:country].is_a?(ActiveUtils::Country) [:country] else ActiveUtils::Country.find([:country]) end @postal_code = [:postal_code] || [:postal] || [:zip] @province = [:province] || [:state] || [:territory] || [:region] @city = [:city] @name = [:name] @address1 = [:address1] @address2 = [:address2] @address3 = [:address3] @phone = [:phone] @fax = [:fax] @email = [:email] @company_name = [:company_name] || [:company] self.address_type = [:address_type] end |
Instance Attribute Details
#address1 ⇒ Object (readonly)
Returns the value of attribute address1.
21 22 23 |
# File 'lib/active_shipping/location.rb', line 21 def address1 @address1 end |
#address2 ⇒ Object (readonly)
Returns the value of attribute address2.
21 22 23 |
# File 'lib/active_shipping/location.rb', line 21 def address2 @address2 end |
#address3 ⇒ Object (readonly)
Returns the value of attribute address3.
21 22 23 |
# File 'lib/active_shipping/location.rb', line 21 def address3 @address3 end |
#address_type ⇒ Object
Returns the value of attribute address_type.
21 22 23 |
# File 'lib/active_shipping/location.rb', line 21 def address_type @address_type end |
#city ⇒ Object (readonly)
Returns the value of attribute city.
21 22 23 |
# File 'lib/active_shipping/location.rb', line 21 def city @city end |
#company_name ⇒ Object (readonly) Also known as: company
Returns the value of attribute company_name.
21 22 23 |
# File 'lib/active_shipping/location.rb', line 21 def company_name @company_name end |
#country ⇒ Object (readonly)
Returns the value of attribute country.
21 22 23 |
# File 'lib/active_shipping/location.rb', line 21 def country @country end |
#email ⇒ Object (readonly)
Returns the value of attribute email.
21 22 23 |
# File 'lib/active_shipping/location.rb', line 21 def email @email end |
#fax ⇒ Object (readonly)
Returns the value of attribute fax.
21 22 23 |
# File 'lib/active_shipping/location.rb', line 21 def fax @fax end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
21 22 23 |
# File 'lib/active_shipping/location.rb', line 21 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
21 22 23 |
# File 'lib/active_shipping/location.rb', line 21 def @options end |
#phone ⇒ Object (readonly)
Returns the value of attribute phone.
21 22 23 |
# File 'lib/active_shipping/location.rb', line 21 def phone @phone end |
#postal_code ⇒ Object (readonly) Also known as: zip, postal
Returns the value of attribute postal_code.
21 22 23 |
# File 'lib/active_shipping/location.rb', line 21 def postal_code @postal_code end |
#province ⇒ Object (readonly) Also known as: state, territory, region
Returns the value of attribute province.
21 22 23 |
# File 'lib/active_shipping/location.rb', line 21 def province @province end |
Class Method Details
.from(object, options = {}) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/active_shipping/location.rb', line 65 def self.from(object, = {}) return object if object.is_a?(ActiveShipping::Location) attributes = {} hash_access = object.respond_to?(:[]) ATTRIBUTE_ALIASES.each do |attribute, aliases| aliases.detect do |sym| value = object[sym] if hash_access if !value && object.respond_to?(sym) && (!hash_access || !Hash.public_instance_methods.include?(sym)) value = object.send(sym) end attributes[attribute] = value if value end end attributes.delete(:address_type) unless ADDRESS_TYPES.include?(attributes[:address_type].to_s) new(attributes.update()) end |
Instance Method Details
#==(other) ⇒ Object
162 163 164 |
# File 'lib/active_shipping/location.rb', line 162 def ==(other) to_hash == other.to_hash end |
#address2_and_3 ⇒ Object
158 159 160 |
# File 'lib/active_shipping/location.rb', line 158 def address2_and_3 [address2, address3].reject(&:blank?).join(", ") end |
#commercial? ⇒ Boolean
98 99 100 |
# File 'lib/active_shipping/location.rb', line 98 def commercial? @address_type == 'commercial' end |
#country_code(format = :alpha2) ⇒ Object
90 91 92 |
# File 'lib/active_shipping/location.rb', line 90 def country_code(format = :alpha2) @country.nil? ? nil : @country.code(format).value end |
#inspect ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/active_shipping/location.rb', line 145 def inspect string = prettyprint string << "\nPhone: #{@phone}" unless @phone.blank? string << "\nFax: #{@fax}" unless @fax.blank? string << "\nEmail: #{@email}" unless @email.blank? string end |
#po_box? ⇒ Boolean
102 103 104 |
# File 'lib/active_shipping/location.rb', line 102 def po_box? @address_type == 'po_box' end |
#prettyprint ⇒ Object
138 139 140 141 142 143 |
# File 'lib/active_shipping/location.rb', line 138 def prettyprint chunks = [@name, @address1, @address2, @address3] chunks << [@city, @province, @postal_code].reject(&:blank?).join(', ') chunks << @country chunks.reject(&:blank?).join("\n") end |
#residential? ⇒ Boolean
94 95 96 |
# File 'lib/active_shipping/location.rb', line 94 def residential? @address_type == 'residential' end |
#to_hash ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/active_shipping/location.rb', line 116 def to_hash { country: country_code, postal_code: postal_code, province: province, city: city, name: name, address1: address1, address2: address2, address3: address3, phone: phone, fax: fax, email: email, address_type: address_type, company_name: company_name } end |
#to_s ⇒ Object
134 135 136 |
# File 'lib/active_shipping/location.rb', line 134 def to_s prettyprint.gsub(/\n/, ' ') end |
#unknown? ⇒ Boolean
106 107 108 |
# File 'lib/active_shipping/location.rb', line 106 def unknown? country_code == 'ZZ' end |
#zip_plus_4 ⇒ Object
Returns the postal code as a properly formatted Zip+4 code, e.g. "77095-2233"
154 155 156 |
# File 'lib/active_shipping/location.rb', line 154 def zip_plus_4 "#{$1}-#{$2}" if /(\d{5})-?(\d{4})/ =~ @postal_code end |