Sha256: a78b8c89409a7f6477b7707248012cfde9de56e439cd525b153d3b20e85ad665

Contents?: true

Size: 1.74 KB

Versions: 50

Compression:

Stored size: 1.74 KB

Contents

module Alchemy

  # The Clipboard holds element ids and page ids
  #
  # It is stored inside the session.
  #
  # Each entry can has an action keyword that describes what to do after pasting the item.
  #
  # The keyword can be one of 'copy' or 'cut'
  class Clipboard

    attr_accessor :items

    def initialize
      @items = self.class.empty_clipboard
    end

    # Returns all items of the collection from category (+:elements+ or +:pages+)
    def all(item_type)
      @items.fetch(item_type.to_sym)
    end
    alias_method :[], :all

    # Returns the item from collection of category (+:elements+ or +:pages+)
    def get(item_type, item_id)
      all(item_type).detect { |item| item[:id].to_i == item_id.to_i }
    end

    # Returns true if the id is already in the collection of category (+:elements+ or +:pages+)
    def contains?(item_type, item_id)
      all(item_type).collect { |item| item[:id].to_i }.include?(item_id.to_i)
    end

    # Insert an item into the collection of category (+:elements+ or +:pages+)
    def push(item_type, item)
      all(item_type).push(normalized(item))
    end

    def replace(item_type, item)
      all(item_type).replace(item.is_a?(Array) ? item : [item])
    end
    alias_method :[]=, :replace

    def remove(item_type, item_id)
      all(item_type).delete_if { |item| item[:id].to_i == item_id.to_i }
    end

    def clear(item_type = nil)
      if item_type
        all(item_type).clear
      else
        @items = self.class.empty_clipboard
      end
    end

    def empty?
      @items == self.class.empty_clipboard
    end

  private

    def normalized(item)
      item[:id] = item[:id].to_i
      item
    end

  protected

    def self.empty_clipboard
      {:elements => [], :pages => []}
    end

  end
end

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
alchemy_cms-2.9.1 app/models/alchemy/clipboard.rb
alchemy_cms-2.9.0 app/models/alchemy/clipboard.rb
alchemy_cms-2.8.3 app/models/alchemy/clipboard.rb
alchemy_cms-2.7.5 app/models/alchemy/clipboard.rb
alchemy_cms-2.6.3 app/models/alchemy/clipboard.rb
alchemy_cms-3.0.0.rc7 app/models/alchemy/clipboard.rb
alchemy_cms-3.0.0.rc6 app/models/alchemy/clipboard.rb
alchemy_cms-2.7.4 app/models/alchemy/clipboard.rb
alchemy_cms-2.8.2 app/models/alchemy/clipboard.rb
alchemy_cms-2.8.1 app/models/alchemy/clipboard.rb
alchemy_cms-2.7.3 app/models/alchemy/clipboard.rb
alchemy_cms-3.0.0.rc5 app/models/alchemy/clipboard.rb
alchemy_cms-2.7.2 app/models/alchemy/clipboard.rb
alchemy_cms-2.7.1 app/models/alchemy/clipboard.rb
alchemy_cms-2.7.0 app/models/alchemy/clipboard.rb
alchemy_cms-2.6.2.1 app/models/alchemy/clipboard.rb
alchemy_cms-2.6.2 app/models/alchemy/clipboard.rb
alchemy_cms-2.6.1 app/models/alchemy/clipboard.rb
alchemy_cms-2.6.0 app/models/alchemy/clipboard.rb
alchemy_cms-2.6.0.rc5 app/models/alchemy/clipboard.rb