Sha256: 6dfedb16fb0c75c166076b00ed07090d221645b117560a3bc70e53c955cd9575

Contents?: true

Size: 1.48 KB

Versions: 9

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module Vedeu

  module Editor

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

      # @param (see #initialize)
      # @return (see #by_index)
      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 collection

        if index.nil? || index > collection.size
          last_item

        elsif index > 0 && index <= collection.size
          nth_item

        else
          first_item

        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

      # @return [String|Vedeu::Editor::Line]
      def first_item
        collection[0]
      end

      # @return [String|Vedeu::Editor::Line]
      def last_item
        collection[-1]
      end

      # @return [String|Vedeu::Editor::Line]
      def nth_item
        collection[index]
      end

    end # Item

  end # Editor

end # Vedeu

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
vedeu-0.8.21 lib/vedeu/editor/item.rb
vedeu-0.8.20 lib/vedeu/editor/item.rb
vedeu-0.8.19 lib/vedeu/editor/item.rb
vedeu-0.8.18 lib/vedeu/editor/item.rb
vedeu-0.8.17 lib/vedeu/editor/item.rb
vedeu-0.8.16 lib/vedeu/editor/item.rb
vedeu-0.8.15 lib/vedeu/editor/item.rb
vedeu-0.8.14 lib/vedeu/editor/item.rb
vedeu-0.8.13 lib/vedeu/editor/item.rb