square_graph 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -6,13 +6,24 @@ This tool is an easier way of working with 2d arrays
6
6
 
7
7
  == How to Install
8
8
 
9
- gem install squareGraph
9
+ You can download directly from rubygems.org with the simple command (and it'll install too!):
10
+
11
+ $ gem install square_graph
12
+
13
+ Or you can compile the project yourself (options to install inside rake):
14
+
15
+ $ git clone git@github.com:D4L/squareGraph.git
16
+ ... blah blah pull stuff ...
17
+ $ rake build_gem
18
+
19
+ == License
20
+
21
+ Released under the MIT License. See the {LICENSE}[https://github.com/D4L/squareGraph/blob/master/LICENSE] file for further details.
10
22
 
11
23
  == Authors
12
24
 
13
25
  Austin.L <b>~D4L</b>
14
26
 
15
-
16
-
27
+
17
28
  ---
18
29
  noli umquam oblivisci: cogito ergo sum
data/Rakefile CHANGED
@@ -4,3 +4,19 @@ task :default => :spec
4
4
 
5
5
  desc "Run tests"
6
6
  RSpec::Core::RakeTask.new(:spec)
7
+
8
+ desc "Builds gem"
9
+ task :build_gem do
10
+ puts %s{gem build square_graph.gemspec}
11
+ output = `gem build square_graph.gemspec`
12
+ puts output
13
+ print %s{Do you want to install the gem (y|n)? }
14
+ while !%w{y n}.include?(inputs = STDIN.gets[0])
15
+ print %s{Please use either y or n: }
16
+ end
17
+ if inputs == 'y'
18
+ sh %s{gem install }.to_s + output.split.last
19
+ else
20
+ p %s{Alright, peace out.}
21
+ end
22
+ end
data/lib/square_graph.rb CHANGED
@@ -21,16 +21,19 @@ class SquareGraph
21
21
  raise NoMethodError if @sized == false
22
22
  test_fixnum(length, width)
23
23
  return nil if test_range([1, length], [1, width])
24
+ if !@sg.empty?
25
+ return nil if test_range([@sg.keys.max_by {|x| x[0]} [0], length], [@sg.keys.max_by {|y| y[1]} [1], width])
26
+ end
24
27
  @length, @width = length, width
25
28
  end
26
29
 
27
- def fill(*args)
28
- x, y = args[0], args[1]
30
+ def fill(x, y, o = true)
29
31
  test_fixnum(x, y)
30
32
  if @sized == true
31
33
  return nil if test_range([1, x], [1, y], [x, @length], [y, @length])
32
34
  end
33
- @sg[[x,y]] = Face.new(x, y, true)
35
+ return nil if @sg[[x,y]]
36
+ @sg[[x,y]] = Face.new(x, y, o)
34
37
  end
35
38
 
36
39
  def get(x, y)
@@ -41,7 +44,7 @@ class SquareGraph
41
44
  def remove(*args)
42
45
  x, y = args[0], args[1]
43
46
  test_fixnum(x, y)
44
- @sg[[x, y]] = nil
47
+ @sg.delete([x, y])
45
48
  end
46
49
 
47
50
  def clear
@@ -54,6 +57,27 @@ class SquareGraph
54
57
  end)
55
58
  end
56
59
 
60
+ def each_obj
61
+ @sg.each_pair do |f, o|
62
+ yield(o.object)
63
+ end
64
+ end
65
+
66
+ def each_pos
67
+ pos = Array.new
68
+ @length.downto(1).each do |l|
69
+ @width.downto(1).each do |w|
70
+ p = Hash.new
71
+ p[:x] = l
72
+ p[:y] = w
73
+ pos.push(p)
74
+ end
75
+ end
76
+ pos.each do |p|
77
+ yield(p)
78
+ end
79
+ end
80
+
57
81
  private
58
82
 
59
83
  def test_fixnum(*value)
@@ -1,5 +1,12 @@
1
1
  require 'square_graph'
2
2
 
3
+ class DummyObject
4
+ attr_accessor :value
5
+ def initialize (value)
6
+ @value = value
7
+ end
8
+ end
9
+
3
10
  describe SquareGraph, '#new' do
4
11
  it "should accept two parameters" do
5
12
  SquareGraph.new(5,5).should_not be_nil
@@ -45,7 +52,9 @@ describe SquareGraph, "#resize" do
45
52
  sg.resize(0, -1).should be_nil
46
53
  end
47
54
  it "should not be able to resize if conflicts" do
48
- #TODO yeah, let's implement this later
55
+ sg = SquareGraph.new(5, 5)
56
+ sg.fill(5, 5)
57
+ sg.resize(4, 4).should be_nil
49
58
  end
50
59
  end
51
60
 
@@ -55,6 +64,12 @@ describe SquareGraph, "#fill, #get" do
55
64
  sg.fill(2,2)
56
65
  sg.get(2,2).should eql(true)
57
66
  end
67
+ it "can also fill a single position with an object by specifying a third parameter" do
68
+ du = DummyObject.new (3)
69
+ sg = SquareGraph.new(5,5)
70
+ sg.fill(3,3,du)
71
+ sg.get(3,3).should eql(du)
72
+ end
58
73
  it "doesn't allow fills of outside the graph" do
59
74
  sg = SquareGraph.new(5,10)
60
75
  sg.fill(10, 2).should be_nil
@@ -82,6 +97,11 @@ describe SquareGraph, "#fill, #get" do
82
97
  sg.get(2,2).should be_nil
83
98
  sg.get(10,10).should be_nil
84
99
  end
100
+ it "return nil when filling a position that's already filled" do
101
+ sg = SquareGraph.new
102
+ sg.fill(0,0)
103
+ sg.fill(0,0).should be_nil
104
+ end
85
105
  end
86
106
 
87
107
  describe SquareGraph, "#insert" do
@@ -142,3 +162,54 @@ describe SquareGraph, "#empty?" do
142
162
  sg.empty?.should eql(false)
143
163
  end
144
164
  end
165
+
166
+ describe SquareGraph, "#each_pos" do
167
+ it "returns a each loop where the value is a 2 value'd array containing x,y" do
168
+ i = 0
169
+ sg = SquareGraph.new(5,5)
170
+ sg.fill(3,3)
171
+ sg.each_pos do |p|
172
+ i += 1 if sg.get(p[:x], p[:y])
173
+ end
174
+ i.should eql(1)
175
+ end
176
+ end
177
+
178
+ describe SquareGraph, "#each_obj" do
179
+ it "returns an each loop where the value is every object in the graph" do
180
+ sg = SquareGraph.new
181
+ a = DummyObject.new(1)
182
+ b = DummyObject.new(2)
183
+ c = DummyObject.new(3)
184
+ d = DummyObject.new(4)
185
+ e = DummyObject.new(5)
186
+ total = 0
187
+ sg.fill(0,0,a)
188
+ sg.fill(0,1,b)
189
+ sg.fill(0,2,c)
190
+ sg.fill(0,3,d)
191
+ sg.fill(0,4,e)
192
+ sg.each_obj do |o|
193
+ total += o.value
194
+ end
195
+ total.should eql(15)
196
+ end
197
+ end
198
+
199
+ describe SquareGraph, "#[][]" do
200
+ it "does the same thing get does" do
201
+ #TODO do this stuff later patches this is epics
202
+ #sg = SquareGraph.new
203
+ #sg.fill(0,0)
204
+ #sg[0][0].should eql(true)
205
+ end
206
+ end
207
+
208
+ describe SquareGraph, "#[][]=" do
209
+ it "does the same thing fill does" do
210
+ #TODO dothis stuff later, if you need reminder, need row and column classes
211
+ #sg = SquareGraph.new
212
+ #sg[0][0] = true
213
+ #sg[0][0].should eql(true)
214
+ end
215
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: square_graph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -25,7 +25,8 @@ files:
25
25
  - spec/square_graph_spec.rb
26
26
  - spec/face_spec.rb
27
27
  homepage: https://github.com/D4L/squareGraph
28
- licenses: []
28
+ licenses:
29
+ - MIT
29
30
  post_install_message:
30
31
  rdoc_options: []
31
32
  require_paths: