Class: TagBat::TagNames

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(taggable) ⇒ TagNames

Returns a new instance of TagNames.



17
18
19
# File 'lib/tag_bat/tag_names.rb', line 17

def initialize(taggable)
  @taggable = taggable
end

Instance Attribute Details

#taggableObject (readonly)

Returns the value of attribute taggable.



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

def taggable
  @taggable
end

Class Method Details

.new_with_names(taggable, names) ⇒ Object



6
7
8
9
10
11
# File 'lib/tag_bat/tag_names.rb', line 6

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



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

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

#-(array) ⇒ Object



39
40
41
42
# File 'lib/tag_bat/tag_names.rb', line 39

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

#<<(name) ⇒ Object



25
26
27
28
# File 'lib/tag_bat/tag_names.rb', line 25

def <<(name)
  tag = TagBat::Tag.where(name: name).first || TagBat::Tag.create(name: name)
  taggable.tags << tag
end

#clearObject



13
14
15
# File 'lib/tag_bat/tag_names.rb', line 13

def clear
  taggable.tags.clear
end

#delete(name) ⇒ Object



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

def delete(name)
  taggable.tags.delete TagBat::Tag.where(name: name).first
end

#each(&block) ⇒ Object



44
45
46
# File 'lib/tag_bat/tag_names.rb', line 44

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

#to_aObject



21
22
23
# File 'lib/tag_bat/tag_names.rb', line 21

def to_a 
  taggable.tags.collect &:name
end