Sha256: 6d98e17c2ef57174a59c84b68df0df4bbb7928dad865b42c47b37e6045d2cc02

Contents?: true

Size: 1.49 KB

Versions: 6

Compression:

Stored size: 1.49 KB

Contents

module Vedeu

  module Editor

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

      # @return [Vedeu::Editor::Line|Vedeu::Editor::Lines]
      # @see #initialize
      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 [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 [Vedeu::Editor::Line|Vedeu::Editor::Lines]
      def insert
        if index
          collection.insert(index, entity)

        else
          collection << entity

        end
      end

      protected

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

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

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

      private

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

        @index = 0    if @index < 0
        @index = size if @index > size
        @index
      end

    end # Insert

  end # Editor

end # Vedeu

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
vedeu-0.6.15 lib/vedeu/editor/insert.rb
vedeu-0.6.14 lib/vedeu/editor/insert.rb
vedeu-0.6.13 lib/vedeu/editor/insert.rb
vedeu-0.6.12 lib/vedeu/editor/insert.rb
vedeu-0.6.11 lib/vedeu/editor/insert.rb
vedeu-0.6.10 lib/vedeu/editor/insert.rb