Class: SimpleCataloger::CatOnSqlite

Inherits:
Object
  • Object
show all
Defined in:
lib/dircat/cat_on_sqlite/cat_on_sqlite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ CatOnSqlite

Returns a new instance of CatOnSqlite.

Parameters:

  • path (Object)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dircat/cat_on_sqlite/cat_on_sqlite.rb', line 9

def initialize(path, options = {})
  @verbose ||= options[:verbose]
  @path = File.expand_path(path)
  @name = File.basename(path)
  catalogs_dir = File.dirname(path)

  #
  # path db, config, etc...
  #
  @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")

  #TODO: logger usabile per loggare altri eventi oltre a quelli del db

  database_filename = File.join(File.dirname(__FILE__), %w{default_database_config.yml})
  @active_record_config        = YAML.load(File.open(database_filename))
  # pp config
  # ActiveRecord::Base.connection.create_database(catalog_dbname)
  # ActiveRecord::Base.establish_connection(config['films_mysql'])
  @active_record_config['films_sqlite']['database'] = @db_filepath
  @active_record_config                             = @active_record_config['films_sqlite']

  puts "database in '#{@db_filepath}'" if @verbose
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/dircat/cat_on_sqlite/cat_on_sqlite.rb', line 6

def name
  @name
end

Instance Method Details

#create(*catalog_roots) ⇒ Object

Create a new catalog

Parameters:

  • array

    of directories



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
# File 'lib/dircat/cat_on_sqlite/cat_on_sqlite.rb', line 45

def create(*catalog_roots)
  fs = [@db_filepath, @db_log_filepath, @config_filepath]
  fs.each do |f|
    FileUtils.rm(f) if File.exist?(f)
  end

  if File.exist? @config_filepath
    raise SimpleCatalogerError, "cannot create already existent catalog '#{@name}'"
  end
  @config = {
      :roots   => catalog_roots,
      :ignore  => %w{sub subtitles images},
      :version => DirCat::VERSION
  }
  write_config

  #
  # migrate db
  #
  # TODO: drop tables!
  ActiveRecord::Base.establish_connection(@active_record_config)
  ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : false
  migration_dir = File.join(File.dirname(__FILE__), %w{migration})
  unless Dir.exist? migration_dir
    raise "migration dir '#{migration_dir}' not exists"
  end
  ActiveRecord::Migrator.migrate(migration_dir, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
  require_models
end

#load_categories_in_configObject

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)



141
142
143
144
145
146
147
148
# File 'lib/dircat/cat_on_sqlite/cat_on_sqlite.rb', line 141

def load_categories_in_config
  h = { }
  Category.all.each do |category|
    next if %w{rating year unknown}.include?(category.name)
    h[category.name] = category.tags.collect { |tag| tag.name }
  end
  @config[:categories] = h
end

#openObject



35
36
37
38
39
# File 'lib/dircat/cat_on_sqlite/cat_on_sqlite.rb', line 35

def open
  ActiveRecord::Base.establish_connection(@active_record_config)
  require_models
  self
end

#rootsObject

array of roots



132
133
134
135
# File 'lib/dircat/cat_on_sqlite/cat_on_sqlite.rb', line 132

def roots
  read_config unless @config
  @config[:roots]
end

#updateObject

Rebuild catalog



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/dircat/cat_on_sqlite/cat_on_sqlite.rb', line 78

def update
  unless File.exist? @config_filepath
    raise "cannot update catalog #{@name}"
  end
  open
  read_config

  #
  # Initialize tag and categories from config
  #
  if @config[:categories]
    # pp @config[:categories]
    @config[:categories].each_pair do |category, tags|
      # puts category
      cat = Category.find_or_create_by_name(category)
      tags.each do |name|
        tag          = Tag.find_or_create_by_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.find_by_name("rating")
  if rating
    Item.all.each do |item|
      t = item.tags.find_by_category_id(rating.id)
      if t
        item.rating = t.name.to_i
        item.save
      end
    end
  end
end

#write_configObject



150
151
152
# File 'lib/dircat/cat_on_sqlite/cat_on_sqlite.rb', line 150

def write_config
  File.open(@config_filepath, "w") { |f| f.write @config.to_yaml }
end