# encoding: utf-8

# Copyright (C) 2011 Clemens Fuchslocher <clemens@vakuumverpackt.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

module VoidMeter

	class Results
		attr_reader :results

		def initialize(file)
			@file = file
			load
		end

		def load
			if File.exist?(@file)
				@results = Marshal.load(IO.read(@file))
			else
				@results = {}
			end
		end

		def save
			File.open(@file, 'w') do |file|
				file.write(Marshal.dump(@results))
			end
		end

		def get_project(name)
			@results[name]
		end

		def get_result(name, version)
			if @results[name] != nil
				@results[name][version]
			end
		end

		def set_result(name, version, result)
			if @results[name].nil?
				@results[name] = {}
			end
			@results[name][version] = result
		end

		def has_result?(name, version)
			get_result(name, version) != nil
		end

		def remove_project(name)
			@results.delete_if do |project|
				project == name
			end
		end

		def remove_result(name, version)
			project = get_project(name)
			if project != nil
				project.delete(version)
			end
		end
	end

end
