Class: TagPanda::TagNames

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tag_panda/tag_names.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(taggable) ⇒ TagNames



4
5
6
# File 'lib/tag_panda/tag_names.rb', line 4

def initialize(taggable)
  @taggable = taggable
end

Class Method Details

.new_with_names(taggable, names) ⇒ Object

Create a new TagNames object, but clear out existing tags and use the provided set instead.



26
27
28
29
30
31
# File 'lib/tag_panda/tag_names.rb', line 26

def self.new_with_names(taggable, names)
  tag_names = new(taggable)
  tag_names.clear
  names.each { |name| tag_names << name }
  tag_names
end

Instance Method Details

#+(array) ⇒ Object



41
42
43
44
# File 'lib/tag_panda/tag_names.rb', line 41

def +(array)
  array.each { |name| self.<< name }
  self
end

#-(array) ⇒ Object



46
47
48
49
# File 'lib/tag_panda/tag_names.rb', line 46

def -(array)
  array.each { |name| self.delete name }
  self
end

#<<(name) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/tag_panda/tag_names.rb', line 12

def <<(name)
  # find an existing tab, or create a new one
  tag = TagPanda::Tag.where(:name => name).first ||
        TagPanda::Tag.create(:name => name)

  taggable.tags << tag
end

#clearObject



33
34
35
# File 'lib/tag_panda/tag_names.rb', line 33

def clear
  taggable.tags.clear
end

#delete(name) ⇒ Object



20
21
22
# File 'lib/tag_panda/tag_names.rb', line 20

def delete(name)
  taggable.tags.delete TagPanda::Tag.where(:name => name).first
end

#each(&block) ⇒ Object



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

def each(&block)
  to_a.each &block
end

#to_aObject



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

def to_a
  taggable.tags.collect &:name
end