Class: TagDangelo::TagNames

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(taggable) ⇒ TagNames

Returns a new instance of TagNames.



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

def initialize(taggable)
  @taggable = taggable
end

Class Method Details

.new_with_names(taggable, names) ⇒ Object



8
9
10
11
12
13
# File 'lib/tag_dangelo/tag_names.rb', line 8

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

#+(names) ⇒ Object



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

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

#-(names) ⇒ Object



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

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

#<<(name) ⇒ Object



27
28
29
30
31
32
# File 'lib/tag_dangelo/tag_names.rb', line 27

def <<(name)
  tag = TagDangelo::Tag.where(name: name).first ||
        TagDangelo::Tag.create(name: name)

  taggable.tags << tag
end

#clearObject



15
16
17
# File 'lib/tag_dangelo/tag_names.rb', line 15

def clear
  taggable.tags.clear
end

#delete(name) ⇒ Object



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

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

#each(&block) ⇒ Object



23
24
25
# File 'lib/tag_dangelo/tag_names.rb', line 23

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

#to_aObject



19
20
21
# File 'lib/tag_dangelo/tag_names.rb', line 19

def to_a
  taggable.tags.map(&:name)
end