Class: CaTissue::Location
- Inherits:
-
Object
- Object
- CaTissue::Location
- 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
-
#container ⇒ Object
Returns the value of attribute container.
-
#coordinate ⇒ Object
Returns the value of attribute coordinate.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#==(other) ⇒ Boolean
Whether other is a Location and has the same content as this Location.
-
#column ⇒ Object
This location’s zero-based first dimension value.
-
#column=(value) ⇒ Object
Sets this location’s column to the given zero-based value.
-
#initialize(params = nil) ⇒ Object
constructor
The new Location.
-
#row ⇒ Object
This location’s zero-based second dimension value.
-
#row=(value) ⇒ Object
Sets this location’s row to the given zero-based value.
-
#succ ⇒ Location?
A new Location at the next slot in this Location’s #container, or nil if there are no more locations.
-
#succ! ⇒ Object
Sets this Location to the next slot in this Location’s #container.
- #to_s ⇒ Object (also: #inspect, #qp)
Constructor Details
#initialize(params = nil) ⇒ Object
Returns the new Location.
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
#container ⇒ Object
Returns the value of attribute container.
12 13 14 |
# File 'lib/catissue/util/location.rb', line 12 def container @container end |
#coordinate ⇒ Object
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
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.
54 55 56 |
# File 'lib/catissue/util/location.rb', line 54 def ==(other) super rescue false end |
#column ⇒ Object
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 |
#row ⇒ Object
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 |
#succ ⇒ Location?
Returns 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.
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_s ⇒ Object 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 |