Sha256: ef32c8fa2c62e55f77f3867c52b691efc35871cd8f7191ccce7f373bfc8d6705

Contents?: true

Size: 1.8 KB

Versions: 31

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module Vedeu

  module Editor

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

      include Vedeu::Common

      # @param (see #initialize)
      # @return (see #insert)
      def self.into(collection, entity, index = nil, size = 0)
        new(collection, entity, index, size).insert
      end

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

      # @return [String|Vedeu::Editor::Line|Vedeu::Editor::Lines]
      def insert
        return collection.insert(position, entity) if index?

        collection << entity
      end

      protected

      # @!attribute [r] entity
      # @return [String]
      attr_reader :entity

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

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

      private

      # @!attribute [r] collection
      # @return [String|Vedeu::Editor::Line|Vedeu::Editor::Lines]
      def collection
        if string?(@collection)
          @collection.dup

        else
          @collection

        end
      end

      # @return [Boolean]
      def index?
        numeric?(index)
      end

      # @return [Fixnum]
      def position
        if index < 0
          0

        elsif index > size
          size

        else
          index

        end
      end

    end # Insert

  end # Editor

end # Vedeu

Version data entries

31 entries across 31 versions & 1 rubygems

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