Class: Sashite::Cell::Coordinate

Inherits:
Object
  • Object
show all
Defined in:
lib/sashite/cell/coordinate.rb

Overview

Represents a parsed CELL coordinate with up to 3 dimensions.

A Coordinate holds 0-indexed integer values for each dimension and provides conversion to/from CELL string format.

Examples:

Creating a coordinate

coord = Sashite::Cell::Coordinate.new(4, 3)
coord.indices    # => [4, 3]
coord.dimensions # => 2
coord.to_s       # => "e4"

3D coordinate

coord = Sashite::Cell::Coordinate.new(0, 0, 0)
coord.to_s # => "a1A"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*indices) ⇒ Coordinate

Creates a Coordinate from 1 to 3 indices.

Examples:

Sashite::Cell::Coordinate.new(4, 3)    # 2D coordinate
Sashite::Cell::Coordinate.new(0, 0, 0) # 3D coordinate

Parameters:

  • indices (Array<Integer>)

    0-indexed coordinate values (0-255 each)

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sashite/cell/coordinate.rb', line 40

def initialize(*indices)
  if indices.empty?
    raise Errors::Argument, Errors::Argument::Messages::NO_INDICES
  end

  if indices.size > Constants::MAX_DIMENSIONS
    raise Errors::Argument, Errors::Argument::Messages::TOO_MANY_DIMENSIONS
  end

  indices.each do |index|
    unless index.is_a?(::Integer) && index >= 0 && index <= Constants::MAX_INDEX_VALUE
      raise Errors::Argument, Errors::Argument::Messages::INDEX_OUT_OF_RANGE
    end
  end

  @indices = indices.freeze
end

Instance Attribute Details

#indicesArray<Integer> (readonly)

Returns the coordinate indices as a frozen array.

Examples:

Sashite::Cell::Coordinate.new(4, 3).indices # => [4, 3]

Returns:

  • (Array<Integer>)

    0-indexed coordinate values



30
31
32
# File 'lib/sashite/cell/coordinate.rb', line 30

def indices
  @indices
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Checks equality with another Coordinate.

Examples:

Sashite::Cell::Coordinate.new(4, 3) == Sashite::Cell::Coordinate.new(4, 3) # => true

Parameters:

  • other (Object)

    object to compare

Returns:

  • (Boolean)

    true if equal, false otherwise



85
86
87
# File 'lib/sashite/cell/coordinate.rb', line 85

def ==(other)
  other.is_a?(Coordinate) && @indices == other.indices
end

#dimensionsInteger

Returns the number of dimensions (1, 2, or 3).

Examples:

Sashite::Cell::Coordinate.new(4, 3).dimensions # => 2

Returns:

  • (Integer)

    dimension count



64
65
66
# File 'lib/sashite/cell/coordinate.rb', line 64

def dimensions
  @indices.size
end

#hashInteger

Returns hash code for use in Hash keys.

Examples:

coord = Sashite::Cell::Coordinate.new(4, 3)
hash = { coord => "value" }

Returns:

  • (Integer)

    hash code



98
99
100
# File 'lib/sashite/cell/coordinate.rb', line 98

def hash
  @indices.hash
end

#inspectString

Returns a human-readable representation.

Examples:

Sashite::Cell::Coordinate.new(4, 3).inspect # => "#<Sashite::Cell::Coordinate e4>"

Returns:

  • (String)

    inspection string



108
109
110
# File 'lib/sashite/cell/coordinate.rb', line 108

def inspect
  "#<#{self.class} #{self}>"
end

#to_sString

Returns the CELL string representation.

Examples:

Sashite::Cell::Coordinate.new(4, 3).to_s # => "e4"

Returns:

  • (String)

    CELL coordinate string



74
75
76
# File 'lib/sashite/cell/coordinate.rb', line 74

def to_s
  Formatter.indices_to_string(@indices)
end