Sha256: a58b47aaac4a0d10c63a6b65c68de90a7fbde2f3e2ed6ebfca068ae243269bda

Contents?: true

Size: 845 Bytes

Versions: 2

Compression:

Stored size: 845 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::TagName.normalise(name) }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gutentag-0.5.0 lib/gutentag/persistence.rb
gutentag-0.4.0 lib/gutentag/persistence.rb