Sha256: 22a0097f88883627d1917d0d740c2284fdd1326c841a0cafc26a51e8796450ad

Contents?: true

Size: 1.24 KB

Versions: 62

Compression:

Stored size: 1.24 KB

Contents

module Workarea
  class Shipping
    class LocationQuery
      attr_reader :services, :country, :region

      def initialize(services, country, region)
        @services = services
        @country = typecast_country(country)
        @region = region
      end

      def location_services
        location_services = matching_services

        if has_service_with_region?
          location_services.select! do |service|
            service.regions.include?(region)
          end
        end

        location_services
      end

      private

      def matching_services
        @matching_services ||= services.select do |service|
          (country.blank? || service.country.blank? || service.country == country) &&
            (region.blank? || service.regions.blank? || service.regions.include?(region))
        end
      end

      def has_service_with_region?
        region.present? &&
          matching_services.detect { |service| service.regions.include?(region) }
      end

      def typecast_country(country)
        if country.is_a?(::Country)
          country
        elsif country.is_a?(String)
          Country[country]
        elsif country.is_a?(ActiveUtils::Country)
          Country[country.code(:alpha2)]
        end
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.15 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.36 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.14 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.35 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.13 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.34 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.12 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.33 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.11 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.10 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.32 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.9 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.31 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.8 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.30 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.7 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.29 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.6 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.28 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.5 app/models/workarea/shipping/location_query.rb