Created
July 26, 2012 16:08
-
-
Save tzvetkoff/3182946 to your computer and use it in GitHub Desktop.
foobarbazbaz.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def collection_from_string(songs_as_string, artist_tags) | |
songs_as_string.strip.split("\n").map do |line| | |
name, artist, genre, tags = line.split('.').map(&:strip) | |
genre, subgenre = genre.split(',').map(&:strip) | |
tags = tags.to_s.split(',').push(genre).push(subgenre).concat(Array(artist_tags[artist])).reject(&:nil?).map(&:strip).map(&:downcase) | |
{:name => name, :artist => artist, :genre => genre, :subgenre => subgenre, :tags => tags} | |
end | |
end | |
def find_in_collection(collection, criteria) | |
collection.select do |song| | |
criteria.all? do |k, v| | |
if k == :filter | |
v.call(song) | |
else | |
Array(v).all? do |t| | |
(k == :tags && t.end_with?('!')) ^ song[k].include?(k == :tags && t.chomp('!') || t) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment