Class: Kagu::Finder
- Inherits:
-
Object
- Object
- Kagu::Finder
- Defined in:
- lib/kagu/finder.rb
Constant Summary collapse
- MANDATORY_ATTRIBUTES =
[]
Instance Attribute Summary collapse
-
#library ⇒ Object
readonly
Returns the value of attribute library.
Class Method Summary collapse
Instance Method Summary collapse
- #find(attributes = {}) ⇒ Object
- #ignored ⇒ Object
-
#initialize(library, options = {}) ⇒ Finder
constructor
A new instance of Finder.
- #reload(options = {}) ⇒ Object
- #reload!(options = {}) ⇒ Object
- #replacements ⇒ Object
Constructor Details
#initialize(library, options = {}) ⇒ Finder
Returns a new instance of Finder.
23 24 25 26 27 |
# File 'lib/kagu/finder.rb', line 23 def initialize(library, = {}) raise ArgumentError.new("#{self.class}#library must be a library, #{library.inspect} given") unless library.is_a?(Library) @library = library reload() end |
Instance Attribute Details
#library ⇒ Object (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 |
#ignored ⇒ Object
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( = {}) @ignored = nil @replacements = nil @tracks_digests = nil .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!( = {}) @tracks = nil reload() end |
#replacements ⇒ Object
66 67 68 |
# File 'lib/kagu/finder.rb', line 66 def replacements @replacements ||= [] end |