Sha256: ef78b56ef6a2fdc56b444343706d737614f710ead5fab5f415ab1045b7ef2ede

Contents?: true

Size: 1.71 KB

Versions: 7

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module Vedeu

  module Cursors

    # Provides the mechanism to move a named cursor in a given
    # direction.
    #
    # @api private
    #
    class Move

      extend Forwardable

      def_delegators :cursor,
                     :visible?

      # @param (see #initialize)
      # @return (see #move)
      def self.move(name, direction, offset)
        new(name, direction, offset).move
      end

      # @param name [String|Symbol]
      # @param direction [Symbol]
      # @param offset [Fixnum]
      # @return [Vedeu::Cursors::Move]
      def initialize(name, direction, offset)
        @name      = name
        @direction = direction
        @offset    = offset
      end

      # @return [NilClass|Vedeu::Cursors::Cursor]
      def move
        return nil unless visible? && valid_direction?

        cursor.public_send(direction, offset)
        cursor.store { Vedeu.trigger(:_refresh_cursor_, name) }
        cursor
      end

      protected

      # @!attribute [r] name
      # @return [String|Symbol] The name of the cursor.
      attr_reader :name

      # @!attribute [r] direction
      # @return [Symbol] The direction to move the cursor.
      attr_reader :direction

      # @!attribute [r] offset
      # @return [Symbol] The number of characters to move the cursor
      #   in the given direction.
      attr_reader :offset

      private

      # @return [Vedeu::Cursors::Cursor]
      def cursor
        @_cursor ||= Vedeu.cursors.by_name(name)
      end

      # @return [Boolean]
      def valid_direction?
        [
          :move_down,
          :move_left,
          :move_right,
          :move_up,
        ].include?(direction)
      end

    end # Move

  end # Cursors

end # Vedeu

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
vedeu-0.8.15 lib/vedeu/cursors/move.rb
vedeu-0.8.14 lib/vedeu/cursors/move.rb
vedeu-0.8.13 lib/vedeu/cursors/move.rb
vedeu-0.8.12 lib/vedeu/cursors/move.rb
vedeu-0.8.10 lib/vedeu/cursors/move.rb
vedeu-0.8.9 lib/vedeu/cursors/move.rb
vedeu-0.8.8 lib/vedeu/cursors/move.rb