Class: CellularMap::Zone

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

Overview

Map zones

Zones are generated on the fly to provide a wrapper to keep a map’s zone under surveillance.

(see README for examples)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, map) ⇒ Zone

:nodoc:



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

def initialize(x, y, map) # :nodoc:
  @x, @y = rangeize(x, y)
  @map = map
end

Instance Attribute Details

#mapObject (readonly)

Map the zone’s part of



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

def map
  @map
end

#xObject (readonly)

Zone’s boundaries



12
13
14
# File 'lib/cellular_map/zone.rb', line 12

def x
  @x
end

#yObject (readonly)

Zone’s boundaries



12
13
14
# File 'lib/cellular_map/zone.rb', line 12

def y
  @y
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



46
47
48
# File 'lib/cellular_map/zone.rb', line 46

def ==(other) # :nodoc:
  ! self.detect { |c| ! (other.map[c.x, c.y].content == c.content) }
end

#[](x, y) ⇒ Object

Access to a cell inside the zone.



37
38
39
# File 'lib/cellular_map/zone.rb', line 37

def [](x, y)
  @map[*relative(x, y)]
end

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

Modification of a cell’s content inside the zone.



42
43
44
# File 'lib/cellular_map/zone.rb', line 42

def []=(x, y, content)
  @map[*relative(x, y)] = content
end

#eachObject

Iterating over each cells inside the zone.

(first to last lines, first to last columns for each line)



53
54
55
56
# File 'lib/cellular_map/zone.rb', line 53

def each # :yields: cell
  @y.each { |y| @x.each { |x| yield Cell.new(x, y, @map) } }
  self
end

#heightObject

Zone’s height.



32
33
34
# File 'lib/cellular_map/zone.rb', line 32

def height
  @y.count
end

#lengthObject

Zone’s length.



22
23
24
# File 'lib/cellular_map/zone.rb', line 22

def length
  width * height
end

#to_aObject

Making an array of arrays of all cells inside the zone.

see each



61
62
63
# File 'lib/cellular_map/zone.rb', line 61

def to_a
  @y.collect { |y| @x.collect { |x| Cell.new(x, y, @map) } }
end

#widthObject

Zone’s width.



27
28
29
# File 'lib/cellular_map/zone.rb', line 27

def width
  @x.count
end