Sha256: 615963f3e8ae4af5933bbc936dbce9ba7ecf2e84fae0234ccc8ed7a10df1668f

Contents?: true

Size: 988 Bytes

Versions: 1

Compression:

Stored size: 988 Bytes

Contents

class TagRingtail::TagNames
  include Enumerable

  # Create a new TagNames object, but clear out existing tags
  # and use the provided set instead.
  def self.new_with_names(taggable, names)
    tag_names = new(taggable)
    tag_names.clear
    names.each { |name| tag_names << name }
    tag_names
  end

  def initialize(taggable)
    @taggable = taggable
  end

  def to_a
    taggable.tags.collect &:name
  end

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

    taggable.tags << tag
  end

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

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

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

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

  def clear
    taggable.tags.clear
  end

  private

  attr_reader :taggable
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tag_ringtail-0.1.0 lib/tag_ringtail/tag_names.rb