Class: Tag::Store
- Inherits:
-
Object
- Object
- Tag::Store
- Defined in:
- lib/tag.rb
Class Attribute Summary collapse
-
.file ⇒ Object
Returns the value of attribute file.
Instance Method Summary collapse
- #delete_tags(*tags) ⇒ Object
-
#initialize ⇒ Store
constructor
A new instance of Store.
- #list(tag) ⇒ Object
- #list_tags ⇒ Object
- #remove_tag(item, tag) ⇒ Object
- #save ⇒ Object
- #tag(item, tag) ⇒ Object
- #tree ⇒ Object
Constructor Details
#initialize ⇒ Store
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
.file ⇒ Object
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 (*) .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_tags ⇒ Object
37 38 39 |
# File 'lib/tag.rb', line 37 def @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 |
#save ⇒ Object
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 |
#tree ⇒ Object
41 42 43 44 45 |
# File 'lib/tag.rb', line 41 def tree .map {|tag| ["#{tag}", list(tag).map {|t| " #{t}" }] }.flatten end |