Class: Tag::Store

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

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStore

Returns a new instance of Store.



11
12
13
14
# File 'lib/tag.rb', line 11

def initialize
  @file = self.class.file
  @hash = File.exists?(@file) ? YAML.load_file(@file) : {}
end

Class Attribute Details

.fileObject

Returns the value of attribute file.



8
9
10
# File 'lib/tag.rb', line 8

def file
  @file
end

Instance Method Details

#delete_tags(*tags) ⇒ Object



47
48
49
50
# File 'lib/tag.rb', line 47

def delete_tags(*tags)
  tags.each {|tag| @hash.delete(tag) }
  save
end

#list(tag) ⇒ Object



32
33
34
35
# File 'lib/tag.rb', line 32

def list(tag)
  @hash[tag] ||= []
  @hash[tag]
end

#list_tagsObject



37
38
39
# File 'lib/tag.rb', line 37

def list_tags
  @hash.keys.sort
end

#remove_tag(item, tag) ⇒ Object



26
27
28
29
30
# File 'lib/tag.rb', line 26

def remove_tag(item, tag)
  @hash[tag] ||= []
  @hash[tag].delete item
  save
end

#saveObject



16
17
18
# File 'lib/tag.rb', line 16

def save
  File.open(@file, 'w') {|f| f.write(@hash.to_yaml) }
end

#tag(item, tag) ⇒ Object



20
21
22
23
24
# File 'lib/tag.rb', line 20

def tag(item, tag)
  @hash[tag] ||= []
  @hash[tag] << item
  save
end

#treeObject



41
42
43
44
45
# File 'lib/tag.rb', line 41

def tree
  list_tags.map {|tag|
    ["#{tag}", list(tag).map {|t| "    #{t}" }]
  }.flatten
end