Class: Hearken::Command::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/hearken/command/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(library) ⇒ Search

Returns a new instance of Search.



8
9
10
11
12
13
14
# File 'lib/hearken/command/search.rb', line 8

def initialize(library)
  @help = "    searches for tracks containing the specified words (in artist, title or album)\n    ids are placed on the clipboard for convenient use with +\n  EOF\n  @library = library\nend\n"

Instance Attribute Details

#helpObject (readonly)

Returns the value of attribute help.



6
7
8
# File 'lib/hearken/command/search.rb', line 6

def help
  @help
end

Instance Method Details

#execute(text) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hearken/command/search.rb', line 16

def execute(text)
  terms = text.split(/\W/)
  matches = []
  @library.tracks.each do |track|
    if terms.all? { |term| track.search_string.include? term }
      puts track
      matches << track.search_id
    end
  end

  if matches.size < 50
    IO.popen("pbcopy", "w") { |clipboard| clipboard.print matches.join " " }
    puts "Found #{matches.size} matches (ids have been placed on clipboard)"
  else
    puts "Found #{matches.size} matches"
  end
end