-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
1510 lines (1269 loc) · 124 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Albert Agram]]></title>
<link href="http://alagram.github.io/atom.xml" rel="self"/>
<link href="http://alagram.github.io/"/>
<updated>2014-10-13T13:21:31+00:00</updated>
<id>http://alagram.github.io/</id>
<author>
<name><![CDATA[Albert Agram]]></name>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[Evaluating Reverse Polish Notation Using a Stack in Ruby]]></title>
<link href="http://alagram.github.io/blog/2014/10/12/evaluating-reverse-polish-notation-using-a-stack-in-ruby/"/>
<updated>2014-10-12T10:45:18+00:00</updated>
<id>http://alagram.github.io/blog/2014/10/12/evaluating-reverse-polish-notation-using-a-stack-in-ruby</id>
<content type="html"><![CDATA[<!-- more -->
<h2 id="introduction">Introduction</h2>
<p>Let's say you want to add two numbers <code>4</code> and <code>9</code>, you'll have three ways to go about it:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>+ 4 9 # Prefix notation or Polish Notation
</span><span class='line'>4 + 9 # Infix notation
</span><span class='line'>4 9 + # Postfix notation or Reverse Polish Notation</span></code></pre></td></tr></table></div></figure>
<p>First we have the <code>Prefix notation</code> or <code>Polish notation</code>, <code>Infix notation</code> and <code>Postfix notation</code> or <code>Reverse Polish Notation</code>. What we're used to is the <em>infix notation</em>. Further when you write expressions like:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>7 + 5
</span><span class='line'>7 + 4 * 2
</span><span class='line'>4 * 7 + 3 * 8 + 16</span></code></pre></td></tr></table></div></figure>
<p>we've been trained to look at <em>infix notation</em> and instantly know how to do the calculation. Simple expressions like the first can be straight forward, however things become ambiguous when the expression becomes complex like we have in the second and third examples. We'll have to use parenthesis everywhere and take note of order of operation when evaluating such expressions. By order of operation, I mean we know to do multiplication before addition etc. In the end you'd have to simplify the second expression to become:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>((7 + 4) * 2)</span></code></pre></td></tr></table></div></figure>
<p>and the third to look like:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>(4 * (((7 + 3) * 8) + 16))</span></code></pre></td></tr></table></div></figure>
<p>before easy simplification can take place. In other words, when more operators are combined doing the calculation is not always straightforward.</p>
<p>Believe it or not, even though they might look unusual at first, Prefix Notation (<a href="http://en.wikipedia.org/wiki/Polish_notation">Polish Notation</a>) and Postfix Notation (<a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation">Reverse Polish Notation</a>) offer advantages that let us perform such calculations with ease.</p>
<h3 id="advantages-of-prefix-and-postfix-notation">Advantages of Prefix and Postfix notation</h3>
<ol>
<li>You don't need the idea of "order of operation".</li>
<li>You never need parentheses.</li>
</ol>
<p>Let's see it in action by transforming the expression <code>4 * (((7 + 3) * 8) + 16)</code> to prefix and postfix notation:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'># Prefix
</span><span class='line'>* 4 + * + 7 3 8 16</span></code></pre></td></tr></table></div></figure>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'># Postfix
</span><span class='line'>4 7 3 + 8 * 16 + *</span></code></pre></td></tr></table></div></figure>
<h3 id="evaluating-postfix/reverse-polish-notation-(rpn)-expressions">Evaluating Postfix/Reverse Polish Notation (RPN) Expressions</h3>
<p>The reason RPN is easier is, the operators are right next to the operands they're operating on. In other words, the first operator you see is the first operator you apply. Whereas in infix notation the operator you apply can be arbitrarily far into the expression. Let's use the expression <code>4 7 3 + 8 * 16 + *</code> to illustrate. RPN is evaluated by reading the expression from left to right and performing the operation as soon as you see it. Reading from left to right, the first operator we see is <code>+</code>. Since addition is a <a href="http://en.wikipedia.org/wiki/Binary_operation">binary operation</a> the <code>+</code> will take effect on the last two numbers <code>7</code> and <code>3</code>. After evaluation <code>7 + 3</code> the result of that is placed into the original expression and is transformed into:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>4 10 8 * 16 + *</span></code></pre></td></tr></table></div></figure>
<p>The next operation we see is <code>*</code> which is also a binary operation. <code>10 * 8</code> is evaluated and the expression is transformed into:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>4 80 16 + *</span></code></pre></td></tr></table></div></figure>
<p>Next, we see the <code>+</code> operator and we perform <code>80 + 16</code> which turns the expression into:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>4 96 *</span></code></pre></td></tr></table></div></figure>
<p>Finally, we see <code>*</code> another binary operation. The final evaluation becomes:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>4 * 96</span></code></pre></td></tr></table></div></figure>
<p>which gives us <code>384</code>.</p>
<p>The algorithm for parsing RPN expressions is called the <a href="http://en.wikipedia.org/wiki/Shunting-yard_algorithm">Shunting-yard algorithm</a> and is implemented using a stack. We dive into that next and implement a stack class in Ruby.</p>
<h3 id="stack">Stack</h3>
<p>A <a href="http://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29">stack</a> is an <a href="http://en.wikipedia.org/wiki/Abstract_data_type">abstract data type</a> that is common and is used all over. An example of a stack is a stack of plates on a table. Say you have 4 plates on top of each other on a table, the only way to add a plate to the stack is by adding it to the top. Also, the only way to get a plate off the stack is by taking it from the top. Hence, it adhere's to the <strong>last in, first out</strong> or <a href="http://en.wikipedia.org/wiki/LIFO_%28computing%29">LIFO</a> pattern. You can think of an abstract data type as a collection of data and a set of operations on it with specifications on how those operations behave. A stack is an abstract data type because an implementation that satisfies those operations is an accepted implementation. A stack has a few operations namely:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'> push(item) -> place new item on stack
</span><span class='line'> pop -> remove most recent item added to stack
</span><span class='line'> peek -> return most recent item added to the stack (don't remove)
</span><span class='line'> empty? -> returns true if nothing on stack, returns false otherwise</span></code></pre></td></tr></table></div></figure>
<p>With <a href="http://en.wikipedia.org/wiki/Shunting-yard_algorithm">Shunting-yard algorithm</a> in mind, let's create a Stack class in Ruby:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Stack</span>
</span><span class='line'> <span class="k">class</span> <span class="nc">UnderFlowError</span> <span class="o"><</span> <span class="no">StandardError</span><span class="p">;</span><span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">initialize</span>
</span><span class='line'> <span class="vi">@stack</span> <span class="o">=</span> <span class="o">[]</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">empty?</span>
</span><span class='line'> <span class="vi">@stack</span><span class="o">.</span><span class="n">empty?</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">push</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>
</span><span class='line'> <span class="vi">@stack</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">pop</span>
</span><span class='line'> <span class="k">raise</span> <span class="no">UnderFlowError</span><span class="p">,</span> <span class="s2">"Stack is empty"</span> <span class="k">if</span> <span class="n">empty?</span>
</span><span class='line'> <span class="vi">@stack</span><span class="o">.</span><span class="n">pop</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">peek</span>
</span><span class='line'> <span class="vi">@stack</span><span class="o">.</span><span class="n">last</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>Note we're raising an error just in case an empty stack is being popped.</p>
<p>Next, lets create a class to handle RPN expression evaluation with addition, multiplication and subtraction operators:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># rpn_expression.rb</span>
</span><span class='line'><span class="o">.</span>
</span><span class='line'><span class="o">.</span>
</span><span class='line'><span class="o">.</span>
</span><span class='line'><span class="c1"># Stack class omitted for brevity</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">RPNExpression</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
</span><span class='line'> <span class="vi">@expr</span> <span class="o">=</span> <span class="n">expr</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">evaluate</span>
</span><span class='line'> <span class="n">stack</span> <span class="o">=</span> <span class="no">Stack</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'>
</span><span class='line'> <span class="n">tokens</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">token</span><span class="o">|</span>
</span><span class='line'> <span class="k">if</span> <span class="n">numeric?</span><span class="p">(</span><span class="n">token</span><span class="p">)</span>
</span><span class='line'> <span class="n">stack</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="n">token</span><span class="o">.</span><span class="n">to_i</span><span class="p">)</span>
</span><span class='line'> <span class="k">elsif</span> <span class="n">token</span> <span class="o">==</span> <span class="s2">"+"</span>
</span><span class='line'> <span class="n">rhs</span> <span class="o">=</span> <span class="n">stack</span><span class="o">.</span><span class="n">pop</span>
</span><span class='line'> <span class="n">lhs</span> <span class="o">=</span> <span class="n">stack</span><span class="o">.</span><span class="n">pop</span>
</span><span class='line'> <span class="n">stack</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="n">lhs</span> <span class="o">+</span> <span class="n">rhs</span><span class="p">)</span>
</span><span class='line'> <span class="k">elsif</span> <span class="n">token</span> <span class="o">==</span> <span class="s2">"*"</span>
</span><span class='line'> <span class="n">rhs</span> <span class="o">=</span> <span class="n">stack</span><span class="o">.</span><span class="n">pop</span>
</span><span class='line'> <span class="n">lhs</span> <span class="o">=</span> <span class="n">stack</span><span class="o">.</span><span class="n">pop</span>
</span><span class='line'> <span class="n">stack</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="n">lhs</span> <span class="o">*</span> <span class="n">rhs</span><span class="p">)</span>
</span><span class='line'> <span class="k">elsif</span> <span class="n">token</span> <span class="o">==</span> <span class="s2">"-"</span>
</span><span class='line'> <span class="n">rhs</span> <span class="o">=</span> <span class="n">stack</span><span class="o">.</span><span class="n">pop</span>
</span><span class='line'> <span class="n">lhs</span> <span class="o">=</span> <span class="n">stack</span><span class="o">.</span><span class="n">pop</span>
</span><span class='line'> <span class="n">stack</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="n">lhs</span> <span class="o">-</span> <span class="n">rhs</span><span class="p">)</span>
</span><span class='line'> <span class="k">else</span>
</span><span class='line'> <span class="k">raise</span> <span class="s2">"whaaaat I don't know this token?"</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="n">stack</span><span class="o">.</span><span class="n">pop</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="kp">private</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">tokens</span>
</span><span class='line'> <span class="vi">@expr</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">" "</span><span class="p">)</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">numeric?</span><span class="p">(</span><span class="n">token</span><span class="p">)</span>
</span><span class='line'> <span class="n">token</span> <span class="o">=~</span> <span class="sr">/^-?\d\+$/</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>We're doing a few things here. <code>tokens</code> helps us split the expression into tokens based on spaces and <code>numeric?</code> helps us determine if the token is a number or not. The <code>evaluate</code> method is where everything happens. In <code>evaluate</code> we're iterating through the tokens and checking if it's a number or an operator. If it's a number, it's pushed onto the stack. If it's not a number, it's an operator, in which case the topmost token is popped from the stack and named <code>rhs</code>. The next token is also popped and named <code>lsh</code>. These tokens become operands and the operation is performed. After this the result is pushed back onto the stack. After all the tokens are check and all operations performed, the last item in the stack is popped to give us the final evaluated value.</p>
<p>Example:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">RPNExpression</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s2">"4 7 3 + 8 * 16 + *"</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="o">=></span> <span class="mi">384</span>
</span></code></pre></td></tr></table></div></figure>
<p>Try it. There are other ways of simplifying the <code>RPNExpression</code> class and I'll leave that up to the reader.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Simple Test Using TDD With Rspec]]></title>
<link href="http://alagram.github.io/blog/2014/09/05/simple-test-using-tdd-with-rspec/"/>
<updated>2014-09-05T19:12:00+00:00</updated>
<id>http://alagram.github.io/blog/2014/09/05/simple-test-using-tdd-with-rspec</id>
<content type="html"><![CDATA[<!-- more -->
<h2 id="introduction">Introduction</h2>
<p>Test Driven Development is great for driving out implementation of features in software development. In this post, I'm going to go through how I write tests using the TDD approach with <a href="https://github.com/rspec/rspec-rails">Rspec</a> by going through the steps to drive out the implementation of simple search feature. We'll implement a method named <code>search_by_title</code>.</p>
<h2 id="how-i-write-tests">How I Write Tests</h2>
<p>1) I start by thinking about all the test cases I want to cover. That is, thinking on a high level on what the functionality should achieve. This allows me to have more complete coverage of what I want my code to accomplish instead of diving straight into one test, making it pass and then figuring out what the next test should be.</p>
<p>2) Setup data for tests, perform action and put an assertion on the result of the action.</p>
<p>3) Run test and let it fail.</p>
<p>4) Finally, take small steps to make the test pass. This is because in TDD you want to write the simplest code to make a test pass and also make sure that every piece of code you write is going to be the result of a failing test.</p>
<h3 id="the-code">The code</h3>
<p>By following step 1, I write out all the test cases I want to cover:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s2">"rails_helper"</span>
</span><span class='line'>
</span><span class='line'><span class="no">RSpec</span><span class="o">.</span><span class="n">describe</span> <span class="no">Video</span><span class="p">,</span> <span class="ss">:type</span> <span class="o">=></span> <span class="ss">:model</span> <span class="k">do</span>
</span><span class='line'> <span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">belong_to</span> <span class="ss">:category</span> <span class="p">}</span>
</span><span class='line'> <span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">validate_presence_of</span> <span class="ss">:title</span> <span class="p">}</span>
</span><span class='line'> <span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">validate_presence_of</span> <span class="ss">:description</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">describe</span> <span class="s2">"search_by_title"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">it</span> <span class="s2">"returns an empty array if there is no match"</span>
</span><span class='line'> <span class="n">it</span> <span class="s2">"returns an array of one video for an exact match"</span>
</span><span class='line'> <span class="n">it</span> <span class="s2">"returns an array of one video for a partial match"</span>
</span><span class='line'> <span class="n">it</span> <span class="s2">"returns an array of all matches ordered by created_at"</span>
</span><span class='line'> <span class="n">it</span> <span class="s2">"returns an empty array for a search with an empty string"</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>The video model in <code>app/models/video.rb</code> looks like this:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Video</span> <span class="o"><</span> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span>
</span><span class='line'> <span class="n">belongs_to</span> <span class="ss">:category</span>
</span><span class='line'> <span class="n">validates_presence_of</span> <span class="ss">:title</span><span class="p">,</span> <span class="ss">:description</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>Next, I'll set up the data I need for the first test, perform an action and make an assertion on the result; step 2. The code for the first test looks like this:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">it</span> <span class="s2">"returns an empty array if there is no match"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">futurama</span> <span class="o">=</span> <span class="no">Video</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">title</span><span class="p">:</span> <span class="s2">"Futurama"</span><span class="p">,</span> <span class="ss">description</span><span class="p">:</span> <span class="s2">"Space Travel!"</span><span class="p">)</span>
</span><span class='line'> <span class="n">back_to_future</span> <span class="o">=</span> <span class="no">Video</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">title</span><span class="p">:</span> <span class="s2">"Back to Future"</span><span class="p">,</span> <span class="ss">description</span><span class="p">:</span> <span class="s2">"Time Travel"</span><span class="p">)</span>
</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="no">Video</span><span class="o">.</span><span class="n">search_by_title</span><span class="p">(</span><span class="s2">"hello"</span><span class="p">))</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="o">[]</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>Now I'll run the test and let it fail; step 3:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>rspec spec/models/video_spec.rb
</span></code></pre></td></tr></table></div></figure>
<p><img src="http://alagram.github.io/images/2014-09-05-simle-test-using-tdd-with-rspec/spec_1.png" title="Spec 1" ></p>
<p>Test is complaining about <code>NoMethodError: undefined method search_by_title for #<Class:0x007f9ec0ad0d98></code></p>
<p>Next, make that little change in <code>app/models/video.rb</code>; step 4:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Video</span> <span class="o"><</span> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span>
</span><span class='line'> <span class="n">belongs_to</span> <span class="ss">:category</span>
</span><span class='line'>
</span><span class='line'> <span class="n">validates_presence_of</span> <span class="ss">:title</span><span class="p">,</span> <span class="ss">:description</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">search_by_title</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>Run test again:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>rspec spec/models/video_spec.rb
</span></code></pre></td></tr></table></div></figure>
<p><img src="http://alagram.github.io/images/2014-09-05-simle-test-using-tdd-with-rspec/spec_2.png" title="Spec 2" ></p>
<p>This time the failure is <code>ArgumentError: wrong number of arguments (1 for 0)</code>. In our test, we pass a parameter to the method and have to replicate that in the real code.</p>
<p>Let's make that change:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Video</span> <span class="o"><</span> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span>
</span><span class='line'>
</span><span class='line'> <span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">search_by_title</span><span class="p">(</span><span class="n">search_term</span><span class="p">)</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>Run the test again:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>rspec spec/models/video_spec.rb
</span></code></pre></td></tr></table></div></figure>
<p><img src="http://alagram.github.io/images/2014-09-05-simle-test-using-tdd-with-rspec/spec_3.png" title="Spec 3" ></p>
<p>There is another error:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>expected: <span class="o">[]</span>
</span><span class='line'>got: nil
</span></code></pre></td></tr></table></div></figure>
<p>The easiest change to make the test pass is to return an empty array in the <code>search_by_title</code> method:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Video</span> <span class="o"><</span> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span>
</span><span class='line'>
</span><span class='line'> <span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">search_by_title</span><span class="p">(</span><span class="n">search_term</span><span class="p">)</span>
</span><span class='line'> <span class="o">[]</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>At this point, if all all method does is to return an empty array this is all we need to satisfy that.</p>
<p>Now when we run the test again, we'll have a passing test:</p>
<p><img src="http://alagram.github.io/images/2014-09-05-simle-test-using-tdd-with-rspec/spec_4.png" title="Spec 4" ></p>
<p>Fortunately, we have other test cases that we do want to satisfy and by following this simple process for each test, we're forced to drive-out the actually implementation of the method:</p>
<p>Our final code in <code>app/models/video.rb</code> will look like this:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Video</span> <span class="o"><</span> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span>
</span><span class='line'> <span class="n">belongs_to</span> <span class="ss">:category</span>
</span><span class='line'>
</span><span class='line'> <span class="n">validates_presence_of</span> <span class="ss">:title</span><span class="p">,</span> <span class="ss">:description</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">search_by_title</span><span class="p">(</span><span class="n">search_term</span><span class="p">)</span>
</span><span class='line'> <span class="k">return</span> <span class="o">[]</span> <span class="k">if</span> <span class="n">search_term</span><span class="o">.</span><span class="n">blank?</span>
</span><span class='line'> <span class="n">where</span><span class="p">(</span><span class="s2">"title LIKE ?"</span><span class="p">,</span> <span class="s2">"%</span><span class="si">#{</span><span class="n">search_term</span><span class="si">}</span><span class="s2">%"</span><span class="p">)</span><span class="o">.</span><span class="n">order</span><span class="p">(</span><span class="s2">"created_at DESC"</span><span class="p">)</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>And our test code will be:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s2">"rails_helper"</span>
</span><span class='line'>
</span><span class='line'><span class="no">RSpec</span><span class="o">.</span><span class="n">describe</span> <span class="no">Video</span><span class="p">,</span> <span class="ss">:type</span> <span class="o">=></span> <span class="ss">:model</span> <span class="k">do</span>
</span><span class='line'> <span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">belong_to</span> <span class="ss">:category</span> <span class="p">}</span>
</span><span class='line'> <span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">validate_presence_of</span> <span class="ss">:title</span> <span class="p">}</span>
</span><span class='line'> <span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">validate_presence_of</span> <span class="ss">:description</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">describe</span> <span class="s2">"search_by_title"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">it</span> <span class="s2">"returns an empty array if there is no match"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">futurama</span> <span class="o">=</span> <span class="no">Video</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">title</span><span class="p">:</span> <span class="s2">"Futurama"</span><span class="p">,</span> <span class="ss">description</span><span class="p">:</span> <span class="s2">"Space Travel!"</span><span class="p">)</span>
</span><span class='line'> <span class="n">back_to_future</span> <span class="o">=</span> <span class="no">Video</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">title</span><span class="p">:</span> <span class="s2">"Back to Future"</span><span class="p">,</span> <span class="ss">description</span><span class="p">:</span> <span class="s2">"Time Travel"</span><span class="p">)</span>
</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="no">Video</span><span class="o">.</span><span class="n">search_by_title</span><span class="p">(</span><span class="s2">"hello"</span><span class="p">))</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="o">[]</span><span class="p">)</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="n">it</span> <span class="s2">"returns an array of one video for an exact match"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">futurama</span> <span class="o">=</span> <span class="no">Video</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">title</span><span class="p">:</span> <span class="s2">"Futurama"</span><span class="p">,</span> <span class="ss">description</span><span class="p">:</span> <span class="s2">"Space Travel!"</span><span class="p">)</span>
</span><span class='line'> <span class="n">back_to_future</span> <span class="o">=</span> <span class="no">Video</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">title</span><span class="p">:</span> <span class="s2">"Back to Future"</span><span class="p">,</span> <span class="ss">description</span><span class="p">:</span> <span class="s2">"Time Travel"</span><span class="p">)</span>
</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="no">Video</span><span class="o">.</span><span class="n">search_by_title</span><span class="p">(</span><span class="s2">"Futurama"</span><span class="p">))</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="o">[</span><span class="n">futurama</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="n">it</span> <span class="s2">"returns an array of one video for a partial match"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">futurama</span> <span class="o">=</span> <span class="no">Video</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">title</span><span class="p">:</span> <span class="s2">"Futurama"</span><span class="p">,</span> <span class="ss">description</span><span class="p">:</span> <span class="s2">"Space Travel!"</span><span class="p">)</span>
</span><span class='line'> <span class="n">back_to_future</span> <span class="o">=</span> <span class="no">Video</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">title</span><span class="p">:</span> <span class="s2">"Back to Future"</span><span class="p">,</span> <span class="ss">description</span><span class="p">:</span> <span class="s2">"Time Travel"</span><span class="p">)</span>
</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="no">Video</span><span class="o">.</span><span class="n">search_by_title</span><span class="p">(</span><span class="s2">"urama"</span><span class="p">))</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="o">[</span><span class="n">futurama</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="n">it</span> <span class="s2">"returns an array of all matches ordered by created_at"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">futurama</span> <span class="o">=</span> <span class="no">Video</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">title</span><span class="p">:</span> <span class="s2">"Futurama"</span><span class="p">,</span> <span class="ss">description</span><span class="p">:</span> <span class="s2">"Space Travel!"</span><span class="p">,</span> <span class="n">created_at</span><span class="p">:</span> <span class="mi">1</span><span class="o">.</span><span class="n">day</span><span class="o">.</span><span class="n">ago</span><span class="p">)</span>
</span><span class='line'> <span class="n">back_to_future</span> <span class="o">=</span> <span class="no">Video</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">title</span><span class="p">:</span> <span class="s2">"Back to Future"</span><span class="p">,</span> <span class="ss">description</span><span class="p">:</span> <span class="s2">"Time Travel"</span><span class="p">)</span>
</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="no">Video</span><span class="o">.</span><span class="n">search_by_title</span><span class="p">(</span><span class="s2">"Futur"</span><span class="p">))</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="o">[</span><span class="n">back_to_future</span><span class="p">,</span> <span class="n">futurama</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="n">it</span> <span class="s2">"returns an empty array for a search with an empty string"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">futurama</span> <span class="o">=</span> <span class="no">Video</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">title</span><span class="p">:</span> <span class="s2">"Futurama"</span><span class="p">,</span> <span class="ss">description</span><span class="p">:</span> <span class="s2">"Space Travel!"</span><span class="p">,</span> <span class="n">created_at</span><span class="p">:</span> <span class="mi">1</span><span class="o">.</span><span class="n">day</span><span class="o">.</span><span class="n">ago</span><span class="p">)</span>
</span><span class='line'> <span class="n">back_to_future</span> <span class="o">=</span> <span class="no">Video</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">title</span><span class="p">:</span> <span class="s2">"Back to Future"</span><span class="p">,</span> <span class="ss">description</span><span class="p">:</span> <span class="s2">"Time Travel"</span><span class="p">)</span>
</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="no">Video</span><span class="o">.</span><span class="n">search_by_title</span><span class="p">(</span><span class="s2">""</span><span class="p">))</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="o">[]</span><span class="p">)</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>And all our tests pass:</p>
<p><img src="http://alagram.github.io/images/2014-09-05-simle-test-using-tdd-with-rspec/spec_pass.png" title="Spec Pass" ></p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Types of Testing and Shoulda-matchers]]></title>
<link href="http://alagram.github.io/blog/2014/08/31/types-of-testing-and-shoulda-matchers/"/>
<updated>2014-08-31T11:11:00+00:00</updated>
<id>http://alagram.github.io/blog/2014/08/31/types-of-testing-and-shoulda-matchers</id>
<content type="html"><![CDATA[<!-- more -->
<h2 id="introduction">Introduction</h2>
<p>When developing software, how do you know something works? Its certain that all software have bugs and the only way to make sure the code you've written is bug-free is by designing test cases to test against your codebase. It is the most objective way to say confidently that a feature works and things did not break. This post looks at types of testing and how to use <a href="https://github.com/thoughtbot/shoulda-matchers">shoulda-matchers</a> to test common Rails functionality.</p>
<h3 id="types-of-testing">Types of testing</h3>
<p>1) <em><strong>Unit Tests</strong></em>: Unit tests involve testing different components in isolation. You'll typically focus on one block at a time and make sure all cases are covered. In the Rails context, unit testing includes tests for models, views, helpers and routes in isolation. In sum, you're focusing on one thing at a time. Unit tests are the fastest in terms of speed and offer the best coverage.</p>
<p>2) <em><strong>Functional Tests</strong></em>: Functional tests involve testing multiple components together. This is another way of making sure different parts of a system work in tandem. In the Rails context, functional testing includes controller tests. Typically the controller is where you pull multiple models and make them work with eachother to generate data before passing them over to the views. You'll also be testing request and response cycles.</p>
<p>3) <em><strong>Integration Tests</strong></em>: This involves following a business process to make sure that not only do components work together, but can also work together to achieve a business objective. Integration testing involves mimicking the end-user by driving through the browser. You can login, fill a form, submit forms, click links and buttons etc. Integration tests are more realistic because you're following what a user of your system would do.</p>
<h3 id="shoulda-matcher">shoulda-matcher</h3>
<p>In Test Driven Development (TDD) you'll typically want to test your own code. <a href="https://github.com/thoughtbot/shoulda-matchers">shoulda-matchers</a> is a library that simplifies testing common Rails functionality like associations and validations. Suppose we have a <code>video</code> model with the following code:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># video.rb</span>
</span><span class='line'><span class="k">class</span> <span class="nc">Video</span> <span class="o"><</span> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span>
</span><span class='line'> <span class="n">belongs_to</span> <span class="ss">:category</span>
</span><span class='line'>
</span><span class='line'> <span class="n">validates_presence_of</span> <span class="ss">:title</span><span class="p">,</span> <span class="ss">:description</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>using <code>shoulda-matchers</code> we can write a few lines of code to test the functionality of our code:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">belong_to</span> <span class="ss">:category</span> <span class="p">}</span>
</span><span class='line'><span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">validate_presence_of</span> <span class="ss">:title</span> <span class="p">}</span>
</span><span class='line'><span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">validate_presence_of</span> <span class="ss">:description</span> <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>These simple lines can take the place of all the following tests:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">it</span> <span class="s2">"belongs to a category"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">cat</span> <span class="o">=</span> <span class="no">Category</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="nb">name</span><span class="p">:</span> <span class="s2">"Action"</span><span class="p">)</span>
</span><span class='line'> <span class="n">vid</span> <span class="o">=</span> <span class="no">Video</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">title</span><span class="p">:</span> <span class="s2">"Yet another fake video"</span><span class="p">,</span> <span class="ss">description</span><span class="p">:</span> <span class="s2">"Not sure..."</span><span class="p">,</span> <span class="ss">category</span><span class="p">:</span> <span class="n">cat</span><span class="p">)</span>
</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="n">vid</span><span class="o">.</span><span class="n">category</span><span class="p">)</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="n">cat</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">it</span> <span class="s2">"does not save video without a title"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">video</span> <span class="o">=</span> <span class="no">Video</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">description</span><span class="p">:</span> <span class="s2">"I like this movie"</span><span class="p">)</span>
</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="no">Video</span><span class="o">.</span><span class="n">count</span><span class="p">)</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">it</span> <span class="s2">"does not save a video without a description"</span> <span class="k">do</span>
</span><span class='line'> <span class="n">video</span> <span class="o">=</span> <span class="no">Video</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">title</span><span class="p">:</span> <span class="s2">"Robocop"</span><span class="p">)</span>
</span><span class='line'> <span class="n">expect</span><span class="p">(</span><span class="no">Video</span><span class="o">.</span><span class="n">count</span><span class="p">)</span><span class="o">.</span><span class="n">to</span> <span class="n">eq</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Github Flow and Review Process]]></title>
<link href="http://alagram.github.io/blog/2014/08/28/github-flow-and-review-process/"/>
<updated>2014-08-28T09:33:00+00:00</updated>
<id>http://alagram.github.io/blog/2014/08/28/github-flow-and-review-process</id>
<content type="html"><![CDATA[<!-- more -->
<p><a href="http://git-scm.com/">Git</a> is an awesome version control system. For the uninitiated it might seem daunting at first, but with time it becomes indispensable. When you're working on a project by yourself, it's easy to keep track of things on <a href="https://github.com/">Github</a>. However, with time issues start cropping up when your project grows and you have a team of developers. Teams in such situations need an efficient way to work together. This aticle on <a href="http://scottchacon.com/2011/08/31/github-flow.html">Github Flow</a> is a great read on the topic. It describes, in my opinion, an organized workflow that allows for systematic collaboration and code review on projects. Here are my notes from the article:</p>
<h3 id="1.-anything-on-master-can-be-deployed">1. Anything on <code>master</code> can be deployed</h3>
<p>If anything is on the <code>master</code> branch, it's ready to be deployed. This means anything on <code>master</code> branch should be thoroughly tested and is ready to go to production.</p>
<h3 id="2.-create-descriptively-named-local-branch-off-master.">2. Create descriptively named local branch off <code>master</code>.</h3>
<p>When working on a new feature, one has to create a descriptively named local branch off <code>master</code>. People usually refer to this branch as a feature branch. Say you're working on a new feature for authentication, you'll typically do something like so:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="c"># make sure you're on your master branch</span>
</span><span class='line'>git checkout -b authentication
</span></code></pre></td></tr></table></div></figure>
<p>The above is the same as running:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>git branch authentication
</span><span class='line'>git checkout authentication
</span></code></pre></td></tr></table></div></figure>
<p>Using the <code>-b</code> option with <code>git checkout</code> creates the local branch and switches to it at the same time.</p>
<h3 id="3.-push-code-up">3. Push code up</h3>
<p>You'll work on this new feature branch, commit to the branch locally and regularly push to the same remote branch on your server. When you're ready to push up to your remote server, you'll enter this command on your terminal:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>git push origin authentication
</span></code></pre></td></tr></table></div></figure>
<p>This will create a new remote branch named <code>authentication</code> on the remote server.</p>
<h3 id="4.-create-a-pull-request">4. Create a pull request</h3>
<p>When you need feedback or assitance, or believe you work on the new feature is complete, open a <a href="https://help.github.com/articles/creating-a-pull-request">pull request</a>. You'll need to create this <code>pull request</code> from your feature branch to your <code>master</code> branch. When you're done, collaborators can review your commits.</p>
<h3 id="5.-code-review-and-merge">5. Code review and merge</h3>
<p>After all reviews have been made and changes effected, you'll want to merge it into your <code>master</code>.</p>
<p>Note that, while the <code>pull request</code> is open, you can still <code>commit</code> and <code>push</code> code up to your remote feature branch. This means any correction can be made while the <code>pull request</code> is open. For example:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>git add your_filename.rb
</span><span class='line'>git commit -m <span class="s2">"added extra validations"</span>
</span><span class='line'>git push origin authentication
</span></code></pre></td></tr></table></div></figure>
<p>Continue until the process is complete. When yoy're done, go to your <code>pull request</code> and click on the <code>merge</code> buton to <code>merge</code> it back to <code>master</code> branch.</p>
<h3 id="6.-pull-changes">6. Pull changes</h3>
<p>Now on your terminal, you'll switch to your <code>master</code> branch:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>git checkout master
</span></code></pre></td></tr></table></div></figure>
<p>Next, you <code>pull</code> the merged code on <a href="https://github.com/">Github</a> to your local <code>master</code> branch:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>git pull origin master
</span></code></pre></td></tr></table></div></figure>
<h3 id="7.-deploy">7. Deploy</h3>
<p>Once it's merged to <code>master</code>, you can deploy.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Seeding Data in Rails]]></title>
<link href="http://alagram.github.io/blog/2014/08/27/seeding-data/"/>
<updated>2014-08-27T16:43:00+00:00</updated>
<id>http://alagram.github.io/blog/2014/08/27/seeding-data</id>
<content type="html"><![CDATA[<!-- more -->
<p>Imagine you've finished building a feature with tests and want to play with it in the UI. You'll have to find a way to get data into your development database somehow. There are two ways of going about this:</p>
<ol>
<li><p>Fire up <code>rails console</code> and manually create all the data needed. This is fine and works when you want to create one-off data entries. Overtime you'll find that things become cumbersome when you need to add more data. What about when you want to fill up a development database over a network? Or when you accidentally lose all your development data and need to repopulate? Things can get out of hand quickly.</p></li>
<li><p>Populate your <code>seeds.rb</code> with all the data you need. This approach is your sure bet to get default values in your database in less time and is stress-free.</p></li>
</ol>
<p>Example seed file:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># seed.rb</span>
</span><span class='line'><span class="no">Video</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">title</span><span class="p">:</span> <span class="s2">"South Park"</span><span class="p">,</span> <span class="ss">description</span><span class="p">:</span> <span class="s2">"A very funny video"</span><span class="p">,</span> <span class="n">small_cover_url</span><span class="p">:</span> <span class="s2">"/tmp/south_park.jpg"</span><span class="p">,</span> <span class="n">large_cover_url</span><span class="p">:</span> <span class="s2">"/tmp/monk_large.jpg"</span><span class="p">)</span>
</span><span class='line'><span class="no">Video</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">title</span><span class="p">:</span> <span class="s2">"Futurama"</span><span class="p">,</span> <span class="ss">description</span><span class="p">:</span> <span class="s2">"I like this one too..."</span><span class="p">,</span> <span class="n">small_cover_url</span><span class="p">:</span> <span class="s2">"/tmp/futurama.jpg"</span><span class="p">,</span> <span class="n">large_cover_url</span><span class="p">:</span> <span class="s2">"/tmp/monk_large.jpg"</span><span class="p">)</span>
</span><span class='line'><span class="no">Video</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="ss">title</span><span class="p">:</span> <span class="s2">"Monk"</span><span class="p">,</span> <span class="ss">description</span><span class="p">:</span> <span class="s2">"Not bad. I'll watch this again."</span><span class="p">,</span> <span class="n">small_cover_url</span><span class="p">:</span> <span class="s2">"/tmp/monk.jpg"</span><span class="p">,</span> <span class="n">large_cover_url</span><span class="p">:</span> <span class="s2">"/tmp/monk_large.jpg"</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<p>After filling in all data you want to be created, you'll simply run the following command in your terminal:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>rake db:seed
</span></code></pre></td></tr></table></div></figure>
<p>That its all it takes. When things get out of hand, you can start on a clean slate by running this in your terminal:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>rake db:drop
</span><span class='line'>rake db:create
</span><span class='line'>rake db:migrate
</span><span class='line'>rake db:seed
</span></code></pre></td></tr></table></div></figure>
<p>This will first drop the development database, then re-create it, run all migrations and finally initialize with the seed data.</p>
<p>There is a simple <code>rake</code> task to complish all this in one fell swoop:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>rake db:reset
</span></code></pre></td></tr></table></div></figure>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Set Up Staging Environment for an App]]></title>
<link href="http://alagram.github.io/blog/2014/04/19/set-up-staging-environment-for-an-app/"/>
<updated>2014-04-19T10:21:00+00:00</updated>
<id>http://alagram.github.io/blog/2014/04/19/set-up-staging-environment-for-an-app</id>
<content type="html"><![CDATA[<!-- more -->
<p>Rails apps usually consist of test, development and production environments. We usually write test code to drive out implementation and make sure things work. However, you generally want to make sure that changes from developemt don't break things in production. For instance, developing on a Windows or Mac machine provides few guarantees that what works on development will work as you expect after deploying to production. This type of requirement calls for another eenvironment called staging. This environment should closely mirrow production. This blog post goes through how to set up a staging environment with a minimum of fuss.</p>
<h2 id="with-app-not-deployed-to-production">With App Not Deployed to Production</h2>
<p>If your app has not been pushed to production, i.e , it's sitting on your development machine, you can start at the top of this <a href="https://devcenter.heroku.com/articles/multiple-environments">document</a> and follow the simple steps to set up <code>staging</code> and <code>production</code> remote environments.</p>
<h2 id="with-app-already-deployed-to-production">With App Already Deployed to Production</h2>
<p>Alternatively, if your app is already in production, you need to create a copy from the production environment by using the <code>heroku fork</code> command. This <a href="https://devcenter.heroku.com/articles/fork-app">command</a> also copies add-ons, config vars and Heroku Potgres data. Suppose our production app is <code>googleclone</code>, we'll run</p>
<div class="highlight"><pre><code class="language-text" data-lang="text">heroku fork -a googleclone googleclone-staging
</code></pre></div>
<p>This will create a copy of googleclone called googleclone-clone</p>
<div class="highlight"><pre><code class="language-text" data-lang="text">Creating fork googleclone-staging... done
Copying slug... done
Adding heroku-postgresql:some-dev... done
Adding pgbackups:plus to googleclone... done
Adding pgbackups:plus to googleclone-staging... done
Transferring database (this can take some time)... done
Copying config vars... done
Fork complete, view it at http://googleclone-staging.herokuapp.com/
</code></pre></div>
<p>With this taken care of, we'll use the <a href="https://github.com/mattpolito/paratrooper">paratrooper gem</a> which provides simple rake tasks and other goodies for Heroku deployment. After the gem is installed, we'll need to create a simple <code>deploy.rake</code> file under <code>lib/tasks</code></p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># lib/tasks/deploy.rake</span>
</span><span class='line'>
</span><span class='line'><span class="nb">require</span> <span class="s1">'paratrooper'</span>
</span><span class='line'>
</span><span class='line'><span class="n">namespace</span> <span class="ss">:deploy</span> <span class="k">do</span>
</span><span class='line'> <span class="n">desc</span> <span class="s1">'Deploy app in staging environment'</span>
</span><span class='line'> <span class="n">task</span> <span class="ss">:staging</span> <span class="k">do</span>
</span><span class='line'> <span class="n">deployment</span> <span class="o">=</span> <span class="ss">Paratrooper</span><span class="p">:</span><span class="ss">:Deploy</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s2">"googleclone-staging"</span><span class="p">,</span> <span class="ss">tag</span><span class="p">:</span> <span class="s1">'staging'</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'> <span class="n">deployment</span><span class="o">.</span><span class="n">deploy</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="n">desc</span> <span class="s1">'Deploy app in production environment'</span>
</span><span class='line'> <span class="n">task</span> <span class="ss">:production</span> <span class="k">do</span>
</span><span class='line'> <span class="n">deployment</span> <span class="o">=</span> <span class="ss">Paratrooper</span><span class="p">:</span><span class="ss">:Deploy</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s2">"googleclone"</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">deploy</span><span class="o">|</span>
</span><span class='line'> <span class="n">deploy</span><span class="o">.</span><span class="n">tag</span> <span class="o">=</span> <span class="s1">'production'</span><span class="p">,</span>
</span><span class='line'> <span class="n">deploy</span><span class="o">.</span><span class="n">match_tag</span> <span class="o">=</span> <span class="s1">'staging'</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="n">deployment</span><span class="o">.</span><span class="n">deploy</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>Now we can call <code>rake deploy:staging</code> and <code>rake deploy:production</code> to deploy to <code>staging</code> and <code>production</code> environments respectively.</p>
<p>I hope you found this useful.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Full-Text Search on Hstore Column]]></title>
<link href="http://alagram.github.io/blog/2013/12/09/full-text-search-on-hstore-column/"/>
<updated>2013-12-09T19:27:00+00:00</updated>
<id>http://alagram.github.io/blog/2013/12/09/full-text-search-on-hstore-column</id>
<content type="html"><![CDATA[<!-- more -->
<p>I've come to realise that PostgreSQL is awesome and comes with amazing in-built full-text search features. Another wonderful feature of Postgres is its Hstore schema less key value store. What this does is basically allow us to store data like hashes into a column.</p>
<p>With this knowledge in place, we can query data based on keys and values as shown <a href="http://schneems.com/post/19298469372/you-got-nosql-in-my-postgres-using-hstore-in-rails">here</a>. This is fine and great but how does one search by value regardless of key? Using the examples in the above link won't cut it. Lets assume we're working on a Product model with hstore properties column. Initial solution I came up with involved creating a new column then added <code>before_save</code> and <code>before_update</code> callbacks to save the hash value into this column. It worked Ok but just didn't feel right.</p>
<p>Upon feather research, I found that to do full-text search on hstore I could cast the properties column to text, but in my case I had to cast the values to text. Ended up with one kickasss search method:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='sql'><span class='line'><span class="n">def</span> <span class="k">self</span><span class="p">.</span><span class="k">search</span><span class="p">(</span><span class="n">query</span><span class="p">)</span>
</span><span class='line'> <span class="n">conditons</span> <span class="o">=</span> <span class="o"><<-</span><span class="n">EOS</span>
</span><span class='line'> <span class="n">to_tsvector</span><span class="p">(</span><span class="s1">'english'</span><span class="p">,</span> <span class="k">CAST</span><span class="p">(</span><span class="n">avals</span><span class="p">(</span><span class="n">properties</span><span class="p">)</span> <span class="k">AS</span> <span class="nb">text</span><span class="p">))</span> <span class="o">@@</span> <span class="n">plainto_tsquery</span><span class="p">(</span><span class="s1">'english'</span><span class="p">,</span> <span class="o">#</span><span class="err">{</span><span class="n">sanitize</span><span class="p">(</span><span class="n">query</span><span class="p">)</span><span class="err">}</span><span class="p">)</span>
</span><span class='line'> <span class="n">EOS</span>
</span><span class='line'>
</span><span class='line'> <span class="k">where</span><span class="p">(</span><span class="n">conditons</span><span class="p">,</span> <span class="n">query</span><span class="p">).</span><span class="k">order</span><span class="p">(</span><span class="ss">"created_at DESC"</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<h3 id="references">References</h3>
<ul>
<li> <a href="http://community.gotealeaf.com/t/search-hstore-column/285">http://community.gotealeaf.com/t/search-hstore-column/285</a></li>
<li> <a href="http://terryrfinn.com/posts/1">http://terryrfinn.com/posts/1</a></li>
</ul>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Testing Multiple File Upload With Rspec, Fabrication and Carrierwave]]></title>
<link href="http://alagram.github.io/blog/2013/11/28/testing-multiple-file-upload-with-rspec-fabrication-and-carrierwave/"/>
<updated>2013-11-28T20:26:00+00:00</updated>
<id>http://alagram.github.io/blog/2013/11/28/testing-multiple-file-upload-with-rspec-fabrication-and-carrierwave</id>
<content type="html"><![CDATA[<!-- more -->
<p>I ran into some problems when testing controller code with Rpsec while generating objects with fabrication. Most tests failed after I set validation on an Image object.</p>
<p>My setup:</p>
<ul>
<li> fabrication (2.9.3)</li>
<li> rspec-rails (2.14.0)</li>
<li> faker (1.2.0)</li>
<li> ruby 2.0.0p247</li>
<li> OSX 10.9</li>
</ul>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Expense</span> <span class="o"><</span> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span>
</span><span class='line'> <span class="n">has_many</span> <span class="ss">:images</span>
</span><span class='line'> <span class="n">accepts_nested_attributes_for</span> <span class="ss">:images</span><span class="p">,</span> <span class="ss">:reject_if</span> <span class="o">=></span> <span class="nb">proc</span> <span class="p">{</span> <span class="o">|</span><span class="kp">attr</span><span class="o">|</span> <span class="kp">attr</span><span class="o">[</span><span class="s1">'image'</span><span class="o">].</span><span class="n">blank?</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">validate</span> <span class="ss">:must_have_image</span>
</span><span class='line'>
</span><span class='line'> <span class="kp">private</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">must_have_image</span>
</span><span class='line'> <span class="n">errors</span><span class="o">[</span><span class="ss">:base</span><span class="o">]</span> <span class="o"><<</span> <span class="s2">"Please upload image"</span> <span class="k">if</span> <span class="n">images_empty?</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">images_empty?</span>
</span><span class='line'> <span class="n">images</span><span class="o">.</span><span class="n">empty?</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Image</span> <span class="o"><</span> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span>
</span><span class='line'> <span class="n">belongs_to</span> <span class="ss">:expense</span>
</span><span class='line'>
</span><span class='line'> <span class="n">mount_uploader</span> <span class="ss">:image</span><span class="p">,</span> <span class="no">ImageUploader</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>With this, create an Image object first as shown <a href="https://github.com/carrierwaveuploader/carrierwave/wiki/How-to%3A-Use-test-fixtures">here</a>:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">Fabricator</span><span class="p">(</span><span class="ss">:image</span><span class="p">)</span> <span class="k">do</span>
</span><span class='line'> <span class="n">file</span> <span class="p">{</span>
</span><span class='line'> <span class="ss">ActionDispatch</span><span class="p">:</span><span class="ss">:Http</span><span class="o">::</span><span class="no">UploadedFile</span><span class="o">.</span><span class="n">new</span><span class="p">(</span>
</span><span class='line'> <span class="ss">:tempfile</span> <span class="o">=></span> <span class="no">File</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="no">Rails</span><span class="o">.</span><span class="n">root</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s2">"spec/support/uploads/file.jpg"</span><span class="p">)),</span>
</span><span class='line'> <span class="ss">:type</span> <span class="o">=></span> <span class="s1">'image/jpg'</span><span class="p">,</span>