Class: Taggata::Persistent::Directory

Inherits:
Abstract
  • Object
show all
Includes:
WithParent
Defined in:
lib/taggata/persistent/directory.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WithParent

#parent

Methods inherited from Abstract

bulk_insert, destroy, #destroy, find, find_one, find_or_create, #save

Constructor Details

#initialize(db, name, parent_id = nil, id = nil) ⇒ Directory

Returns a new instance of Directory.



9
10
11
12
13
14
# File 'lib/taggata/persistent/directory.rb', line 9

def initialize(db, name, parent_id = nil, id = nil)
  @db = db
  @name = name
  @parent_id = parent_id
  @id = id
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



7
8
9
# File 'lib/taggata/persistent/directory.rb', line 7

def db
  @db
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/taggata/persistent/directory.rb', line 7

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/taggata/persistent/directory.rb', line 7

def name
  @name
end

#parent_idObject (readonly)

Returns the value of attribute parent_id.



7
8
9
# File 'lib/taggata/persistent/directory.rb', line 7

def parent_id
  @parent_id
end

Class Method Details

.new_from_hash(db, hash) ⇒ Object



28
29
30
31
32
33
# File 'lib/taggata/persistent/directory.rb', line 28

def self.new_from_hash(db, hash)
  self.new(db,
           hash[:name],
           hash[:parent_id],
           hash[:id])
end

.tableObject



16
17
18
# File 'lib/taggata/persistent/directory.rb', line 16

def self.table
  :taggata_directories
end

Instance Method Details

#directoriesObject



41
42
43
# File 'lib/taggata/persistent/directory.rb', line 41

def directories
  @child_directories ||= db.find_child_directories(self)
end

#entriesObject



53
54
55
# File 'lib/taggata/persistent/directory.rb', line 53

def entries
  directories + files
end

#filesObject



45
46
47
# File 'lib/taggata/persistent/directory.rb', line 45

def files
  @child_files ||= db.find_child_files(self)
end

#invalidate_cacheObject



49
50
51
# File 'lib/taggata/persistent/directory.rb', line 49

def invalidate_cache
  @child_directories = @child_files = nil
end

#pathObject

Get full path of this directory



73
74
75
76
77
# File 'lib/taggata/persistent/directory.rb', line 73

def path
  parents = [self]
  parents << parents.last.parent while parents.last.parent
  ::File.join(parents.reverse.map(&:name))
end

#scanObject

Scan children of this directory



58
59
60
61
62
# File 'lib/taggata/persistent/directory.rb', line 58

def scan
  scanner = ::Taggata::FilesystemScanner.new db
  scanner.process(self)
  validate
end

#show(indent = 0) ⇒ Object



35
36
37
38
39
# File 'lib/taggata/persistent/directory.rb', line 35

def show(indent = 0)
  indent.times { print "  " }
  puts "+ #{self.name}"
  entries.each { |e| e.show(indent + 1) }
end

#to_hashObject



20
21
22
23
24
25
26
# File 'lib/taggata/persistent/directory.rb', line 20

def to_hash
  {
    :name => name,
    :parent_id => parent_id,
    :id => id
  }
end

#validateObject



64
65
66
67
68
# File 'lib/taggata/persistent/directory.rb', line 64

def validate
  missing = ::Taggata::Persistent::Tag.find_or_create(:name => MISSING_TAG_NAME)
  files.reject { |f| ::File.exist? f.path }.each { |f| f.add_tag missing }
  directories.each(&:validate)
end