Skip to content

Instantly share code, notes, and snippets.

@tomekwojcik
Created July 17, 2011 16:38
Show Gist options
  • Save tomekwojcik/1087768 to your computer and use it in GitHub Desktop.
Save tomekwojcik/1087768 to your computer and use it in GitHub Desktop.
My first Ruby "app".
# -*- coding: utf-8 -*-
VERSION = "1.0"
DEFAULT_LIBRARY = "library.cdlib"
class Library
attr_reader :entry_fields
attr_accessor :path
def initialize(path=DEFAULT_LIBRARY)
begin
@library = Marshal.load(File.open(path, 'r:binary'))
rescue
@library = { cds: [] }
if path == DEFAULT_LIBRARY
@path = path
else
@path = nil
end
else
@path = path
end
@dirty = false
@entry_fields = {
artist: "Artist",
title: "Title",
year: "Year",
genre: "Genre",
tracks: "Tracks count",
comment: "Comment"
}
end
def dirty?()
return @dirty
end
def has_file?()
return @path != nil
end
def add()
record = {}
@entry_fields.each do |key, value|
print "#{value}: "
record[key] = STDIN.readline.chomp()
end
@library[:cds] << record
@dirty = true
end
def list()
@library[:cds].each_with_index do |item, index|
puts cd_to_str(item, index)
end
end
def save()
if @path != nil
Marshal.dump(@library, File.open(@path, 'w:binary'))
@dirty = false
end
end
def search(terms)
idx = 0
@library[:cds].each do |item|
terms.each do |key, value|
if item[key].index(Regexp.new("#{value}", Regexp::IGNORECASE)) != nil
puts cd_to_str(item, idx)
idx += 1
break
end
end
end
end
private
def cd_to_str(item, index)
return %Q|#{index + 1}. "#{item[:title]}" by #{item[:artist]} (#{item[:year]})|
end
end
def ask_for_file_path(check_if_exists=true)
path = nil
loop do
print "File path? "
path = STDIN.readline.chomp()
if path == ''
next
end
if check_if_exists == true
if File.file?(path) == true
break
end
else
break
end
end
return path
end
def load_library()
path = ask_for_file_path()
return Library.new(path)
end
def menu()
puts
puts "[1] List CDs."
puts "[2] Find a CD."
puts "[3] Add a CD."
puts "[4] Save the library."
puts "---------------------"
puts "[C] Create a new library."
puts "[L] Load another library."
puts "[Q] Quit."
puts
print "Your choice? "
return STDIN.readline.chomp.downcase()
end
def main()
title = "CD Library version #{VERSION} by Tomek Wójcik"
puts title
puts "-" * title.length
library = nil
if File.file?(DEFAULT_LIBRARY) == false
print "Default library not found. [C]reate a new one or [L]oad existing? "
choice = STDIN.readline.chomp.downcase()
if choice == 'c'
library = Library.new(DEFAULT_LIBRARY)
else
library = load_library()
end
else
library = Library.new()
end
save_library = Proc.new do
if library.has_file? == false
path = ask_for_file_path(false)
library.path = path
end
library.save()
end
save_dirty_library = Proc.new do
if library.dirty? == true
puts
print "The library has been changed. Do you wish to save your changes [Y/n]? "
choice = STDIN.readline.chomp.downcase
if choice == 'y'
save_library.call()
end
end
end
at_exit do
save_dirty_library.call()
end
loop do
case menu()
when 'q'
break
when '1'
library.list()
print 'Press [ENTER] to continue'
STDIN.readline()
when '2'
terms = {}
library.entry_fields.each do |key, value|
print "#{value}? "
search = STDIN.readline.chomp()
if search != ""
terms[key] = search
end
end
library.search(terms)
print 'Press [ENTER] to continue'
STDIN.readline()
when '3'
library.add()
when '4'
save_library.call()
when 'c'
save_dirty_library.call()
library = Library.new(nil)
when 'l'
save_dirty_library.call()
library = load_library()
end
end
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment