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.27 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.26 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.45 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.25 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.23 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.44 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.22 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.43 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.21 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.42 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.20 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.41 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.19 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.40 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.18 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.39 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.17 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.38 app/models/workarea/shipping/location_query.rb
workarea-core-3.5.16 app/models/workarea/shipping/location_query.rb
workarea-core-3.4.37 app/models/workarea/shipping/location_query.rb