Class: CaTissue::Location

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/catissue/util/location.rb

Overview

A Location is a non-Resource utility class which represents a Container row and column.

Location does not capture the occupant; therefore, changing a location coordinate value alone does not change the storage assignment of an occupant.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = nil) ⇒ Object

Returns the new Location.

Parameters:

  • params ({Symbol => Object}) (defaults to: nil)

    the location fields

  • [Integer] (Hash)

    a customizable set of options

  • [Coordinate, (Hash)

    a customizable set of options



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/catissue/util/location.rb', line 21

def initialize(params=nil)
  Options.validate(params, INIT_OPTS)
  @container = Options.get(:in, params)
  coord = Options.get(:at, params, Coordinate.new)
  # turn an :at Array value into a Coordinate
  if Array === coord and not Coordinate === coord then
    coord = Coordinate.new(*coord) 
  end
  validate_coordinate(coord)
  @coordinate = coord
end

Instance Attribute Details

#containerObject

Returns the value of attribute container.



12
13
14
# File 'lib/catissue/util/location.rb', line 12

def container
  @container
end

#coordinateObject

Returns the value of attribute coordinate.



12
13
14
# File 'lib/catissue/util/location.rb', line 12

def coordinate
  @coordinate
end

Instance Method Details

#<=>(other) ⇒ Object

Parameters:

  • other (Location)

    the position to compare

Raises:

  • (ArgumentError)

    if other is not an Location



61
62
63
64
65
66
67
# File 'lib/catissue/util/location.rb', line 61

def <=>(other)
  raise ArgumentError.new("Can't compare #{qp} to #{other.qp}") unless @container and Location === other
  unless container == other.container then
    raise ArgumentError.new("Can't compare #{qp} in container #{container} to #{other.qp} in container #{other.container}")
  end
  @coordinate <=> other.coordinate
end

#==(other) ⇒ Boolean

Returns whether other is a Location and has the same content as this Location.

Returns:

  • (Boolean)

    whether other is a Location and has the same content as this Location



54
55
56
# File 'lib/catissue/util/location.rb', line 54

def ==(other)
  super rescue false
end

#columnObject

Returns this location’s zero-based first dimension value.

Returns:

  • this location’s zero-based first dimension value



34
35
36
# File 'lib/catissue/util/location.rb', line 34

def column
  @coordinate.x
end

#column=(value) ⇒ Object

Sets this location’s column to the given zero-based value.



39
40
41
# File 'lib/catissue/util/location.rb', line 39

def column=(value)
  @coordinate.x = value
end

#rowObject

Returns this location’s zero-based second dimension value.

Returns:

  • this location’s zero-based second dimension value



44
45
46
# File 'lib/catissue/util/location.rb', line 44

def row
  @coordinate.y
end

#row=(value) ⇒ Object

Sets this location’s row to the given zero-based value.



49
50
51
# File 'lib/catissue/util/location.rb', line 49

def row=(value)
  @coordinate.y = value
end

#succLocation?

Returns a new Location at the next slot in this Location’s #container, or nil if there are no more locations.

Returns:

  • (Location, nil)

    a new Location at the next slot in this Location’s #container, or nil if there are no more locations



71
72
73
# File 'lib/catissue/util/location.rb', line 71

def succ
  self.class.new(:in => container, :at => @coordinate).succ! rescue nil
end

#succ!Object

Sets this Location to the next slot in this Location’s #container.

Returns:

  • self

Raises:

  • (IndexError)

    if the next slot exceeds the container capacity



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/catissue/util/location.rb', line 79

def succ!
  raise IndexError.new("Location #{qp} container not set") unless @container
  raise IndexError.new("Location #{qp} coordinate not set") unless @coordinate
  c = column.succ % @container.capacity.columns
  r = c.zero? ? row.succ : row
  unless r < container.capacity.rows then
    raise IndexError.new("Location #{[c, r].qp} exceeds #{@container} container capacity #{container.capacity.bounds}")
  end
  @coordinate.x = c
  @coordinate.y = r
  self
end

#to_sObject Also known as: inspect, qp



92
93
94
95
96
97
# File 'lib/catissue/util/location.rb', line 92

def to_s
  ctr_s = @container.print_class_and_id if @container
  coord_s = @coordinate.to_s if @coordinate
  content_s = "{#{ctr_s}#{coord_s}}" if ctr_s or coord_s
  "#{print_class_and_id}{#{content_s}"
end