Class: SimpleCataloger::Catalog
- Inherits:
-
Object
- Object
- SimpleCataloger::Catalog
- Defined in:
- lib/simple_cataloger_dm/core/catalog.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#create(*catalog_roots) ⇒ Object
Create a new catalog.
-
#initialize(name) ⇒ Catalog
constructor
A new instance of Catalog.
-
#roots ⇒ Object
array of roots.
- #setup_db(db_path, db_log_filepath) ⇒ Object
-
#update ⇒ Object
Rebuild catalog.
-
#update_categories ⇒ Object
update config file with tag category list and association from category to tag name, so to not lose the association next time the catalog is rebuilt (updated).
- #write_config ⇒ Object
Constructor Details
#initialize(name) ⇒ Catalog
Returns a new instance of Catalog.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/simple_cataloger_dm/core/catalog.rb', line 7 def initialize(name) @name = name # find directory where to store catalog data home_dir = ENV['HOME'] || ENV['APPDATA'] if RUBY_PLATFORM =~ /linux/ catalogs_dir = File.(File.join(home_dir, ".simple_cataloger")) else catalogs_dir = File.(File.join(home_dir, "simple_cataloger")) end Dir.mkdir(catalogs_dir) unless File.directory?(catalogs_dir) db_filepath = File.join(catalogs_dir, "#{name}.sqlite3") db_log_filepath = File.join(catalogs_dir, "#{name}.log") @config_filepath = File.join(catalogs_dir, "#{name}.yml") setup_db(db_filepath, db_log_filepath) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/simple_cataloger_dm/core/catalog.rb', line 5 def name @name end |
Instance Method Details
#create(*catalog_roots) ⇒ Object
Create a new catalog
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/simple_cataloger_dm/core/catalog.rb', line 29 def create(*catalog_roots) if File.exist? @config_filepath raise SimpleCatalogerError, "cannot create already existent catalog '#{@name}'" end @config = { :roots => catalog_roots, :ignore => ['sub', 'subtitles', 'images'], :version => SimpleCataloger::VERSION } write_config update end |
#roots ⇒ Object
array of roots
110 111 112 113 |
# File 'lib/simple_cataloger_dm/core/catalog.rb', line 110 def roots read_config unless @config @config[:roots] end |
#setup_db(db_path, db_log_filepath) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/simple_cataloger_dm/core/catalog.rb', line 100 def setup_db(db_path, db_log_filepath) #TODO: logger usabile per loggare altri eventi oltre a quelli del db DataMapper::Logger.new(db_log_filepath, :debug) DataMapper.setup(:default, "sqlite3:#{db_path}") DataMapper.finalize end |
#update ⇒ Object
Rebuild catalog
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/simple_cataloger_dm/core/catalog.rb', line 45 def update unless File.exist? @config_filepath raise "cannot update catalog #{@name}" end read_config # # migrate db # DataMapper.auto_migrate! #DataMapper.auto_upgrade! # # Initialize categories # if @config[:categories] # pp @config[:categories] @config[:categories].each_pair do |category, | # puts category cat = Category.first_or_create(:name => category) .each do |name| tag = Tag.first_or_create(:name => name) tag.category = cat tag.save end end end # # read catalog root # @config[:roots].each do |root| dtw = TreeRb::DirTreeWalker.new(root) dtw.ignore /^\./ @config[:ignore].each do |i| dtw.ignore i end dtw.visit_file=true dtw.run(DirectoryVisitor.new(root)) end # # cache rating tag # = Category.first(:name => "rating") if Item.all.each do |item| t = item..find { |t| t.category == } if t item. = t.name.to_i item.save end end end end |
#update_categories ⇒ Object
update config file with tag category list and association from category to tag name, so to not lose the association next time the catalog is rebuilt (updated)
119 120 121 122 123 124 125 126 |
# File 'lib/simple_cataloger_dm/core/catalog.rb', line 119 def update_categories h = {} Category.all.each do |category| next if ["rating", "year", "unknown"].include?(category.name) h[category.name] = category..collect { |tag| tag.name } end @config[:categories] = h end |
#write_config ⇒ Object
128 129 130 |
# File 'lib/simple_cataloger_dm/core/catalog.rb', line 128 def write_config File.open(@config_filepath, "w") { |f| f.write @config.to_yaml } end |