Class: ActiveMerchant::Shipping::Location
- Defined in:
- lib/active_shipping/shipping/location.rb
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
readonly
Returns the value of attribute address_type.
-
#city ⇒ Object
readonly
Returns the value of attribute city.
-
#country ⇒ Object
readonly
Returns the value of attribute country.
-
#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
- #commercial? ⇒ Boolean
- #country_code(format = :alpha2) ⇒ Object
-
#initialize(options = {}) ⇒ Location
constructor
A new instance of Location.
- #inspect ⇒ Object
- #prettyprint ⇒ Object
- #residential? ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Location
Returns a new instance of Location.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/active_shipping/shipping/location.rb', line 24 def initialize( = {}) @country = ([:country].nil? or [:country].is_a?(ActiveMerchant::Country)) ? [:country] : ActiveMerchant::Country.find([:country]) @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] raise ArgumentError.new('address_type must be either "residential" or "commercial"') if [:address_type] and not (["residential", "commercial", ""]).include?([:address_type].to_s) @address_type = [:address_type].nil? ? nil : [:address_type].to_s end |
Instance Attribute Details
#address1 ⇒ Object (readonly)
Returns the value of attribute address1.
5 6 7 |
# File 'lib/active_shipping/shipping/location.rb', line 5 def address1 @address1 end |
#address2 ⇒ Object (readonly)
Returns the value of attribute address2.
5 6 7 |
# File 'lib/active_shipping/shipping/location.rb', line 5 def address2 @address2 end |
#address3 ⇒ Object (readonly)
Returns the value of attribute address3.
5 6 7 |
# File 'lib/active_shipping/shipping/location.rb', line 5 def address3 @address3 end |
#address_type ⇒ Object (readonly)
Returns the value of attribute address_type.
5 6 7 |
# File 'lib/active_shipping/shipping/location.rb', line 5 def address_type @address_type end |
#city ⇒ Object (readonly)
Returns the value of attribute city.
5 6 7 |
# File 'lib/active_shipping/shipping/location.rb', line 5 def city @city end |
#country ⇒ Object (readonly)
Returns the value of attribute country.
5 6 7 |
# File 'lib/active_shipping/shipping/location.rb', line 5 def country @country end |
#fax ⇒ Object (readonly)
Returns the value of attribute fax.
5 6 7 |
# File 'lib/active_shipping/shipping/location.rb', line 5 def fax @fax end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/active_shipping/shipping/location.rb', line 5 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/active_shipping/shipping/location.rb', line 5 def @options end |
#phone ⇒ Object (readonly)
Returns the value of attribute phone.
5 6 7 |
# File 'lib/active_shipping/shipping/location.rb', line 5 def phone @phone end |
#postal_code ⇒ Object (readonly) Also known as: zip, postal
Returns the value of attribute postal_code.
5 6 7 |
# File 'lib/active_shipping/shipping/location.rb', line 5 def postal_code @postal_code end |
#province ⇒ Object (readonly) Also known as: state, territory, region
Returns the value of attribute province.
5 6 7 |
# File 'lib/active_shipping/shipping/location.rb', line 5 def province @province end |
Class Method Details
.from(object, options = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/active_shipping/shipping/location.rb', line 41 def self.from(object, ={}) return object if object.is_a? ActiveMerchant::Shipping::Location attr_mappings = { :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], :address_type => [:address_type] } attributes = {} hash_access = begin object[:some_symbol] true rescue false end attr_mappings.each do |pair| pair[1].each do |sym| if value = (object[sym] if hash_access) || (object.send(sym) if object.respond_to?(sym) && (!hash_access || !Hash.public_instance_methods.include?(sym.to_s))) attributes[pair[0]] = value break end end end attributes.delete(:address_type) unless %w{residential commercial}.include?(attributes[:address_type].to_s) self.new(attributes.update()) end |
Instance Method Details
#commercial? ⇒ Boolean
80 |
# File 'lib/active_shipping/shipping/location.rb', line 80 def commercial?; (@address_type == 'commercial') end |
#country_code(format = :alpha2) ⇒ Object
75 76 77 |
# File 'lib/active_shipping/shipping/location.rb', line 75 def country_code(format = :alpha2) @country.nil? ? nil : @country.code(format).value end |
#inspect ⇒ Object
94 95 96 97 98 99 |
# File 'lib/active_shipping/shipping/location.rb', line 94 def inspect string = prettyprint string << "\nPhone: #{@phone}" unless @phone.blank? string << "\nFax: #{@fax}" unless @fax.blank? string end |
#prettyprint ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/active_shipping/shipping/location.rb', line 86 def prettyprint chunks = [] chunks << [@name, @address1,@address2,@address3].reject {|e| e.blank?}.join("\n") chunks << [@city,@province,@postal_code].reject {|e| e.blank?}.join(', ') chunks << @country chunks.reject {|e| e.blank?}.join("\n") end |
#residential? ⇒ Boolean
79 |
# File 'lib/active_shipping/shipping/location.rb', line 79 def residential?; (@address_type == 'residential') end |
#to_s ⇒ Object
82 83 84 |
# File 'lib/active_shipping/shipping/location.rb', line 82 def to_s prettyprint.gsub(/\n/, ' ') end |