Class: SimpleCataloger::DirectoryVisitor
- Inherits:
-
Object
- Object
- SimpleCataloger::DirectoryVisitor
- Defined in:
- lib/dircat/cat_on_sqlite/directory_visitor.rb,
lib/simple_cataloger_dm/core/directory_visitor.rb
Overview
Find directory without subdirectories
Instance Method Summary collapse
- #enter_node(pathname) ⇒ Object
- #exit_node(pathname) ⇒ Object
-
#initialize(catalog_root) ⇒ DirectoryVisitor
constructor
A new instance of DirectoryVisitor.
-
#visit_leaf(pathname) ⇒ Object
called when visit leaf node.
Constructor Details
#initialize(catalog_root) ⇒ DirectoryVisitor
Returns a new instance of DirectoryVisitor.
8 9 10 11 12 13 |
# File 'lib/dircat/cat_on_sqlite/directory_visitor.rb', line 8 def initialize(catalog_root) @stack = [] @files = [] @nr = 0 @catalog_root = catalog_root end |
Instance Method Details
#enter_node(pathname) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/dircat/cat_on_sqlite/directory_visitor.rb', line 15 def enter_node(pathname) # each directory is associated to a number, # so when exit_tree_node is called and the number @nr is not increased # the directory doesn't have subdirectories @nr += 1 dirname = File.basename(pathname) = Tag.(dirname) = .concat(@stack.last.).uniq unless @stack.empty? info = OpenStruct.new(:nr => @nr, :pathname => pathname, :tags => , :name => Tag.extract_name(dirname)) @files = [] @stack.push(info) end |
#exit_node(pathname) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 |
# File 'lib/dircat/cat_on_sqlite/directory_visitor.rb', line 30 def exit_node(pathname) info = @stack.pop if info.nr == @nr # this directory doesn't have subdirectories name = info.name tag_names = info. puts name # pp tags # every name must be unique, two films cannot have the some name if Item.find_by_name(name) raise "item #{name} is not unique path #{pathname}" end item = Item.create( :name => name, :added_at => File.lstat(pathname).ctime, :path => pathname, :path_from_catalog_root => pathname[File.dirname(@catalog_root).length..-1] ) # # Associate item and tags # unknown = Category.find_or_create_by_name("unknown") tag_names.each do |tag_name| tag = Tag.find_by_name(tag_name) unless tag tag = Tag.match_category(tag_name) unless tag tag = Tag.find_by_name_and_category_id(tag_name, unknown.id) unless tag tag = Tag.create(:name => tag_name, :category => unknown) end end end Tagging.find_or_create_by_item_id_and_tag_id(item.id, tag.id) end # # Searches images # @files.each do |pathname| next unless pathname.match /(jpg|jpeg)$/ image = Image.find_or_create_by_path_and_path_from_catalog_root( pathname, pathname[File.dirname(@catalog_root).length..-1] ) item.images << image filename = File.basename(pathname) tag_names = Tag.(filename) tag_names.each do |tag_name| tag = Tag.first(:name => tag_name) if tag tag.images << image tag.save else puts "WARNING: tag '#{tag_name}' not found" end end end item.save end end |
#visit_leaf(pathname) ⇒ Object
called when visit leaf node
102 103 104 |
# File 'lib/dircat/cat_on_sqlite/directory_visitor.rb', line 102 def visit_leaf(pathname) @files << pathname end |