Class: SimpleCataloger::Catalog

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_cataloger_dm/core/catalog.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.expand_path(File.join(home_dir, ".simple_cataloger"))
  else
    catalogs_dir = File.expand_path(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

#nameObject (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

Parameters:

  • array

    of directories



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

#rootsObject

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

#updateObject

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, tags|
      # puts category
      cat = Category.first_or_create(:name => category)
      tags.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
  #
  rating = Category.first(:name => "rating")
  if rating
    Item.all.each do |item|
      t = item.tags.find { |t| t.category == rating }
      if t
        item.rating = t.name.to_i
        item.save
      end
    end
  end
end

#update_categoriesObject

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.tags.collect { |tag| tag.name }
  end
  @config[:categories] = h
end

#write_configObject



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