Class: CaTissue::Container

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

Storable::Position

Instance Method Summary collapse

Methods included from Storable

#container, #move_to

Instance Method Details

#[](column, row) ⇒ Storable?

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

Returns:

  • (Storable, nil)

    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.

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



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

#boundsObject

Returns [] the capacity bounds.

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.

Returns:

  • (Boolean)

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

See Also:



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.

Returns:

  • (Boolean)

    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

#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

#columnsInteger

Returns the number of columns in this Container.

Returns:

  • (Integer)

    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.

Returns:

  • (Boolean)

    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_typeCaTissue::ContainerType

Returns this Container’s meta-type.

Returns:

Raises:

  • (NotImplementedError)

    if this container’s Container subclass does not override the #container_type method



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.

Returns:

  • (Boolean)

    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.

Parameters:

Returns:

  • (Boolean)

    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.

Returns:

  • (<Storable>)

    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

#parentContainer?

Returns this Container’s parent container, if any.

Returns:

  • (Container, nil)

    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_classClass

Returns:



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

def position_class
  CaTissue::ContainerPosition
end

#rowsInteger

Returns the number of rows in this Container.

Returns:

  • (Integer)

    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.

Returns:

  • (<Specimen>)

    the direct Specimen occupants



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

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

#storable_typeCaTissue::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.

Returns:

  • (<Container>)

    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.

Returns:

  • (<Container>)

    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

#uniquifyObject

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