Class: CaTissue::Location

Inherits:
Object
  • Object
show all
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

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



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

#containerObject

Returns the value of attribute container.



10
11
12
# File 'lib/catissue/helpers/location.rb', line 10

def container
  @container
end

#coordinateObject

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.

Returns:

  • (Boolean)

    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

#columnObject

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

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

#rowObject

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

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

#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



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.

Returns:

  • self

Raises:

  • (IndexError)

    if the next slot exceeds the container capacity



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_sObject 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