lib/vedeu/cursors/cursor.rb in vedeu-0.6.14 vs lib/vedeu/cursors/cursor.rb in vedeu-0.6.15

- old
+ new

@@ -43,10 +43,16 @@ # @!attribute [w] y # @return [Fixnum] attr_writer :y + # @param attributes [Hash] See #initialize + # @return [Vedeu::Cursors::Cursor] + def self.store(attributes) + new(attributes).store + end + # Returns a new instance of Vedeu::Cursors::Cursor. # # @param attributes [Hash] # @option attributes name [String] The name of the interface # this cursor belongs to. @@ -58,71 +64,74 @@ # cursor. # @option attributes x [Fixnum] The terminal x coordinate for # the cursor. # @option attributes y [Fixnum] The terminal y coordinate for # the cursor. - # # @return [Vedeu::Cursors::Cursor] def initialize(attributes = {}) @attributes = defaults.merge!(attributes) @attributes.each do |key, value| instance_variable_set("@#{key}", value) end end + # An object is equal when its values are the same. + # + # @param other [Vedeu::Cursors::Cursor] + # @return [Boolean] + def eql?(other) + self.class == other.class && name == other.name + end + alias_method :==, :eql? + # Moves the cursor down by one row. # # @return [Vedeu::Cursors::Cursor] def move_down @oy += 1 - @attributes = new_attributes(coordinate.y_position, x, oy, ox) - - Vedeu::Cursors::Cursor.new(@attributes).store + Vedeu::Cursors::Cursor.store( + new_attributes(coordinate.y_position, x, oy, ox)) end # Moves the cursor left by one column. # # @return [Vedeu::Cursors::Cursor] def move_left @ox -= 1 - @attributes = new_attributes(y, coordinate.x_position, oy, ox) - - Vedeu::Cursors::Cursor.new(@attributes).store + Vedeu::Cursors::Cursor.store( + new_attributes(y, coordinate.x_position, oy, ox)) end # Moves the cursor to the top left of the named interface. # # @return [Vedeu::Cursors::Cursor] def move_origin - @attributes = attributes.merge!(x: bx, y: by, ox: 0, oy: 0) - - Vedeu::Cursors::Cursor.new(@attributes).store + Vedeu::Cursors::Cursor.store( + attributes.merge!(x: bx, y: by, ox: 0, oy: 0)) end # Moves the cursor right by one column. # # @return [Vedeu::Cursors::Cursor] def move_right @ox += 1 - @attributes = new_attributes(y, coordinate.x_position, oy, ox) - - Vedeu::Cursors::Cursor.new(@attributes).store + Vedeu::Cursors::Cursor.store( + new_attributes(y, coordinate.x_position, oy, ox)) end # Moves the cursor up by one row. # # @return [Vedeu::Cursors::Cursor] def move_up @oy -= 1 - @attributes = new_attributes(coordinate.y_position, x, oy, ox) - - Vedeu::Cursors::Cursor.new(@attributes).store + Vedeu::Cursors::Cursor.store( + new_attributes(coordinate.y_position, x, oy, ox)) end # Renders the cursor. # # @return [Array<Vedeu::Models::Escape>] @@ -134,15 +143,14 @@ # # @param new_oy [Fixnum] The row/line position. # @param new_ox [Fixnum] The column/character position. # @return [Vedeu::Cursors::Cursor] def reposition(new_oy, new_ox) - @attributes = new_attributes(coordinate.y_position, - coordinate.x_position, - new_oy, - new_ox) + @oy = new_oy + @ox = new_ox - Vedeu::Cursors::Cursor.new(@attributes).store + Vedeu::Cursors::Cursor.store( + new_attributes(coordinate.y_position, coordinate.x_position, oy, ox)) end # Returns an escape sequence to position the cursor and set its # visibility. When passed a block, will position the cursor, # yield and return the original position.