Sha256: d8b0ec293163d0aa19b3816f908dc826a6b99133e08a0bf4f55e8eea61f6301b

Contents?: true

Size: 828 Bytes

Versions: 1

Compression:

Stored size: 828 Bytes

Contents

class Gutentag::TagNames
  include Enumerable

  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 +(array)
    array.each { |name| self.<< name }
    self
  end

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

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

    taggable.tags << tag
  end

  def clear
    taggable.tags.clear
  end

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

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

  private

  attr_reader :taggable
end

Version data entries

1 entries across 1 versions & 1 rubygems

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