Class: Songbirdsh::Library
- Inherits:
-
Object
- Object
- Songbirdsh::Library
- Includes:
- Debug
- Defined in:
- lib/songbirdsh/library.rb
Instance Attribute Summary collapse
-
#tracks ⇒ Object
readonly
Returns the value of attribute tracks.
Instance Method Summary collapse
- #choose_profile(path) ⇒ Object
-
#initialize(preferences) ⇒ Library
constructor
A new instance of Library.
- #reload ⇒ Object
- #with_db ⇒ Object
- #with_track(id) {|track| ... } ⇒ Object
Methods included from Debug
Constructor Details
#initialize(preferences) ⇒ Library
Returns a new instance of Library.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/songbirdsh/library.rb', line 9 def initialize preferences home = File. '~' debug "Home Directory is \"#{home}\"" songbird_home = "#{home}/Library/Application Support/Songbird2" debug "Songbird home is \"#{songbird_home}\"" profiles = "#{songbird_home}/Profiles" preferences['profile'] ||= choose_profile profiles debug "found profile \"#{preferences['profile']}\"" @db_path = "#{profiles}/#{preferences['profile']}/db/[email protected]" debug "using db \"#{@db_path}\"" end |
Instance Attribute Details
#tracks ⇒ Object (readonly)
Returns the value of attribute tracks.
7 8 9 |
# File 'lib/songbirdsh/library.rb', line 7 def tracks @tracks end |
Instance Method Details
#choose_profile(path) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/songbirdsh/library.rb', line 21 def choose_profile path choices = Dir.entries(path).select {|path| !['.','..'].include?(path) } raise 'no profiles found' if choices.empty? return choices.first if choices.size == 1 loop do choices.each {|choice| puts "* #{choice}"} print "please enter your preferred songbird profile > " subset = choices.grep(/#{gets.chomp}/) return subset.first if subset.size == 1 puts 'invalid choice - please enter the full name of the profile' end end |
#reload ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/songbirdsh/library.rb', line 54 def reload s = Time.now.to_i @tracks = [] with_db do |db| current_media_item_id, current_track = nil, nil db[:resource_properties].order(:media_item_id).each do |row| unless row[:media_item_id] == current_media_item_id append current_track current_track = Songbirdsh::Track.new row[:media_item_id] debug "Created new track with id #{current_track.id}" current_media_item_id = current_track.id end append_to_track current_track, row end append current_track end puts "Reloaded db with #{@tracks.size} tracks in #{Time.now.to_i-s} seconds" end |
#with_db ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/songbirdsh/library.rb', line 34 def with_db db = Sequel.sqlite @db_path begin val = yield db ensure db.disconnect end val end |
#with_track(id) {|track| ... } ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/songbirdsh/library.rb', line 44 def with_track id track = Songbirdsh::Track.new id with_db do |db| db[:resource_properties].filter(:media_item_id=>id).each do |row| append_to_track track, row end end yield track end |