Sha256: fa1e8b003df8d0837632739e19280ef4cb48ec643fd3bde3dcb1171f4f3e297e

Contents?: true

Size: 1.43 KB

Versions: 9

Compression:

Stored size: 1.43 KB

Contents

module Vedeu

  module Editor

    # Fetches an item from a collection.
    #
    # @api private
    #
    class Item

      # @param collection [Vedeu::Editor::Line|Vedeu::Editor::Lines]
      # @param index [Fixnum]
      # @return [String|Vedeu::Editor::Line]
      def self.by_index(collection, index = nil)
        new(collection, index).by_index
      end

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

      # @return [String|Vedeu::Editor::Line]
      def by_index
        return nil unless size > 0

        if index.nil? || index > size
          collection[-1]

        elsif index <= 0
          collection[0]

        elsif index > 0 && index <= size
          collection[index]

        end
      end

      protected

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

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

      private

      # Returns the size of the collection or 0.
      #
      # @return [Fixnum]
      def size
        if collection
          collection.size

        else
          0

        end
      end

    end # Item

  end # Editor

end # Vedeu

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
vedeu-0.6.60 lib/vedeu/editor/item.rb
vedeu-0.6.59 lib/vedeu/editor/item.rb
vedeu-0.6.58 lib/vedeu/editor/item.rb
vedeu-0.6.57 lib/vedeu/editor/item.rb
vedeu-0.6.56 lib/vedeu/editor/item.rb
vedeu-0.6.54 lib/vedeu/editor/item.rb
vedeu-0.6.53 lib/vedeu/editor/item.rb
vedeu-0.6.52 lib/vedeu/editor/item.rb
vedeu-0.6.51 lib/vedeu/editor/item.rb