taggata 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,33 +0,0 @@
1
- module Taggata
2
- class File < Sequel::Model(:files)
3
- set_schema do
4
- primary_key :id
5
- String :name
6
- foreign_key :parent_id, :directories, :on_delete => :set_null
7
- end
8
-
9
- def before_destroy
10
- remove_all_tags
11
- end
12
-
13
- create_table unless table_exists?
14
-
15
- require 'taggata/directory'
16
-
17
- many_to_one :parent,
18
- :key => :parent_id,
19
- :class => ::Taggata::Directory
20
-
21
- many_to_many :tags,
22
- :left_id => :file_id,
23
- :right_id => :tag_id,
24
- :join_table => :file_tags
25
-
26
- # Gets full path of the file
27
- #
28
- # @return String full path of the file
29
- def path
30
- ::File.join(parent.path, name)
31
- end
32
- end
33
- end
@@ -1,72 +0,0 @@
1
- module Taggata
2
- class FilesystemScanner
3
- # Initialize scanner's internal objects and default tag
4
- def initialize
5
- @jobs = []
6
- @done_files = 0
7
- @done_directories = 0
8
- end
9
-
10
- # Report progress
11
- def report
12
- print 'Done files/dirs - Queued: ',
13
- "#{@done_files}/#{@done_directories} ",
14
- "- #{@jobs.length}\n"
15
- end
16
-
17
- # Process directory at full path
18
- #
19
- # @param dir String name of the directory
20
- # @param path String full path of the directory
21
- def do_job(dir_id, path)
22
- contents = Dir.glob("#{path}/*")
23
- .reduce(::Taggata::File => [],
24
- ::Taggata::Directory => []) do |acc, cur|
25
- key = ::File.file?(cur) ? ::Taggata::File : ::Taggata::Directory
26
- acc.merge(key => acc[key].push(cur))
27
- end
28
-
29
- contents.each_pair do |klass, files|
30
- save_missing files.map { |f| ::File.basename f },
31
- dir_id,
32
- klass unless files.empty?
33
- end
34
- @done_files += contents[::Taggata::File].length
35
- add_directory_jobs contents[::Taggata::Directory],
36
- dir_id unless contents[::Taggata::Directory].empty?
37
- @done_directories += 1
38
- end
39
-
40
- def add_directory_jobs(dirs, parent_id)
41
- ids = find_in_db ::Taggata::Directory,
42
- parent_id,
43
- dirs.map { |d| ::File.basename d },
44
- :id
45
- ids.zip(dirs).each { |job| @jobs << job }
46
- end
47
-
48
- def save_missing(files, parent_id, klass)
49
- in_db = find_in_db(klass, parent_id, files, :name)
50
- to_save = (files - in_db).map do |basename|
51
- { :name => basename, :parent_id => parent_id }
52
- end
53
- klass.dataset.multi_insert(to_save)
54
- end
55
-
56
- def find_in_db(klass, parent_id, names, param)
57
- klass
58
- .where(:parent_id => parent_id)
59
- .where(:name => names)
60
- .map(param)
61
- end
62
-
63
- # Breadth first search traversal through the filesystem tree
64
- def process(dir)
65
- @jobs << [dir.id, dir.name]
66
- until @jobs.empty?
67
- do_job(*@jobs.shift)
68
- report
69
- end
70
- end
71
- end
72
- end
@@ -1,20 +0,0 @@
1
- module Taggata
2
- class Tag < Sequel::Model
3
- many_to_many :files,
4
- :left_id => :tag_id,
5
- :right_id => :file_id,
6
- :join_table => :file_tags
7
-
8
- set_schema do
9
- primary_key :id
10
- String :name, :unique => true
11
- end
12
-
13
- create_table unless table_exists?
14
-
15
- def self.files(query)
16
- tag = find(query)
17
- tag.nil? ? [] : tag.files
18
- end
19
- end
20
- end