square_graph 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/square_graph.rb CHANGED
@@ -63,6 +63,13 @@ class SquareGraph
63
63
  end)
64
64
  end
65
65
 
66
+ def each
67
+ return nil if @sg.empty?
68
+ @sg.each_pair do |p, f|
69
+ yield(f)
70
+ end if block_given?
71
+ end
72
+
66
73
  def each_obj
67
74
  return nil if @sg.empty?
68
75
  @sg.each_pair do |f, o|
@@ -74,8 +81,8 @@ class SquareGraph
74
81
  def each_pos
75
82
  return nil if @sg.empty?
76
83
  pos = Array.new
77
- @length.downto(1).each do |l|
78
- @width.downto(1).each do |w|
84
+ (@length||@sg.keys.max(&comp('x'))[0]).downto(@sized?1:@sg.keys.min(&comp('x'))[0]).each do |l|
85
+ (@width||@sg.keys.max(&comp('y'))[1]).downto(@sized?1:@sg.keys.min(&comp('y'))[1]).each do |w|
79
86
  p = Hash.new
80
87
  p[:x] = l
81
88
  p[:y] = w
@@ -97,6 +104,57 @@ class SquareGraph
97
104
  rt
98
105
  end
99
106
 
107
+ def truthy
108
+ if @sized
109
+ result = SquareGraph.new(@length, @width)
110
+ else
111
+ result = SquareGraph.new
112
+ end
113
+ @sg.each_pair do |p, f|
114
+ result.fill(f.x, f.y, f.object) if f.truthy?
115
+ end
116
+ return nil if result.objects.nil?
117
+ result
118
+ end
119
+
120
+ def need_truthy
121
+ result = Array.new
122
+ @sg.each_pair do |p, f|
123
+ result.push(f.object.class) if not f.object.class.method_defined? :truthy?
124
+ end
125
+ return nil if result.empty?
126
+ result
127
+ end
128
+
129
+ def self.create_truthy (objClass, &truthyMethod)
130
+ return nil if objClass.method_defined? :truthy?
131
+ objClass.send :define_method, :truthy?, truthyMethod
132
+ end
133
+
134
+ def self.create_truthy! (objClass, &truthyMethod)
135
+ objClass.send :define_method, :truthy?, truthyMethod
136
+ end
137
+
138
+ def truthy? (&alt_cond)
139
+ return false if @sg.size == 0
140
+ @sg.each_pair do |p, f|
141
+ return false if not f.truthy?(&alt_cond)
142
+ end
143
+ true
144
+ end
145
+
146
+ def falsey?
147
+ return false if @sg.size == 0
148
+ self.truthy.nil?
149
+ end
150
+
151
+ def == sg2
152
+ sg2.each_pos do |p|
153
+ return false if self.get(p[:x], p[:y]) != sg2.get(p[:x], p[:y])
154
+ end
155
+ true
156
+ end
157
+
100
158
  private
101
159
 
102
160
  def test_fixnum(*value)
@@ -109,6 +167,16 @@ class SquareGraph
109
167
  value.any? {|pair| pair[0] > pair[1]}
110
168
  end
111
169
 
170
+ def comp(pos)
171
+ Proc.new do |x,y|
172
+ if pos == 'x'
173
+ x[0] <=> y[0]
174
+ else
175
+ x[1] <=> y[1]
176
+ end
177
+ end
178
+ end
179
+
112
180
  end
113
181
 
114
182
  require 'square_graph/face'
@@ -5,4 +5,11 @@ class SquareGraph::Face
5
5
  @y = y
6
6
  @object = object
7
7
  end
8
+
9
+ def truthy?(&alt)
10
+ if alt
11
+ return alt.call(object)
12
+ end
13
+ (object == true) || (object.truthy? if object.respond_to? :truthy?)
14
+ end
8
15
  end
data/spec/face_spec.rb CHANGED
@@ -1,14 +1,40 @@
1
1
  require 'square_graph/face'
2
2
 
3
- describe SquareGraph::Face, '#new' do
3
+ class DummyObject
4
+ attr_accessor :value
5
+ def initialize (value)
6
+ @value = value
7
+ end
8
+ end
9
+
10
+ describe SquareGraph::Face, "#new" do
4
11
  it "accepts an x, y, object and can display them easily" do
5
- a = SquareGraph::Face.new(5, 5, true)
6
- a.x.should eql(5)
7
- a.y.should eql(5)
8
- a.object.should eql(true)
12
+ f = SquareGraph::Face.new(5, 5, true)
13
+ f.x.should eql(5)
14
+ f.y.should eql(5)
15
+ f.object.should eql(true)
16
+ end
17
+ it "accepts objects too!" do
18
+ du = DummyObject.new(0)
19
+ f = SquareGraph::Face.new(5,5, du)
20
+ f.object.should eql(du)
9
21
  end
10
22
  end
11
23
 
12
- describe SquareGraph::Face, '#get_truthy' do
13
- #TODO this will be fun, basically, it will get the truth value of the object
24
+ describe SquareGraph::Face, "#truthy?" do
25
+ before(:each) {DummyObject.send(:remove_method, :truthy?) if DummyObject.method_defined? :truthy?}
26
+ it "asks an object whether it is true or not" do
27
+ f = SquareGraph::Face.new(5,5, true)
28
+ f.truthy?.should eql(true)
29
+ end
30
+ it "returns nil if the object doesn't have a truthy method" do
31
+ du = DummyObject.new(0)
32
+ f = SquareGraph::Face.new(5,5, du)
33
+ f.truthy?.should be_nil
34
+ end
35
+ it "accepts a block that will override a truthy method or in replace of a truthy method" do
36
+ du = DummyObject.new(0)
37
+ f = SquareGraph::Face.new(5,5, du)
38
+ (f.truthy?{|o| o.value == 0}).should eql(true)
39
+ end
14
40
  end
@@ -62,7 +62,7 @@ describe SquareGraph, "#fill, #get" do
62
62
  it "fills a single position with a TrueClass and gets it back with get" do
63
63
  sg = SquareGraph.new(5,5)
64
64
  sg.fill(2,2)
65
- sg.get(2,2).should eql(true)
65
+ sg.get(2,2).should be_true
66
66
  end
67
67
  it "can also fill a single position with an object by specifying a third parameter" do
68
68
  du = DummyObject.new (3)
@@ -86,7 +86,7 @@ describe SquareGraph, "#fill, #get" do
86
86
  sg.fill(0,0)
87
87
  sg.objects.size.should eql(1)
88
88
  sg.fill(0,0,nil).should be_nil
89
- sg.empty?.should eql(true)
89
+ sg.empty?.should be_true
90
90
  end
91
91
  it "doesn't allow fills of outside the graph" do
92
92
  sg = SquareGraph.new(5,10)
@@ -104,7 +104,7 @@ describe SquareGraph, "#fill, #get" do
104
104
  sg = SquareGraph.new
105
105
  sg.fill(2,2)
106
106
  sg.fill(-1, -3)
107
- sg.get(-1, -3).should eql(true)
107
+ sg.get(-1, -3).should be_true
108
108
  end
109
109
  it "nils if getting something that doesn't exist yet" do
110
110
  sg = SquareGraph.new
@@ -132,7 +132,7 @@ describe SquareGraph, "#remove" do
132
132
  it "removes any objects from a certain position" do
133
133
  sg = SquareGraph.new
134
134
  sg.fill(2,2)
135
- sg.get(2,2).should eql(true)
135
+ sg.get(2,2).should be_true
136
136
  sg.remove(2,2)
137
137
  sg.get(2,2).should be_nil
138
138
  end
@@ -150,7 +150,7 @@ describe SquareGraph, "#clear" do
150
150
  it "removes every object in the graph" do
151
151
  sg = SquareGraph.new
152
152
  (0..5).each {|x| sg.fill(x, x)}
153
- (1..6).each {|x| sg.get(x-1, x-1).should eql(true)}
153
+ (1..6).each {|x| sg.get(x-1, x-1).should be_true}
154
154
  sg.clear
155
155
  (2..7).each {|x| sg.get(x-2, x-2).should be_nil}
156
156
  end
@@ -164,15 +164,15 @@ describe SquareGraph, "#empty?" do
164
164
  sg.fill(x,y)
165
165
  end
166
166
  end
167
- sg.get(3, 3).should eql(true)
167
+ sg.get(3, 3).should be_true
168
168
  sg.clear
169
- sg.empty?.should eql(true)
169
+ sg.empty?.should be_true
170
170
  end
171
171
  it "informs us even if we just remove the items" do
172
172
  sg = SquareGraph.new
173
173
  sg.fill(0,0)
174
174
  sg.remove(0, 0)
175
- sg.empty?.should eql(true)
175
+ sg.empty?.should be_true
176
176
  end
177
177
  it "returns false if the graph isn't empty" do
178
178
  sg = SquareGraph.new
@@ -196,6 +196,27 @@ describe SquareGraph, "#each_pos" do
196
196
  sg.fill(3,3)
197
197
  sg.each_pos.class.should eql(Enumerator)
198
198
  end
199
+ it "works for non-dimentioned graphs by using the max/min points" do
200
+ sg = SquareGraph.new
201
+ sg.fill(0,0)
202
+ sg.fill(2,2)
203
+ i = 0
204
+ sg.each_pos do |p|
205
+ i += 1
206
+ end
207
+ i.should eql(9)
208
+ end
209
+ it "works if there are negative positions too" do
210
+ sg = SquareGraph.new
211
+ sg.fill(-3, -4)
212
+ sg.fill(1, 4)
213
+ sg.fill(-1, 8)
214
+ i = 0
215
+ sg.each_pos do |p|
216
+ i += 1
217
+ end
218
+ i.should eql(65)
219
+ end
199
220
  end
200
221
 
201
222
  describe SquareGraph, "#each_obj" do
@@ -229,7 +250,7 @@ describe SquareGraph, "#[][]" do
229
250
  #TODO do this stuff later patches this is epics
230
251
  #sg = SquareGraph.new
231
252
  #sg.fill(0,0)
232
- #sg[0][0].should eql(true)
253
+ #sg[0][0].should be_true
233
254
  end
234
255
  end
235
256
 
@@ -238,7 +259,7 @@ describe SquareGraph, "#[][]=" do
238
259
  #TODO dothis stuff later, if you need reminder, need row and column classes
239
260
  #sg = SquareGraph.new
240
261
  #sg[0][0] = true
241
- #sg[0][0].should eql(true)
262
+ #sg[0][0].should be_true
242
263
  end
243
264
  end
244
265
 
@@ -249,7 +270,7 @@ describe SquareGraph, "#objects" do
249
270
  sg.fill(2,2)
250
271
  sg.fill(3,3)
251
272
  sg.objects.each do |i|
252
- i.should eql(true)
273
+ i.should be_true
253
274
  end
254
275
  end
255
276
  it "returns nil if there aren't any objects" do
@@ -258,9 +279,136 @@ describe SquareGraph, "#objects" do
258
279
  end
259
280
  end
260
281
 
261
- describe SquareGraph, "#anytrue?" do
262
- it "requires some more truthy stuff" do
263
- #TODO kekeke
282
+ describe SquareGraph, "#truthy?" do
283
+ it "checks if any of the objects result in truthy" do
284
+ sg = SquareGraph.new
285
+ sg.truthy?.should eql(false)
286
+ sg.fill(0,0)
287
+ sg.truthy?.should be_true
288
+ end
289
+ it "lets users pass a block that will be used as truthy? for each object" do
290
+ sg = SquareGraph.new
291
+ du = DummyObject.new(0)
292
+ sg.fill(0,0, du)
293
+ sg.truthy?{|o| o.value == 0}.should be_true
294
+ sg.truthy?{|o| o.value == 1}.should eql(false)
295
+ end
296
+ end
297
+
298
+ describe SquareGraph, "#falsey?" do
299
+ it "checks if any of the objects results in falsey" do
300
+ sg = SquareGraph.new
301
+ sg.falsey?.should eql(false)
302
+ sg.fill(0,0)
303
+ sg.falsey?.should eql(false)
304
+ end
305
+ end
306
+ describe SquareGraph, "#truthy" do
307
+ it "returns a SquareGraph that contains only the truthy elements" do
308
+ sg = SquareGraph.new
309
+ sg.fill(0,0)
310
+ tsg = sg.truthy
311
+ sg.should eq(tsg)
312
+ end
313
+ it "returns nil if there aren't any truthy objects" do
314
+ sg = SquareGraph.new
315
+ sg.truthy?.should eql(false)
316
+ sg.truthy.should be_nil
317
+ end
318
+ end
319
+
320
+ describe SquareGraph, "#need_truthy" do
321
+ before(:each) {DummyObject.send(:remove_method, :truthy?) if DummyObject.method_defined? :truthy?}
322
+ it "provides an array of all the classes in the squareGraph that need a truthy method" do
323
+ sg = SquareGraph.new
324
+ du = DummyObject.new(0)
325
+ sg.fill(0,0, du)
326
+ sg.need_truthy.include?(DummyObject).should be_true
327
+ end
328
+ it "returns nil if there all the elements in the squareGraph have a truthy method" do
329
+ sg = SquareGraph.new
330
+ du = DummyObject.new(0)
331
+ tehTruthy = Proc.new do
332
+ p "hello world"
333
+ end
334
+ SquareGraph.create_truthy(DummyObject, &tehTruthy)
335
+ sg.need_truthy.should be_nil
264
336
  end
265
337
  end
266
338
 
339
+ describe SquareGraph, ".create_truthy" do
340
+ before(:each) {DummyObject.send(:remove_method, :truthy?) if DummyObject.method_defined? :truthy?}
341
+ it "helps define truthy methods for classes" do
342
+ sg = SquareGraph.new
343
+ du = DummyObject.new(0)
344
+ SquareGraph.create_truthy(DummyObject) do
345
+ value == 0
346
+ end
347
+ du.truthy?.should be_true
348
+ end
349
+ it "will nil if truthy is already defined" do
350
+ sg = SquareGraph.new
351
+ du = DummyObject.new(0)
352
+ SquareGraph.create_truthy(DummyObject) do
353
+ value == 1
354
+ end
355
+ du.truthy?.should eql(false)
356
+ SquareGraph.create_truthy(DummyObject) do
357
+ value == 0
358
+ end.should be_nil
359
+ du.truthy?.should eql(false)
360
+ end
361
+ end
362
+
363
+ describe SquareGraph, ".create_truthy!" do
364
+ before(:each) {DummyObject.send(:remove_method, :truthy?) if DummyObject.method_defined? :truthy?}
365
+ it "does the same as .create_truthy" do
366
+ sg = SquareGraph.new
367
+ du = DummyObject.new(0)
368
+ SquareGraph.create_truthy!(DummyObject) do
369
+ value == 0
370
+ end
371
+ du.truthy?.should be_true
372
+ end
373
+ it "overwrite truthy methods!" do
374
+ sg = SquareGraph.new
375
+ du = DummyObject.new(0)
376
+ SquareGraph.create_truthy(DummyObject) do
377
+ value == 1
378
+ end
379
+ du.truthy?.should eql(false)
380
+ SquareGraph.create_truthy!(DummyObject) do
381
+ value == 0
382
+ end.should_not be_nil
383
+ du.truthy?.should be_true
384
+ end
385
+ end
386
+
387
+ describe SquareGraph, "#each" do
388
+ it "returns an iterator of faces." do
389
+ sg = SquareGraph.new
390
+ sg.fill(0,0)
391
+ sg.fill(1,1)
392
+ sg.each do |f|
393
+ f.truthy?.should be_true
394
+ end
395
+ end
396
+ end
397
+
398
+ describe SquareGraph, "#==" do
399
+ it "compares two squaregraphs without identites" do
400
+ sg1 = SquareGraph.new
401
+ sg2 = SquareGraph.new
402
+ (sg1 == sg2).should be_true
403
+ sg1.should == sg2
404
+ end
405
+ it "compares objects without identies too" do
406
+ du = DummyObject.new(5)
407
+ sg1 = SquareGraph.new
408
+ sg2 = SquareGraph.new
409
+ sg1.fill(0,0,du)
410
+ sg2.fill(0,0,du)
411
+ (sg1 == sg2).should be_true
412
+ sg1.should == sg2
413
+ end
414
+ 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: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,13 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-26 00:00:00.000000000 Z
12
+ date: 2012-03-26 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A dimensionable graph that acts kinda like a grid. You can fill in spots
15
- with objects and then iterate over the entire grid or just objects. This is just
16
- version 1, there will be alot of neat tricks to come.
15
+ with objects and then iterate over the entire grid or just objects. Now, there is
16
+ a new concept called truthy which will return given a certain condition. Each object
17
+ in the graph can be given a truthy? method and you can use that to iterate through
18
+ objects in the graphs more efficiently.
17
19
  email: Austin.L.D4L@gmail.com
18
20
  executables: []
19
21
  extensions: []