Sha256: 61264a56025e626381b450a5f3d904bc39fd987652ffa2b9f897dc8dca90fd7c

Contents?: true

Size: 1.99 KB

Versions: 19

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module Vedeu

  module Editor

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

      include Vedeu::Common

      # @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?
        string?(collection)
      end

      # If true, we are dealing with a {Vedeu::Editor::Lines}
      # collection.
      #
      # @return [Boolean]
      def lines?
        array?(collection)
      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

19 entries across 19 versions & 1 rubygems

Version Path
vedeu-0.8.32 lib/vedeu/editor/delete.rb
vedeu-0.8.31 lib/vedeu/editor/delete.rb
vedeu-0.8.30 lib/vedeu/editor/delete.rb
vedeu-0.8.29 lib/vedeu/editor/delete.rb
vedeu-0.8.28 lib/vedeu/editor/delete.rb
vedeu-0.8.27 lib/vedeu/editor/delete.rb
vedeu-0.8.26 lib/vedeu/editor/delete.rb
vedeu-0.8.25 lib/vedeu/editor/delete.rb
vedeu-0.8.24 lib/vedeu/editor/delete.rb
vedeu-0.8.23 lib/vedeu/editor/delete.rb
vedeu-0.8.22 lib/vedeu/editor/delete.rb
vedeu-0.8.21 lib/vedeu/editor/delete.rb
vedeu-0.8.20 lib/vedeu/editor/delete.rb
vedeu-0.8.19 lib/vedeu/editor/delete.rb
vedeu-0.8.18 lib/vedeu/editor/delete.rb
vedeu-0.8.17 lib/vedeu/editor/delete.rb
vedeu-0.8.16 lib/vedeu/editor/delete.rb
vedeu-0.8.15 lib/vedeu/editor/delete.rb
vedeu-0.8.14 lib/vedeu/editor/delete.rb