Class: Kagu::Finder

Inherits:
Object
  • Object
show all
Defined in:
lib/kagu/finder.rb

Constant Summary collapse

MANDATORY_ATTRIBUTES =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(library, options = {}) ⇒ Finder

Returns a new instance of Finder.

Raises:

  • (ArgumentError)


23
24
25
26
27
# File 'lib/kagu/finder.rb', line 23

def initialize(library, options = {})
  raise ArgumentError.new("#{self.class}#library must be a library, #{library.inspect} given") unless library.is_a?(Library)
  @library = library
  reload(options)
end

Instance Attribute Details

#libraryObject (readonly)

Returns the value of attribute library.



7
8
9
# File 'lib/kagu/finder.rb', line 7

def library
  @library
end

Class Method Details

.replace(value, replacements = {}) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/kagu/finder.rb', line 11

def self.replace(value, replacements = {})
  replaced = value.to_s.dup
  replacements.each do |pattern, replacement|
    replaced.gsub!(pattern, replacement)
  end
  replaced.presence
end

.transliterate(value) ⇒ Object



19
20
21
# File 'lib/kagu/finder.rb', line 19

def self.transliterate(value)
  ActiveSupport::Inflector.transliterate(value.to_s).squish.downcase.presence
end

Instance Method Details

#find(attributes = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kagu/finder.rb', line 29

def find(attributes = {})
  attributes.stringify_keys!
  results = [].tap do |matches|
    tracks_digests.each_with_index do |hash, digests_index|
      digests(attributes).each_with_index do |digest, digest_index|
        tracks = hash[digest].presence || next
        tracks = tracks.select { |track| !matches.any? { |match| match.include?(track) } }
        next if tracks.empty?
        index = [digests_index, digest_index].max
        matches[index] ||= []
        matches[index].push(*tracks)
      end
    end
  end
  results.compact!
  replacements.any? ? results : results.flatten
end

#ignoredObject



47
48
49
# File 'lib/kagu/finder.rb', line 47

def ignored
  @ignored ||= []
end

#reload(options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/kagu/finder.rb', line 51

def reload(options = {})
  @ignored = nil
  @replacements = nil
  @tracks_digests = nil
  options.each do |name, value|
    send("#{name}=", value) if respond_to?("#{name}=", true)
  end
  self
end

#reload!(options = {}) ⇒ Object



61
62
63
64
# File 'lib/kagu/finder.rb', line 61

def reload!(options = {})
  @tracks = nil
  reload(options)
end

#replacementsObject



66
67
68
# File 'lib/kagu/finder.rb', line 66

def replacements
  @replacements ||= []
end