Class: CellularMap::Map

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cellular_map/map.rb

Overview

Map

Maps are limitless, only non-empty (nil content) cells are actually stored.

(see README for examples)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMap

:nodoc:



16
17
18
# File 'lib/cellular_map/map.rb', line 16

def initialize # :nodoc:
  @store = {}
end

Instance Attribute Details

#storeObject (readonly)

Store of actually filled cells.



14
15
16
# File 'lib/cellular_map/map.rb', line 14

def store
  @store
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



49
50
51
52
# File 'lib/cellular_map/map.rb', line 49

def ==(other) # :nodoc:
  @store.length == other.store.length &&
    @store.collect { |k, v| other[*k].content == v }.uniq == [true]
end

#[](x, y) ⇒ Object

Accessing a cell or a zone.



21
22
23
24
25
26
27
# File 'lib/cellular_map/map.rb', line 21

def [](x, y)
  if x.respond_to?(:to_i) && y.respond_to?(:to_i)
    Cell.new(x, y, self)
  else
    Zone.new(x, y, self)
  end
end

#[]=(x, y, content) ⇒ Object

Putting new content in a cell.



30
31
32
33
34
35
36
# File 'lib/cellular_map/map.rb', line 30

def []=(x, y, content)
  if content.nil?
    @store.delete([x, y]) && nil
  else
    @store[[x, y]] = content
  end
end

#eachObject

Iterating over each filled cell.



39
40
41
42
# File 'lib/cellular_map/map.rb', line 39

def each # :yields: cell
  @store.keys.each { |k| yield Cell.new(*(k + [self])) }
  self
end

#empty!Object

Empties the map filled cells.



45
46
47
# File 'lib/cellular_map/map.rb', line 45

def empty!
  @store = {}
end

#initialize_copy(other) ⇒ Object

:nodoc:



54
55
56
57
# File 'lib/cellular_map/map.rb', line 54

def initialize_copy(other) # :nodoc:
  @store = {}
  other.each { |c| self[c.x, c.y] = c.content }
end