Class: Rivendell::Import::CartFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/rivendell/import/cart_finder.rb

Direct Known Subclasses

ByApi, ByDb

Defined Under Namespace

Classes: ByApi, ByDb

Instance Method Summary collapse

Instance Method Details

#default_normalizerObject



13
14
15
16
17
# File 'lib/rivendell/import/cart_finder.rb', line 13

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

#find_all_by_title(string, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/rivendell/import/cart_finder.rb', line 4

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

#find_by_title(string, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/rivendell/import/cart_finder.rb', line 19

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