Class: Taggata::Persistent::File

Inherits:
Abstract
  • Object
show all
Includes:
WithParent
Defined in:
lib/taggata/persistent/file.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, find, find_one, find_or_create, #invalidate_cache, #save

Constructor Details

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

Returns a new instance of File.



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

def initialize(db, name, parent_id, id = nil)
  @name = name
  @db = db
  @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/file.rb', line 7

def db
  @db
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#parent_idObject (readonly)

Returns the value of attribute parent_id.



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

def parent_id
  @parent_id
end

Class Method Details

.new_from_hash(db, hash) ⇒ Object



66
67
68
69
70
71
# File 'lib/taggata/persistent/file.rb', line 66

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

.tableObject



73
74
75
# File 'lib/taggata/persistent/file.rb', line 73

def self.table
  :taggata_files
end

Instance Method Details

#add_tags(*to_add) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/taggata/persistent/file.rb', line 24

def add_tags(*to_add)
  current_tag_ids = tags.map(&:id)
  missing_tag_ids = to_add.map(&:id) - current_tag_ids
  return if missing_tag_ids.empty?
  options = missing_tag_ids.map { |tag_id| { :file_id => id, :tag_id => tag_id } }
  db.bulk_insert(FileTag, options)
end

#add_tags_by_name(*tag_names) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/taggata/persistent/file.rb', line 16

def add_tags_by_name(*tag_names)
  tag_records = nil
  db.transaction do
    tag_records = tag_names.map { |tag_name| ::Taggata::Persistent::Tag.find_or_create(db, :name => tag_name) }
  end
  add_tags(*tag_records)
end

#destroyObject



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

def destroy
  file_tags.each(&:destroy)
  super
end

#file_tagsObject



50
51
52
# File 'lib/taggata/persistent/file.rb', line 50

def file_tags
  ::Taggata::Persistent::FileTag.find(db, :file_id => id)
end

#pathObject



82
83
84
# File 'lib/taggata/persistent/file.rb', line 82

def path
  ::File.join(parent.path, name)
end

#remove_tags(*to_remove) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/taggata/persistent/file.rb', line 32

def remove_tags(*to_remove)
  return if to_remove.empty?
  current_tag_ids = tags.map(&:id)
  to_remove_ids = current_tag_ids & to_remove.map(&:id)
  options = to_remove_ids.map { |tag_id| { :file_id => id, :tag_id => tag_id } }
  db.destroy(FileTag, options)
end

#remove_tags_by_name(*tag_names) ⇒ Object



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

def remove_tags_by_name(*tag_names)
  tags = tag_names.map { |tag_name| Tag.find_or_create db, :name => tag_name }
  remove_tags(*tags)
end

#show(indent = 0) ⇒ Object



77
78
79
80
# File 'lib/taggata/persistent/file.rb', line 77

def show(indent = 0)
  indent.times { print "  " }
  puts "- #{self.name}"
end

#tagsObject



54
55
56
# File 'lib/taggata/persistent/file.rb', line 54

def tags
  file_tags.map(&:tag)
end

#to_hashObject



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

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