Sha256: a7bbce953953edec11343ec72e18b7f97782a3fc47adde2f8ccdf5a03503a510

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 KB

Contents

module Vedeu

  module Editor

    # Manipulate the lines of an Vedeu::Editor::Document.
    #
    class Delete

      # @param (see #initialize)
      # @return [Vedeu::Editor::Line|Vedeu::Editor::Lines]
      def self.from(collection, index = nil, size = 0)
        new(collection, index, size).delete
      end

      # Returns a new instance of Vedeu::Editor::Delete.
      #
      # @param collection [Vedeu::Editor::Line|Vedeu::Editor::Lines]
      # @param index [Fixnum]
      # @param size [Fixnum]
      # @return [Vedeu::Editor::Delete]
      def initialize(collection, index = nil, size = 0)
        @collection = collection
        @index      = index
        @size       = size
      end

      # @return [Vedeu::Editor::Line|Vedeu::Editor::Lines]
      def delete
        return collection if collection.empty? || negative_index?
        return collection.dup.tap { |c| c.slice!(index) } if index
        return collection.dup.tap(&:pop) if lines?
        return collection.chop if line?
      end

      protected

      # @!attribute [r] collection
      # @return [Vedeu::Editor::Line|Vedeu::Editor::Lines]
      attr_reader :collection

      # @!attribute [r] size
      # @return [Fixnum]
      attr_reader :size

      private

      # @return [Fixnum]
      def index
        return nil unless @index

        @index = size - 1 if @index > size
        @index
      end

      # If true, we are dealing with a {Vedeu::Editor::Line} object.
      #
      # @return [Boolean]
      def line?
        collection.is_a?(String)
      end

      # If true, we are dealing with a {Vedeu::Editor::Lines}
      # collection.
      #
      # @return [Boolean]
      def lines?
        collection.is_a?(Array)
      end

      # Returns a boolean indicating whether the index was given or
      # negative.
      #
      # @return [Boolean]
      def negative_index?
        index && index < 0
      end

    end # Delete

  end # Editor

end # Vedeu

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vedeu-0.6.50 lib/vedeu/editor/delete.rb
vedeu-0.6.49 lib/vedeu/editor/delete.rb
vedeu-0.6.48 lib/vedeu/editor/delete.rb