Class: Dhun::Query
- Inherits:
-
Object
- Object
- Dhun::Query
- Defined in:
- lib/dhun/query.rb
Constant Summary collapse
- MAPPINGS =
{ :file => :kMDItemFSName, :album => :kMDItemAlbum, :artist => :kMDItemAuthors, :title => :kMDItemTitle, :genre => :kMDItemMusicalGenre, :composer => :kMDItemComposer, :display => :kMDItemDisplayName }
Instance Attribute Summary collapse
-
#is_valid ⇒ Object
Returns the value of attribute is_valid.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#query_fields ⇒ Object
Returns the value of attribute query_fields.
-
#query_search ⇒ Object
Returns the value of attribute query_search.
-
#spotlight_query ⇒ Object
Returns the value of attribute spotlight_query.
Instance Method Summary collapse
-
#create_filter_query(filters, mappings) ⇒ Object
create filter queries { :album => ‘test’ } => “kMDItemAlbum == ‘test’wc” ADDITIONALLY, throws out any non matching filters { :album => ‘test’, :booger => ‘one’ } => “kMDItemAlbum == ‘test’wc”.
-
#create_spotlight_query(filter_query, string_query) ⇒ Object
create spotlight queries with => ‘test’,“” => “kMDItemContentTypeTree == ‘public.audio’ && kMDItemAlbum == ‘test’wc”.
-
#create_string_query(strings, mappings) ⇒ Object
create string queries this sets string to all fields not already matched by create_filter_query ‘test’ => “( kMDItemTitle == ‘holy’wc || kMDItemMusicalGenre == ‘holy’wc )” if kMDItemTitle and kMDItemMusicalGenre are the only fields left open.
-
#execute_spotlight_query ⇒ Object
Use extension to query spotlight.
-
#initialize(search = nil, fields = {}) ⇒ Query
constructor
A new instance of Query.
- #is_valid? ⇒ Boolean
-
#parse! ⇒ Object
parses all search terms and stores query return false if both are empty.
Constructor Details
#initialize(search = nil, fields = {}) ⇒ Query
Returns a new instance of Query.
18 19 20 21 22 23 |
# File 'lib/dhun/query.rb', line 18 def initialize(search=nil,fields={}) @logger = Dhun::Logger.instance @query_search = search @query_fields = fields @is_valid = parse! end |
Instance Attribute Details
#is_valid ⇒ Object
Returns the value of attribute is_valid.
16 17 18 |
# File 'lib/dhun/query.rb', line 16 def is_valid @is_valid end |
#logger ⇒ Object
Returns the value of attribute logger.
16 17 18 |
# File 'lib/dhun/query.rb', line 16 def logger @logger end |
#query_fields ⇒ Object
Returns the value of attribute query_fields.
16 17 18 |
# File 'lib/dhun/query.rb', line 16 def query_fields @query_fields end |
#query_search ⇒ Object
Returns the value of attribute query_search.
16 17 18 |
# File 'lib/dhun/query.rb', line 16 def query_search @query_search end |
#spotlight_query ⇒ Object
Returns the value of attribute spotlight_query.
16 17 18 |
# File 'lib/dhun/query.rb', line 16 def spotlight_query @spotlight_query end |
Instance Method Details
#create_filter_query(filters, mappings) ⇒ Object
create filter queries { :album => ‘test’ } => “kMDItemAlbum == ‘test’wc” ADDITIONALLY, throws out any non matching filters { :album => ‘test’, :booger => ‘one’ } => “kMDItemAlbum == ‘test’wc”
44 45 46 47 48 49 50 51 |
# File 'lib/dhun/query.rb', line 44 def create_filter_query(filters,mappings) filters.collect do |field,value| md_item = MAPPINGS[field.to_sym] next unless md_item # makes sure that field is to sym, or funky stuff happens mappings.delete md_item "#{md_item} == '#{value}'wc && " end.join.chomp(" && ") end |
#create_spotlight_query(filter_query, string_query) ⇒ Object
create spotlight queries with => ‘test’,“” => “kMDItemContentTypeTree == ‘public.audio’ && kMDItemAlbum == ‘test’wc”
73 74 75 76 77 |
# File 'lib/dhun/query.rb', line 73 def create_spotlight_query(filter_query,string_query) ["kMDItemContentTypeTree == 'public.audio'", filter_query, string_query].select do |s| s.length > 0 end.join(" && ") end |
#create_string_query(strings, mappings) ⇒ Object
create string queries this sets string to all fields not already matched by create_filter_query ‘test’ => “( kMDItemTitle == ‘holy’wc || kMDItemMusicalGenre == ‘holy’wc )” if kMDItemTitle and kMDItemMusicalGenre are the only fields left open. returns “” if given nil if given multiple strings: ‘holy’,‘test’ => ( kMDItemTitle == ‘holy’wc || kMDItemMusicalGenre == ‘holy’wc ) && ( kMDItemTitle == ‘test’wc || kMDItemMusicalGenre == ‘test’wc )
62 63 64 65 66 67 68 |
# File 'lib/dhun/query.rb', line 62 def create_string_query(strings,mappings) return "" unless strings strings.collect do |keyword| query = mappings.collect { |key| "%s == '%s'wc" % [key,keyword] }.join(" || ") "( #{query} )" end.join(" && ") end |
#execute_spotlight_query ⇒ Object
Use extension to query spotlight
80 81 82 |
# File 'lib/dhun/query.rb', line 80 def execute_spotlight_query return DhunExt.query_spotlight(@spotlight_query) end |
#is_valid? ⇒ Boolean
84 |
# File 'lib/dhun/query.rb', line 84 def is_valid?; @is_valid; end |
#parse! ⇒ Object
parses all search terms and stores query return false if both are empty.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dhun/query.rb', line 27 def parse! return false if @query_search.nil? and @query_fields.empty? mappings = MAPPINGS.values #instantiate mappings to be picked off by query methods #create the queries filter_query = create_filter_query(@query_fields,mappings) string_query = create_string_query(@query_search,mappings) @spotlight_query = create_spotlight_query(filter_query,string_query) @logger.debug @spotlight_query return true end |