Sha256: 1ac6e7a52e9adc7d5b150bc9f35eab38c9cf917a781d823b56a55f74bdfc8159

Contents?: true

Size: 1.41 KB

Versions: 6

Compression:

Stored size: 1.41 KB

Contents

module Vedeu

  # Validates that the provided coordinates are within the terminal and
  # interface's geometry (with or without a border).
  #
  class PositionValidator

    extend Forwardable

    def_delegators :border,
                   :bx,
                   :bxn,
                   :by,
                   :byn

    # @!attribute [rw] x
    # @return [Fixnum]
    attr_accessor :x

    # @!attribute [rw] x
    # @return [Fixnum]
    attr_accessor :y

    # @param (see #initialize)
    def self.validate(name, x, y)
      new(name, x, y).validate
    end

    # Returns a new instance of Vedeu::PositionValidator.
    #
    # @param name [String]
    # @param y [Fixnum] The row/line coordinate.
    # @param x [Fixnum] The column/character coordinate.
    # @return [Vedeu::PositionValidator]
    def initialize(name, x, y)
      @name = name
      @x    = x
      @y    = y
    end

    # Ensures the coordinates provided are within the terminal, interface and
    # when applicable, bordered interface area.
    #
    # @return [PositionValidator]
    def validate
      @x = bx  if x < bx
      @x = bxn if x > bxn
      @y = by  if y < by
      @y = byn if y > byn

      self
    end

    protected

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

    private

    # @see Vedeu::Borders#by_name
    def border
      @border ||= Vedeu.borders.by_name(name)
    end

  end # PositionValidator

end # Vedeu

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
vedeu-0.5.3 lib/vedeu/geometry/position_validator.rb
vedeu-0.5.2 lib/vedeu/geometry/position_validator.rb
vedeu-0.5.1 lib/vedeu/geometry/position_validator.rb
vedeu-0.5.0 lib/vedeu/geometry/position_validator.rb
vedeu-0.4.65 lib/vedeu/geometry/position_validator.rb
vedeu-0.4.64 lib/vedeu/geometry/position_validator.rb