Module: Mongoid::Tag::Meta::InstanceMethods

Defined in:
lib/mongoid_tag/meta.rb

Instance Method Summary collapse

Instance Method Details

#add_meta_tag(context, tag, meta) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/mongoid_tag/meta.rb', line 46

def add_meta_tag(context, tag, meta)
  old_tag = self.meta_tags.where(:name => tag, :context => context).first
  if old_tag
    old_tag.meta.merge!(meta)
  else
    self.meta_tags << MetaTag.new(:name => tag, :count => 1, :context => context, :meta => meta)
  end
end

#add_meta_tags(context, tags) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/mongoid_tag/meta.rb', line 55

def add_meta_tags(context, tags)
  tags.each do |tag|
    old_tag = self.meta_tags.where(:name => tag, :context => context).first
    if old_tag
      old_tag.count += 1
    else
      self.meta_tags << MetaTag.new(:name => tag, :count => 1, :context => context)
    end
  end
end

#get_meta_for(context) ⇒ Object



36
37
38
# File 'lib/mongoid_tag/meta.rb', line 36

def get_meta_for(context)
  meta_tags.where(:context => context).map{|tag| [tag.name, tag.meta.merge(:count => tag.count)]}
end

#get_weights_for(context) ⇒ Object



32
33
34
# File 'lib/mongoid_tag/meta.rb', line 32

def get_weights_for(context)
  meta_tags.where(:context => context).map{|tag| [tag.name, tag.count]}
end

#remove_meta_tags(context, tags) ⇒ Object



66
67
68
69
70
71
# File 'lib/mongoid_tag/meta.rb', line 66

def remove_meta_tags(context, tags)
  tags.each do |tag|
    old_tag = self.meta_tags.where(:name => tag, :context => context).first
    old_tag.count -= 1 if old_tag && old_tag.count > 0
  end
end

#update_meta_tags(context, added = nil, removed = nil) ⇒ Object



40
41
42
43
44
# File 'lib/mongoid_tag/meta.rb', line 40

def update_meta_tags(context, added=nil, removed=nil)
  add_meta_tags(context, added) if added
  remove_meta_tags(context, removed) if removed
  save
end