Sha256: ec141b64094bfd344c6e17db85267fff9756679026de611bb739333662689b76

Contents?: true

Size: 842 Bytes

Versions: 5

Compression:

Stored size: 842 Bytes

Contents

class Gutentag::Persistence

  attr_writer :tagger, :normaliser

  def initialize(taggable)
    @taggable = taggable
    @existing = normalised taggable.tags.collect(&:name)
    @changes  = normalised taggable.tag_names
  end

  def persist
    remove_old
    add_new

    taggable.reset_tag_names
  end

  private

  attr_reader :taggable, :existing, :changes

  def add_new
    (changes - existing).each do |name|
      taggable.tags << tagger.find_or_create(name)
    end
  end

  def normalised(names)
    names.collect { |name| normaliser.call(name) }.uniq
  end

  def remove_old
    (existing - changes).each do |name|
      taggable.tags.delete tagger.find_by_name(name)
    end
  end

  def tagger
    @tagger ||= Gutentag::Tag
  end

  def normaliser
    @normaliser ||= Proc.new { |name| Gutentag.normaliser.call(name) }
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gutentag-0.9.0 lib/gutentag/persistence.rb
gutentag-0.8.0 lib/gutentag/persistence.rb
gutentag-0.7.0 lib/gutentag/persistence.rb
gutentag-0.6.0 lib/gutentag/persistence.rb
gutentag-0.5.1 lib/gutentag/persistence.rb