Module: MusicBrainz::BaseModel::ClassMethods

Defined in:
lib/musicbrainz/models/base_model.rb

Instance Method Summary collapse

Instance Method Details

#build_query(hash) ⇒ Object



49
50
51
52
53
54
# File 'lib/musicbrainz/models/base_model.rb', line 49

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

#clientObject



23
24
25
# File 'lib/musicbrainz/models/base_model.rb', line 23

def client
  MusicBrainz.client
end

#escape_strings(hash) ⇒ Object



56
57
58
59
# File 'lib/musicbrainz/models/base_model.rb', line 56

def escape_strings(hash)
	hash.each { |k, v| hash[k] = CGI.escape(v).gsub(/\!/, '\!') }
	hash
end

#field(name, type) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/musicbrainz/models/base_model.rb', line 9

def field(name, type)
  fields[name] = type
  attr_reader name

  define_method("#{name}=") do |val|
    instance_variable_set("@#{name}", validate_type(val, type))
  end
end

#fieldsObject



18
19
20
21
# File 'lib/musicbrainz/models/base_model.rb', line 18

def fields
  instance_variable_set(:@fields, {}) unless instance_variable_defined?(:@fields)
  instance_variable_get(:@fields)
end

#find(hash) ⇒ Object



27
28
29
30
# File 'lib/musicbrainz/models/base_model.rb', line 27

def find(hash)
	underscored_name = underscore_name.to_sym
	client.load(underscored_name, hash, { binding: underscored_name, create_model: underscored_name })
end

#search(hash) ⇒ Object



32
33
34
35
36
37
# File 'lib/musicbrainz/models/base_model.rb', line 32

def search(hash)
	hash = escape_strings(hash)
	query_val = build_query(hash)
	underscored_name = underscore_name
	client.load(underscored_name.to_sym, { query: query_val, limit: 10 }, { binding: underscored_name.insert(-1,"_search").to_sym })
end

#underscore_nameObject



61
62
63
64
# File 'lib/musicbrainz/models/base_model.rb', line 61

def underscore_name
	# self.name[13..-1] => removes MusicBrainz::
	self.name[13..-1].underscore
end