Module: CaTissue::Position

Includes:
Comparable
Included in:
SpecimenArrayContent
Defined in:
lib/catissue/util/position.rb

Overview

The Position mix-in encapsulates the location of an occupant in a holder. Classes which include Position are required to implement the column, row, occupant and holder methods. The occupant must be a Storable. The holder must be a Container.

Instance Method Summary collapse

Instance Method Details

#<=>(other) ⇒ Object

Parameters:

  • other (Position)

    the position to compare

Raises:

  • (ArgumentError)

    if other is not an instance of this Position’s concrete class



15
16
17
18
# File 'lib/catissue/util/position.rb', line 15

def <=>(other)
  raise ArgumentError.new("Can't compare #{qp} to #{other.qp}") unless self.class === other
  equal?(other) ? 0 : location <=> other.location
end

#==(other) ⇒ Boolean

Returns whether other is an instance of this position’s class with the same occupant and equal location.

Returns:

  • (Boolean)

    whether other is an instance of this position’s class with the same occupant and equal location

See Also:



24
25
26
# File 'lib/catissue/util/position.rb', line 24

def ==(other)
  self.class === other and occupant == other.occupant and location == other.location
end

#coordinateCoordinate

Returns the read-only coordinate with this AbstractPosition’s #row and #column.

Returns:

  • (Coordinate)

    the read-only coordinate with this AbstractPosition’s #row and #column.



29
30
31
# File 'lib/catissue/util/position.rb', line 29

def coordinate
  location.coordinate
end

#locationLocation

Returns the location of this Position.

Returns:

  • (Location)

    the location of this Position.



34
35
36
37
38
39
40
41
# File 'lib/catissue/util/position.rb', line 34

def location
  @location ||= Location.new
  # always ensure that the location is consistent with the Java state
  @location.holder = holder
  @location.row = row
  @location.column = column
  @location
end

#location=(value) ⇒ Object

Parameters:

  • the (Location)

    location value to set



44
45
46
47
48
49
# File 'lib/catissue/util/position.rb', line 44

def location=(value)
  @location = value || Location.new
  self.holder = @location.holder
  self.row = @location.row
  self.column = @location.column
end

#to_a(Integer, Integer)

Returns this Position’s zero-based (#column, #row) tuple.

Returns:

  • ((Integer, Integer))

    this Position’s zero-based (#column, #row) tuple.



57
58
59
# File 'lib/catissue/util/position.rb', line 57

def to_a
  [column, row]
end

#unspecified?Boolean

Returns whether either the column or the row is nil.

Returns:

  • (Boolean)

    whether either the column or the row is nil



52
53
54
# File 'lib/catissue/util/position.rb', line 52

def unspecified?
  column.nil? or row.nil?
end

#validateObject

Raises:

  • (ValidationError)

    if the holder cannot hold the occupant type



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/catissue/util/position.rb', line 62

def validate
  super
  logger.debug { "Validating that #{holder} can hold #{occupant}..." }
  curr_occ = holder[column, row]
  if curr_occ.nil? then
    unless holder.can_hold_child?(occupant) then
      reason = holder.full? ? "it is full" : "the occupant type is not among the supported types #{holder.container_type.child_types.qp}"
      raise ValidationError.new("#{holder} cannot hold #{occupant} since #{reason}")
    end
  elsif curr_occ != occupant
    raise ValidationError.new("#{holder} cannot hold #{occupant} since the location #{[colum, row]} is already occupied by #{curr_occ}")
  end
end