cellular_map 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,8 +13,7 @@ module CellularMap
13
13
  attr_reader :map
14
14
 
15
15
  def initialize(x, y, map) # :nodoc:
16
- @x = x.respond_to?(:to_i) ? (x.to_i..x.to_i) : x
17
- @y = y.respond_to?(:to_i) ? (y.to_i..y.to_i) : y
16
+ @x, @y = rangeize(x, y)
18
17
  @map = map
19
18
  end
20
19
 
@@ -35,14 +34,12 @@ module CellularMap
35
34
 
36
35
  # Access to a cell inside the zone.
37
36
  def [](x, y)
38
- if x.respond_to?(:to_i) && y.respond_to?(:to_i)
39
- @map[@x.min + x.to_i, @y.min + y.to_i]
40
- else
41
- x = x.to_i..x.to_i if x.respond_to?(:to_i)
42
- y = y.to_i..y.to_i if y.respond_to?(:to_i)
43
- @map[(@x.min + x.min)..(@x.min + x.max),
44
- (@y.min + y.min)..(@y.min + y.max)]
45
- end
37
+ @map[*relative(x, y)]
38
+ end
39
+
40
+ # Modification of a cell's content inside the zone.
41
+ def []=(x, y, content)
42
+ @map[*relative(x, y)] = content
46
43
  end
47
44
 
48
45
  def ==(other) # :nodoc:
@@ -62,5 +59,23 @@ module CellularMap
62
59
  def to_a
63
60
  @y.collect { |y| @x.collect { |x| Cell.new(x, y, @map) } }
64
61
  end
62
+
63
+ protected
64
+
65
+ # Converts coordinates to coordinates relative to inside the map.
66
+ def relative(x, y)
67
+ if x.respond_to?(:to_i) && y.respond_to?(:to_i)
68
+ [@x.min + x.to_i, @y.min + y.to_i]
69
+ else
70
+ x, y = rangeize(x, y)
71
+ [ (@x.min + x.min)..(@x.min + x.max),
72
+ (@y.min + y.min)..(@y.min + y.max) ]
73
+ end
74
+ end
75
+
76
+ # Converts given coordinates to ranges if necessary.
77
+ def rangeize(x, y)
78
+ [x, y].collect { |i| i.respond_to?(:to_i) ? (i.to_i..i.to_i) : i }
79
+ end
65
80
  end
66
81
  end
data/test/test_zone.rb CHANGED
@@ -20,6 +20,12 @@ class TestZone < Test::Unit::TestCase
20
20
 
21
21
  should("allow access to other zones relatively to boundaries") {
22
22
  assert_equal @map[-2..5, 6..11], @zone[-1..6, 9..14] }
23
+
24
+ should("allow to fill cells relatively to its boundaries") {
25
+ @zone[2, 1] = 'thingy'
26
+ assert_equal 'thingy', @zone[2, 1].content
27
+ assert_equal 'thingy', @map[1, -2].content
28
+ }
23
29
  }
24
30
 
25
31
  context("with a few filled cells") {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cellular_map
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Belleville