Class: TagListCollection

Inherits:
TagListProxy show all
Defined in:
lib/scoped_tags/tag_list_collection.rb

Instance Method Summary collapse

Methods inherited from TagListProxy

#===, #initialize, #inspect, #proxy_context, #proxy_owner, #proxy_respond_to?, #proxy_target, #reload, #respond_to?, #send, #target, #target=

Constructor Details

This class inherits a constructor from TagListProxy

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class TagListProxy

Instance Method Details

#<<(tag_names) ⇒ Object Also known as: push, concat



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/scoped_tags/tag_list_collection.rb', line 8

def <<(tag_names)
  tag_names = clean_tag_list(tag_names)
  current_list = self.to_a
  context_tags = self.proxy_owner.send(self.proxy_context)
  tag_names.each do |new_tag|
    unless current_list.include?(new_tag)
      context_tags << Tag.find_or_new_by_name_and_context(new_tag, self.proxy_context.to_s) 
    end
  end
  self
end

#delete(tag_names) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/scoped_tags/tag_list_collection.rb', line 24

def delete(tag_names)
  context_tags = self.proxy_owner.send(self.proxy_context)
  to_delete = []
  tag_names = clean_tag_list(tag_names)
  context_tags.each do |tag|
    to_delete << tag if tag_names.include?(tag.name)
  end
  context_tags.delete(to_delete)
  to_delete.collect(&:name)
end

#delete_at(index) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/scoped_tags/tag_list_collection.rb', line 35

def delete_at(index)
  context_tags = self.proxy_owner.send(self.proxy_context)
  return nil if 0 > index or index > context_tags.size
  tag_at_index = context_tags[index]
  context_tags.delete(tag_at_index)
  tag_at_index
end

#popObject



43
44
45
# File 'lib/scoped_tags/tag_list_collection.rb', line 43

def pop
  self.delete_at(self.size - 1)
end

#replace(tag_names) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/scoped_tags/tag_list_collection.rb', line 47

def replace(tag_names)
  new_uniq_list = clean_tag_list(tag_names).uniq
  new_tag_list = new_uniq_list.inject([]) do |list, tag_name|
    list << Tag.find_or_new_by_name_and_context(tag_name, self.proxy_context)
    list
  end
  context_tags = self.proxy_owner.send(self.proxy_context)
  context_tags.replace(new_tag_list)
  self
end

#to_sObject



59
60
61
# File 'lib/scoped_tags/tag_list_collection.rb', line 59

def to_s
  self.join("#{TagListCollection.delimiter} ")
end