lib/musicbrainz/models/base_model.rb in musicbrainz-0.7.4 vs lib/musicbrainz/models/base_model.rb in musicbrainz-0.7.5
- old
+ new
@@ -21,9 +21,41 @@
end
def client
MusicBrainz.client
end
+
+ def search(hash)
+ hash = escape_strings(hash)
+ query_val = build_query(hash)
+ underscore_name = self.name[13..-1].underscore
+ client.load(underscore_name.to_sym, { query: query_val, limit: 10 }, { binding: underscore_name.insert(-1,"_search").to_sym })
+ end
+
+ class ::String
+ def underscore
+ self.gsub(/::/, '/').
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
+ tr("-", "_").
+ downcase
+ end
+ end
+
+ def build_query(hash)
+ return ["#{hash.first[0].to_s}:\"#{hash.first[1]}\""] if hash.size ==1
+ arr ||= []
+ hash.each { |k, v| arr << "#{k.to_s}:\"#{hash[k]}\"" }
+ arr.join(' AND ')
+ end
+
+ def escape_strings(hash)
+ hash.each { |k, v| hash[k] = CGI.escape(v).gsub(/\!/, '\!') }
+ hash
+ end
+
+ # these probably should be private... but I'm not sure how to get it to work in a module...
+ # private_class_method :build_query, :escape_strings
end
module InstanceMethods
def initialize(params = {})
params.each do |field, value|