Class: CaTissue::Container
- Inherits:
-
Object
- Object
- CaTissue::Container
- 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
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Returns -1, 0, or 1 if self is contained in, contains or the same as the other Container, resp.
-
#[](column, row) ⇒ Object
The occupant at the given zero-based row and column, or nil if none.
-
#add(storable, *coordinate) ⇒ Object
(also: #<<)
Moves the given Storable from its current Position, if any, to this Container at the optional coordinate.
- #bounds ⇒ Object
-
#can_hold?(storable) ⇒ Boolean
True if this Container or a subcontainer in the hierarchy can hold the given storable.
-
#can_hold_child?(storable) ⇒ Boolean
True if this Container is not full and the #container_type can hold the storable as a child.
-
#capacity ⇒ Object
Lazy-initializes this Container’s capacity to a copy of the #storable_type capacity.
-
#columns ⇒ Object
The number of columns in this Container.
-
#completely_full? ⇒ Boolean
Whether this Container and every subcontainer in the hierarchy are full.
-
#container_type ⇒ Object
This Container’s ContainerType.
- #copy(*attributes) ⇒ Object
-
#holds?(storable) ⇒ Boolean
Whether this container or a subcontainer in the hierarchy holds the given object.
-
#include?(item) ⇒ Boolean
Returns whether this Container holds the given item or this Container holds a subcontainer which holds the item.
-
#occupants ⇒ Object
(also: #contents)
The occupants in this Container’s positions.
-
#parent ⇒ Object
This Container’s parent container.
-
#position_class ⇒ Object
Returns the ContainerPosition class which this Container can occupy in its role as a Storable.
-
#rows ⇒ Object
The number of rows in this Container.
-
#specimens ⇒ Object
The Specimen occupants.
-
#storable_type ⇒ Object
Returns the ContainerType which constrains a Container in its roles as a Storable occupant rather than a Storable holder.
-
#subcontainers ⇒ Object
The Container occupants.
-
#subcontainers_in_hierarchy ⇒ Object
The Containers in this StorageContainer hierarchy.
-
#uniquify ⇒ Object
Makes this Container and all of its subcontainers unique.
Methods included from Storable
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.
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.
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.
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 |
#bounds ⇒ Object
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.
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.
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 |
#capacity ⇒ Object
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 |
#columns ⇒ Object
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.
125 126 127 |
# File 'lib/catissue/domain/container.rb', line 125 def completely_full? full? and subcontainers.all? { |ctr| ctr.completely_full? } end |
#container_type ⇒ Object
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.
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.
88 89 90 |
# File 'lib/catissue/domain/container.rb', line 88 def include?(item) occupants.detect { |occ| occ == item or occ.include?(item) } end |
#occupants ⇒ Object Also known as: contents
Returns the occupants in this Container’s positions.
80 81 82 |
# File 'lib/catissue/domain/container.rb', line 80 def occupants all_occupied_positions.wrap { |pos| pos.occupant } end |
#parent ⇒ Object
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_class ⇒ Object
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 |
#rows ⇒ Object
Returns the number of rows in this Container.
64 65 66 |
# File 'lib/catissue/domain/container.rb', line 64 def rows capacity.rows end |
#specimens ⇒ Object
Returns the Specimen occupants.
93 94 95 |
# File 'lib/catissue/domain/container.rb', line 93 def specimens occupants.filter { |occ| Specimen === occ } end |
#storable_type ⇒ Object
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 |
#subcontainers ⇒ Object
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_hierarchy ⇒ Object
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 |
#uniquify ⇒ Object
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 |