Class: CaTissue::Location
- Inherits:
-
Object
- Object
- CaTissue::Location
- Defined in:
- lib/catissue/helpers/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) ⇒ 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.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/catissue/helpers/location.rb', line 19 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.
10 11 12 |
# File 'lib/catissue/helpers/location.rb', line 10 def container @container end |
#coordinate ⇒ Object
Returns the value of attribute coordinate.
10 11 12 |
# File 'lib/catissue/helpers/location.rb', line 10 def coordinate @coordinate end |
Instance Method Details
#==(other) ⇒ Boolean
Returns whether other is a Location and has the same content as this Location.
52 53 54 |
# File 'lib/catissue/helpers/location.rb', line 52 def ==(other) container == other.container and coordinate == other.coordinate end |
#column ⇒ Object
Returns this location’s zero-based first dimension value.
32 33 34 |
# File 'lib/catissue/helpers/location.rb', line 32 def column @coordinate.x end |
#column=(value) ⇒ Object
Sets this location’s column to the given zero-based value.
37 38 39 |
# File 'lib/catissue/helpers/location.rb', line 37 def column=(value) @coordinate.x = value end |
#row ⇒ Object
Returns this location’s zero-based second dimension value.
42 43 44 |
# File 'lib/catissue/helpers/location.rb', line 42 def row @coordinate.y end |
#row=(value) ⇒ Object
Sets this location’s row to the given zero-based value.
47 48 49 |
# File 'lib/catissue/helpers/location.rb', line 47 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.
58 59 60 |
# File 'lib/catissue/helpers/location.rb', line 58 def succ self.class.new(:in => container, :at => @coordinate.dup).succ! rescue nil end |
#succ! ⇒ Object
Sets this Location to the next slot in this Location’s #container.
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/catissue/helpers/location.rb', line 66 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
79 80 81 82 83 84 |
# File 'lib/catissue/helpers/location.rb', line 79 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 |