Class: TagOcelot::TagNames

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(taggable) ⇒ TagNames

Returns a new instance of TagNames.



5
6
7
# File 'lib/tag_ocelot/tag_names.rb', line 5

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.



23
24
25
26
27
28
# File 'lib/tag_ocelot/tag_names.rb', line 23

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



42
43
44
45
# File 'lib/tag_ocelot/tag_names.rb', line 42

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

#-(array) ⇒ Object



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

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

#<<(name) ⇒ Object



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

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

  taggable.tags << tag
end

#clearObject



30
31
32
# File 'lib/tag_ocelot/tag_names.rb', line 30

def clear
  taggable.tags.clear
end

#delete(name) ⇒ Object



38
39
40
# File 'lib/tag_ocelot/tag_names.rb', line 38

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

#each(&block) ⇒ Object



34
35
36
# File 'lib/tag_ocelot/tag_names.rb', line 34

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

#to_aObject



9
10
11
# File 'lib/tag_ocelot/tag_names.rb', line 9

def to_a
  taggable.tags.collect &:name
end