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.4.27 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.4 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.26 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.3 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.25 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.2 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.24 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.1 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.23 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.22 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.0 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.21 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.0.beta.1 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.20 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.19 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.18 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.17 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.16 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.15 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.14 app/models/workarea/shipping/location_query.rb