Skip to content

Commit

Permalink
fix parsing and style in fmi#2
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Nov 14, 2011
1 parent f43d75d commit 863841a
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions solutions/02.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
class Collection
def initialize songs, additional_tags
@collection = songs.lines.map { |line| Song.new *line.split(".") }
def initialize(songs, additional_tags)
@collection = songs.lines.map { |line| Song.new *parse_line(line) }
additional_tags.each do |artist, tags|
find(artist: artist).each { |song| song.tags += tags }
end
end

def find criteria
filters = []
def find(criteria)
fields = [:name,:artist,:genre,:subgenre].select { |field| criteria[field] }
filters += fields.map { |field| field_filter field, criteria[field] }
filters = fields.map { |field| field_filter(field, criteria[field]) }
filters += tags_filters(*criteria[:tags]) if criteria[:tags]
filters << criteria[:filter] if criteria[:filter]
filter = ->(song) { filters.all? { |filter| filter.call song }}
@collection.select { |song| filter.call song }
end

private
def field_filter field, value
def field_filter(field, value)
->(song) { song.send(field) == value }
end

def tags_filters *tags
def tags_filters(*tags)
tags.map { |tag| tag_filter tag }
end

def tag_filter tag
def tag_filter(tag)
tag, exclude = tag.chop, true if tag.end_with? '!'
->(song) { song.tags.include?(tag) ^ exclude }
end

def parse_array(line)
args = line.split(".")
result = args[0..1].map(&:strip)
result[2..3] = args[2].split(',').map(&:strip)
result[4] = args[3].split(',').map(&:strip) if args[3]
result
end
end

class Song
attr_accessor :name, :artist, :genre, :subgenre, :tags

def initialize name, artist, genres, tags = ""
@name,@artist = name.strip, artist.strip
@genre,@subgenre = genres.split(",").map { |genre| genre.strip }
@tags = tags.split( ",").map { |tag| tag.strip.downcase }
def initialize(name, artist, genre, subgenre = nil, tags = [])
@name, @artist, @genre, @subgenre = name, artist, genre, subgenre
@tags = tags
@tags << @genre.downcase
@tags << @subgenre.downcase if @subgenre
@tags << @subgenre.downcase if @subgenre
end
end

0 comments on commit 863841a

Please sign in to comment.