Class: CaTissue::Container

Inherits:
Object
  • Object
show all
Includes:
CaRuby::Resource::Unique, Resource, Storable
Defined in:
lib/catissue/domain/container.rb,
lib/catissue/domain/uniquify.rb

Overview

The caTissue Container domain class wrapper. Each Container subclass is required to implement the #container_type method.

Constant Summary

Constants included from Storable

Storable::Position

Instance Method Summary collapse

Methods included from Storable

#container, #move_to

Methods included from Resource

#database, included, #tolerant_match?

Methods included from Annotatable

#annotation_proxy, #create_proxy, #method_missing

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class CaTissue::Annotatable

Instance Method Details

#<=>(other) ⇒ Object

Returns -1, 0, or 1 if self is contained in, contains or the same as the other Container, resp.

Raises:

  • (TypeError)


131
132
133
134
135
136
# File 'lib/catissue/domain/container.rb', line 131

def <=>(other)
  raise TypeError.new("Can't compare #{qp} to #{other}") unless StorageContainer === self
  return 0 if equal?(other) or (name and name == other.name)
  return 1 if subcontainers.detect { |child| child >= other if StorageContainer === child }
  -1 if other > self
end

#[](column, row) ⇒ Object

Returns the occupant at the given zero-based row and column, or nil if none.

Returns:

  • the occupant at the given zero-based row and column, or nil if none



139
140
141
142
143
144
145
146
# File 'lib/catissue/domain/container.rb', line 139

def [](column, row)
  return if column.nil? or row.nil?
  all_occupied_positions.detect_value do |pos|
    return if row < pos.row
    next unless row == pos.row
    pos.occupant if pos.column == column
  end
end

#add(storable, *coordinate) ⇒ Object Also known as: <<

Moves the given Storable from its current Position, if any, to this Container at the optional coordinate. The default coordinate is the first available slot within this Container. The storable Storable position is updated to reflect the new location. Returns self.

Parameters:

  • storable (Storable)

    the item to add

  • coordinate (CaRuby::Coordinate, <Integer>)

    the x-y coordinate to place the item

Raises:

  • (IndexError)

    if this Container is full

  • (IndexError)

    if the row and column are given but exceed the Container bounds



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/catissue/domain/container.rb', line 156

def add(storable, *coordinate)
  validate_type(storable)
  loc = create_location(coordinate)
  pos = storable.position || storable.position_class.new
  pos.location = loc
  pos.occupant = storable
  pos.holder = self
  logger.debug { "Added #{storable.qp} to #{qp} at #{loc.coordinate}." }
  update_full_flag
  self
end

#boundsObject



59
60
61
# File 'lib/catissue/domain/container.rb', line 59

def bounds
  capacity.bounds if capacity
end

#can_hold?(storable) ⇒ Boolean

Returns true if this Container or a subcontainer in the hierarchy can hold the given storable.

Returns:

  • (Boolean)

    true if this Container or a subcontainer in the hierarchy can hold the given storable

See Also:



115
116
117
# File 'lib/catissue/domain/container.rb', line 115

def can_hold?(storable)
  can_hold_child?(storable) or subcontainers.detect { |ctr| ctr.can_hold?(storable) }
end

#can_hold_child?(storable) ⇒ Boolean

Returns true if this Container is not full and the #container_type can hold the storable as a child.

Returns:

  • (Boolean)

    true if this Container is not full and the #container_type can hold the storable as a child



120
121
122
# File 'lib/catissue/domain/container.rb', line 120

def can_hold_child?(storable)
  not full? and container_type.can_hold_child?(storable)
end

#capacityObject

Lazy-initializes this Container’s capacity to a copy of the #storable_type capacity.



55
56
57
# File 'lib/catissue/domain/container.rb', line 55

def capacity
  getCapacity or copy_container_type_capacity
end

#columnsObject

Returns the number of columns in this Container.

Returns:

  • the number of columns in this Container



69
70
71
# File 'lib/catissue/domain/container.rb', line 69

def columns
  capacity.columns
end

#completely_full?Boolean

Returns whether this Container and every subcontainer in the hierarchy are full.

Returns:

  • (Boolean)

    whether this Container and every subcontainer in the hierarchy are full



125
126
127
# File 'lib/catissue/domain/container.rb', line 125

def completely_full?
  full? and subcontainers.all? { |ctr| ctr.completely_full? }
end

#container_typeObject

Returns this Container’s ContainerType.

Returns:

  • this Container’s ContainerType



38
39
40
# File 'lib/catissue/domain/container.rb', line 38

def container_type
  if self.class < Container then raise NotImplementedError.new("Container subclass does not implement the container_type method") end
end

#copy(*attributes) ⇒ Object



42
43
44
45
46
# File 'lib/catissue/domain/container.rb', line 42

def copy(*attributes)
  ctr = super
  ctr.container_type = self.container_type if attributes.empty?
  ctr
end

#holds?(storable) ⇒ Boolean

Returns whether this container or a subcontainer in the hierarchy holds the given object.

Returns:

  • (Boolean)

    whether this container or a subcontainer in the hierarchy holds the given object



108
109
110
# File 'lib/catissue/domain/container.rb', line 108

def holds?(storable)
  contents.include?(storable) or subcontainers.any? { |ctr| ctr.holds?(storable) }
end

#include?(item) ⇒ Boolean

Returns whether this Container holds the given item or this Container holds a subcontainer which holds the item.

Returns:

  • (Boolean)


88
89
90
# File 'lib/catissue/domain/container.rb', line 88

def include?(item)
  occupants.detect { |occ| occ == item or occ.include?(item) }
end

#occupantsObject Also known as: contents

Returns the occupants in this Container’s positions.

Returns:

  • the occupants in this Container’s positions.

See Also:



80
81
82
# File 'lib/catissue/domain/container.rb', line 80

def occupants
  all_occupied_positions.wrap { |pos| pos.occupant }
end

#parentObject

Returns this Container’s parent container.

Returns:

  • this Container’s parent container



74
75
76
# File 'lib/catissue/domain/container.rb', line 74

def parent
  position and position.parent
end

#position_classObject

Returns the ContainerPosition class which this Container can occupy in its role as a Storable.



50
51
52
# File 'lib/catissue/domain/container.rb', line 50

def position_class
  CaTissue::ContainerPosition
end

#rowsObject

Returns the number of rows in this Container.

Returns:

  • the number of rows in this Container



64
65
66
# File 'lib/catissue/domain/container.rb', line 64

def rows
  capacity.rows
end

#specimensObject

Returns the Specimen occupants.

Returns:

  • the Specimen occupants



93
94
95
# File 'lib/catissue/domain/container.rb', line 93

def specimens
  occupants.filter { |occ| Specimen === occ }
end

#storable_typeObject

Returns the ContainerType which constrains a Container in its roles as a Storable occupant rather than a Storable holder. #storable_type aliases the container_type defined by every Container subclass.



32
33
34
35
# File 'lib/catissue/domain/container.rb', line 32

def storable_type
  # can't alias because container_type is defined by subclasses
  container_type
end

#subcontainersObject

Returns the Container occupants.

Returns:

  • the Container occupants



98
99
100
# File 'lib/catissue/domain/container.rb', line 98

def subcontainers
  occupants.filter { |occ| Container === occ }
end

#subcontainers_in_hierarchyObject

Returns the Containers in this StorageContainer hierarchy.

Returns:

  • the Containers in this StorageContainer hierarchy



103
104
105
# File 'lib/catissue/domain/container.rb', line 103

def subcontainers_in_hierarchy
  @ctr_enum ||= SUBCTR_VISITOR.to_enum(self)
end

#uniquifyObject

Makes this Container and all of its subcontainers unique.



18
19
20
21
# File 'lib/catissue/domain/uniquify.rb', line 18

def uniquify
  super
  subcontainers.each { |ctr| ctr.uniquify }
end