Class: CaTissue::Container
- Inherits:
-
Object
- Object
- CaTissue::Container
- Includes:
- Storable, Jinx::Unique
- Defined in:
- lib/catissue/domain/container.rb,
lib/catissue/migration/unique.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
-
#[](column, row) ⇒ Storable?
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
-
the capacity bounds.
-
#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 ⇒ Integer
The number of columns in this Container.
-
#completely_full? ⇒ Boolean
Whether this Container and every subcontainer in the hierarchy are full.
-
#container_type ⇒ CaTissue::ContainerType
This Container’s meta-type.
-
#holds?(storable) ⇒ Boolean
Whether this container or a subcontainer in the hierarchy holds the given object.
-
#include?(obj) ⇒ Boolean
Whether this Container holds the given item or this Container holds a subcontainer which holds the item.
-
#occupants ⇒ <Storable>
(also: #contents)
The occupants in this Container’s positions.
-
#parent ⇒ Container?
This Container’s parent container, if any.
-
#position_class ⇒ Class
a Storable.
-
#rows ⇒ Integer
The number of rows in this Container.
-
#specimens ⇒ <Specimen>
The direct Specimen occupants.
- #storable_type ⇒ CaTissue::ContainerType
-
#subcontainers ⇒ <Container>
The direct Container occupants.
-
#subcontainers_in_hierarchy ⇒ <Container>
The Containers in this StorageContainer hierarchy.
-
#uniquify ⇒ Object
Makes this Container and all of its subcontainers unique.
Methods included from Storable
Instance Method Details
#[](column, row) ⇒ Storable?
Returns the occupant at the given zero-based row and column, or nil if none.
131 132 133 134 135 136 137 138 |
# File 'lib/catissue/domain/container.rb', line 131 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.
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/catissue/domain/container.rb', line 148 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
Returns [] the capacity bounds.
60 61 62 |
# File 'lib/catissue/domain/container.rb', line 60 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.
116 117 118 |
# File 'lib/catissue/domain/container.rb', line 116 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.
121 122 123 |
# File 'lib/catissue/domain/container.rb', line 121 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 ⇒ Integer
Returns the number of columns in this Container.
70 71 72 |
# File 'lib/catissue/domain/container.rb', line 70 def columns capacity.columns end |
#completely_full? ⇒ Boolean
Returns whether this Container and every subcontainer in the hierarchy are full.
126 127 128 |
# File 'lib/catissue/domain/container.rb', line 126 def completely_full? full? and subcontainers.all? { |ctr| ctr.completely_full? } end |
#container_type ⇒ CaTissue::ContainerType
Returns this Container’s meta-type.
44 45 46 |
# File 'lib/catissue/domain/container.rb', line 44 def container_type raise NotImplementedError.new("Container subclass does not implement the container_type method") end |
#holds?(storable) ⇒ Boolean
Returns whether this container or a subcontainer in the hierarchy holds the given object.
109 110 111 |
# File 'lib/catissue/domain/container.rb', line 109 def holds?(storable) contents.include?(storable) or subcontainers.any? { |ctr| ctr.holds?(storable) } end |
#include?(obj) ⇒ Boolean
Returns whether this Container holds the given item or this Container holds a subcontainer which holds the item.
89 90 91 |
# File 'lib/catissue/domain/container.rb', line 89 def include?(obj) occupants.any? { |occ| occ == obj } or subcontainers.any? { |ctr| ctr.include?(obj) } end |
#occupants ⇒ <Storable> 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 ⇒ Container?
Returns this Container’s parent container, if any.
75 76 77 |
# File 'lib/catissue/domain/container.rb', line 75 def parent position and position.parent end |
#position_class ⇒ Class
a Storable.
50 51 52 |
# File 'lib/catissue/domain/container.rb', line 50 def position_class CaTissue::ContainerPosition end |
#rows ⇒ Integer
Returns the number of rows in this Container.
65 66 67 |
# File 'lib/catissue/domain/container.rb', line 65 def rows capacity.rows end |
#specimens ⇒ <Specimen>
Returns the direct Specimen occupants.
94 95 96 |
# File 'lib/catissue/domain/container.rb', line 94 def specimens occupants.filter { |occ| Specimen === occ } end |
#storable_type ⇒ CaTissue::ContainerType
Returns the meta-type which constrains this Container in its role as a Storable occupant rather than a Storable holder. #storable_type aliases the #container_type defined by every Container subclass.
36 37 38 39 |
# File 'lib/catissue/domain/container.rb', line 36 def storable_type # can't alias because container_type is defined by subclasses container_type end |
#subcontainers ⇒ <Container>
Returns the direct Container occupants.
99 100 101 |
# File 'lib/catissue/domain/container.rb', line 99 def subcontainers occupants.filter { |occ| Container === occ } end |
#subcontainers_in_hierarchy ⇒ <Container>
Returns the Containers in this StorageContainer hierarchy.
104 105 106 |
# File 'lib/catissue/domain/container.rb', line 104 def subcontainers_in_hierarchy @ctr_enum ||= SUBCTR_VISITOR.to_enum(self) end |
#uniquify ⇒ Object
Makes this Container and all of its subcontainers unique.
26 27 28 29 |
# File 'lib/catissue/migration/unique.rb', line 26 def uniquify super subcontainers.each { |ctr| ctr.uniquify } end |