Sha256: 3b7e1f4003a805ee5cbbf6c73a57a004caeddd31e553a72bf58a02cf512b8c65

Contents?: true

Size: 1.36 KB

Versions: 10

Compression:

Stored size: 1.36 KB

Contents

module Rivendell::Import
  class CartFinder

    def find_all_by_title(string, options = {})
      normalizer = options.delete(:normalizer) || Proc.new { |id| id }

      string = normalizer.call(string)
      carts(options).select do |cart|
        normalizer.call(cart.title) == string
      end
    end
    
    def default_normalizer
      Proc.new do |string|
        ActiveSupport::Multibyte::Chars.new(string).normalize(:kd).gsub(/[^\x00-\x7F]/n,'').downcase.to_s.gsub(/[^a-z0-9]/," ").gsub(/[ ]+/," ")
      end
    end

    def find_by_title(string, options = {})
      matching_carts = find_all_by_title(string, options)

      if matching_carts.blank?
        matching_carts = find_all_by_title(string, options.merge(:normalizer => default_normalizer))
      end

      matching_carts.first if matching_carts.one?
    end

    class ByApi < CartFinder

      attr_accessor :xport

      def initialize(xport)
        @xport = xport
      end

      def cache
        clear if purged_at < time_to_live.ago
        @cache ||= {}
      end

      def clear
        @cache = nil
      end

      @@time_to_live = 600
      cattr_accessor :time_to_live
      
      attr_accessor :purged_at
      
      def purged_at
        @purged_at ||= Time.now
      end
      
      def carts(options = {})
        cache[options.to_s] ||= xport.list_carts(options)
      end

    end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rivendell-import-1.04 lib/rivendell/import/cart_finder.rb
rivendell-import-1.03 lib/rivendell/import/cart_finder.rb
rivendell-import-1.02 lib/rivendell/import/cart_finder.rb
rivendell-import-1.01 lib/rivendell/import/cart_finder.rb
rivendell-import-0.10 lib/rivendell/import/cart_finder.rb
rivendell-import-0.9 lib/rivendell/import/cart_finder.rb
rivendell-import-0.8 lib/rivendell/import/cart_finder.rb
rivendell-import-0.7 lib/rivendell/import/cart_finder.rb
rivendell-import-0.6 lib/rivendell/import/cart_finder.rb
rivendell-import-0.0.5 lib/rivendell/import/cart_finder.rb