Class: Google4R::Checkout::ShippingMethod

Inherits:
DeliveryMethod show all
Defined in:
lib/google4r/checkout/shared.rb

Overview

Abstract class for shipping methods. Do not use this class directly but only one of its subclasses.

Direct Known Subclasses

FlatRateShipping, MerchantCalculatedShipping

Instance Attribute Summary collapse

Attributes inherited from DeliveryMethod

#name, #price

Instance Method Summary collapse

Constructor Details

#initializeShippingMethod

Returns a new instance of ShippingMethod.



768
769
770
771
# File 'lib/google4r/checkout/shared.rb', line 768

def initialize
  @shipping_restrictions_allowed_areas = Array.new
  @shipping_restrictions_excluded_areas = Array.new
end

Instance Attribute Details

#shipping_restrictions_allowed_areasObject (readonly)

An Array of allowed areas for shipping-restrictions of this shipping instance. Use #create_allowed_area to add to this area but do not change it directly.



762
763
764
# File 'lib/google4r/checkout/shared.rb', line 762

def shipping_restrictions_allowed_areas
  @shipping_restrictions_allowed_areas
end

#shipping_restrictions_excluded_areasObject (readonly)

An Array of excluded areas for shipping-restrictions of this shipping instance. Use #create_excluded_area to add to this area but do not change it directly.



766
767
768
# File 'lib/google4r/checkout/shared.rb', line 766

def shipping_restrictions_excluded_areas
  @shipping_restrictions_excluded_areas
end

Instance Method Details

#create_allowed_area(clazz, &block) ⇒ Object Also known as: create_shipping_restrictions_allowed_area

Creates a new Area, adds it to the internal list of allowed areas for shipping restrictions. If you passed a block (preferred) then the block is called with the Area as the only parameter.

The area to be created depends on the given parameter clazz. It can be one of { PostalArea, UsCountryArea, UsStateArea, UsZipArea, WorldArea }.

Raises a RuntimeError if the parameter clazz is invalid.

Example

method = FlatRateShipping.new
method.create_allowed_area(UsCountryArea) do |area|
  area.area = UsCountryArea::ALL
end


823
824
825
# File 'lib/google4r/checkout/shared.rb', line 823

def create_allowed_area(clazz, &block)
  return create_area(:shipping_restrictions, :allowed_areas, clazz, &block)
end

#create_area(type, areas, clazz) {|area| ... } ⇒ Object

This method create a new instance of subclass of Area and put it in the array determined by the two symbols provided. The valid symbols for the first two parameters are:

type : :shipping_restrictions, :address_filters areas : :allowed_areas, :excluded_areas

The third parameter clazz is used to specify the type of Area you want to create. It can be one of { PostalArea, UsCountryArea, UsStateArea, UsZipArea, WorldArea }.

Raises a RuntimeError if the parameter clazz is invalid.

If you passed a block (preferred) then the block is called with the Area as the only parameter.

Example

method = MerchantCalculatedShipping.new
method.create_area(:shipping_restrictions, :allowed_areas, UsCountryArea) do |area|
  area.area = UsCountryArea::ALL
end

Yields:

  • (area)


795
796
797
798
799
800
801
802
803
804
805
806
# File 'lib/google4r/checkout/shared.rb', line 795

def create_area(type, areas, clazz, &block)
  areas_array_name = "@#{type.to_s + '_' + areas.to_s}"
  areas = instance_variable_get(areas_array_name)
  raise "Undefined instance variable: #{areas_array_name}" unless areas.nil? == false
  raise "Invalid Area class: #{clazz}!" unless [ PostalArea, UsCountryArea, UsStateArea, UsZipArea, WorldArea ].include?(clazz)
  area = clazz.new
  areas << area

  yield(area) if block_given?
  
  return area
end

#create_excluded_area(clazz, &block) ⇒ Object Also known as: create_shipping_restrictions_excluded_area

Creates a new Area, adds it to the internal list of excluded areas for shipping restrictions. If you passed a block (preferred) then the block is called with the Area as the only parameter. The created area is returned in any case.

The area to be created depends on the given parameter clazz. It can be one of { PostalArea, UsCountryArea, UsStateArea, UsZipArea, WorldArea }.

Raises a RuntimeError if the parameter clazz is invalid.

Example

method = FlatRateShipping.new
method.create_excluded_area(UsCountryArea) do |area|
  area.area = UsCountryArea::ALL
end


842
843
844
# File 'lib/google4r/checkout/shared.rb', line 842

def create_excluded_area(clazz, &block)
  return create_area(:shipping_restrictions, :excluded_areas, clazz, &block)
end