Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Change existing and add new pipe commands

  • Loading branch information...
commit 783ef6a09eb0163c0d230a846103aeb3f7519db0 1 parent 9d2beca
nas authored

Showing 2 changed files with 292 additions and 15 deletions. Show diff stats Hide diff stats

  1. +28 15 lib/yql/query_builder.rb
  2. +264 0 spec/yql/query_builder_spec.rb
43 lib/yql/query_builder.rb
@@ -2,9 +2,10 @@ module Yql
2 2
3 3 class QueryBuilder
4 4
5   - attr_accessor :table, :conditions, :limit, :truncate, :sanitize_field, :select,
6   - :sort_field, :current_pipe_command_types, :sort_descending,
7   - :per_page, :current_page
  5 + attr_accessor :table, :conditions, :limit, :select, :sort_descending,
  6 + :current_pipe_command_types, :per_page, :current_page
  7 +
  8 + attr_reader :sort_field, :sanitize_field
8 9
9 10 def initialize(table, args = {})
10 11 @select = args[:select]
@@ -39,7 +40,7 @@ def find
39 40 self.limit = 1
40 41 "#{construct_query}"
41 42 end
42   -
  43 +
43 44 # Can be optionally passed a limit for limiting the number of records returned
44 45 # object.find_all(:limit => 10)
45 46 # #=> will return only 10 records
@@ -75,22 +76,15 @@ def conditions
75 76 return "where #{cond}"
76 77 end
77 78
78   - %w{sort tail truncate reverse unique sanitize}.each do |method|
  79 + %w{sort_field tail truncate reverse unique sanitize_field}.each do |method|
79 80 self.send(:define_method, "#{method}=") do |param|
80 81 instance_variable_set("@#{method}", param)
81   - current_pipe_command_types << method unless current_pipe_command_types.include?(method)
  82 + pipe_command_type = method.sub('_field', '')
  83 + current_pipe_command_types << pipe_command_type unless current_pipe_command_types.include?(pipe_command_type)
82 84 end
83 85 end
84 86
85 87 # Option can be piped
86   - # Sorts the result set according to the specified field (column) in the result set.
87   - def sort
88   - return unless @sort_field
89   - return "sort(field='#{@sort_field}')" unless @sort_descending
90   - return "sort(field='#{@sort_field}', descending='true')"
91   - end
92   -
93   - # Option can be piped
94 88 # Gets the last count items
95 89 def tail
96 90 return unless @tail
@@ -118,14 +112,33 @@ def unique
118 112 "unique(field='#{@unique}')"
119 113 end
120 114
  115 + # set sanitize to true if you want to sanitize all fields
  116 + def sanitize=(sanitize)
  117 + @sanitize = sanitize
  118 + @sanitize_field = nil
  119 + if @sanitize
  120 + current_pipe_command_types << 'sanitize' unless current_pipe_command_types.include?('sanitize')
  121 + else
  122 + remove_pipe_command('sanitize')
  123 + end
  124 + end
  125 +
121 126 # Option can be piped
122 127 # Sanitizes the output for HTML-safe rendering. To sanitize all returned fields, omit the field parameter.
123 128 def sanitize
124   - return unless @sanitize
  129 + return unless @sanitize || @sanitize_field
125 130 return "sanitize()" unless @sanitize_field
126 131 "sanitize(field='#{@sanitize_field}')"
127 132 end
128 133
  134 + # Option can be piped
  135 + # Sorts the result set according to the specified field (column) in the result set.
  136 + def sort
  137 + return unless @sort_field
  138 + return "sort(field='#{@sort_field}')" unless @sort_descending
  139 + return "sort(field='#{@sort_field}', descending='true')"
  140 + end
  141 +
129 142 # Its always advisable to order the pipe when there are more than one
130 143 # pipe commands available else unexpected results might get returned
131 144 # reorder_pipe_command {:from => 1, :to => 0}
264 spec/yql/query_builder_spec.rb
@@ -197,4 +197,268 @@
197 197
198 198 end
199 199
  200 + describe "#tail" do
  201 +
  202 + before(:each) do
  203 + @query_builder = Yql::QueryBuilder.new('yql.table.name')
  204 + end
  205 +
  206 + context "when tail pipe command is not set" do
  207 +
  208 + it "should return nothing" do
  209 + @query_builder.tail.should be_nil
  210 + end
  211 +
  212 + it "should not be present in the current pipe commands" do
  213 + @query_builder.current_pipe_command_types.should_not be_include('tail')
  214 + end
  215 +
  216 + end
  217 +
  218 + context "when tail pipe command is set" do
  219 +
  220 + before(:each) do
  221 + @query_builder.tail = 4
  222 + end
  223 +
  224 + it "should set the tail pipe command" do
  225 + @query_builder.tail.should eql("tail(count=4)")
  226 + end
  227 +
  228 + it "should get added to the current pipe commands" do
  229 + @query_builder.current_pipe_command_types.should be_include('tail')
  230 + end
  231 +
  232 + end
  233 +
  234 + end
  235 +
  236 + describe "#truncate" do
  237 +
  238 + before(:each) do
  239 + @query_builder = Yql::QueryBuilder.new('yql.table.name')
  240 + end
  241 +
  242 + context "when truncate pipe command is not set" do
  243 +
  244 + it "should return nothing" do
  245 + @query_builder.truncate.should be_nil
  246 + end
  247 +
  248 + it "should not be present in the current pipe commands" do
  249 + @query_builder.current_pipe_command_types.should_not be_include('truncate')
  250 + end
  251 +
  252 + end
  253 +
  254 + context "when truncate pipe command is set" do
  255 +
  256 + before(:each) do
  257 + @query_builder.truncate = 4
  258 + end
  259 +
  260 + it "should set the truncate pipe command" do
  261 + @query_builder.truncate.should eql("truncate(count=4)")
  262 + end
  263 +
  264 + it "should get added to the current pipe commands" do
  265 + @query_builder.current_pipe_command_types.should be_include('truncate')
  266 + end
  267 +
  268 + end
  269 +
  270 + end
  271 +
  272 + describe "#reverse" do
  273 +
  274 + before(:each) do
  275 + @query_builder = Yql::QueryBuilder.new('yql.table.name')
  276 + end
  277 +
  278 + context "when reverse pipe command is not set" do
  279 +
  280 + it "should return nothing" do
  281 + @query_builder.reverse.should be_nil
  282 + end
  283 +
  284 + it "should not be present in the current pipe commands" do
  285 + @query_builder.current_pipe_command_types.should_not be_include('reverse')
  286 + end
  287 +
  288 + end
  289 +
  290 + context "when reverse pipe command is set" do
  291 +
  292 + before(:each) do
  293 + @query_builder.reverse = true
  294 + end
  295 +
  296 + it "should set the reverse pipe command" do
  297 + @query_builder.reverse.should eql('reverse()')
  298 + end
  299 +
  300 + it "should get added to the current pipe commands" do
  301 + @query_builder.current_pipe_command_types.should be_include('reverse')
  302 + end
  303 +
  304 + end
  305 +
  306 + end
  307 +
  308 + describe "#unique" do
  309 +
  310 + before(:each) do
  311 + @query_builder = Yql::QueryBuilder.new('yql.table.name')
  312 + end
  313 +
  314 + context "when unique pipe command is not set" do
  315 +
  316 + it "should return nothing" do
  317 + @query_builder.unique.should be_nil
  318 + end
  319 +
  320 + it "should not be present in the current pipe commands" do
  321 + @query_builder.current_pipe_command_types.should_not be_include('unique')
  322 + end
  323 +
  324 + end
  325 +
  326 + context "when unique pipe command is set" do
  327 +
  328 + before(:each) do
  329 + @query_builder.unique = 'field_name'
  330 + end
  331 +
  332 + it "should set the unique pipe command" do
  333 + @query_builder.unique.should eql("unique(field='field_name')")
  334 + end
  335 +
  336 + it "should get added to the current pipe commands" do
  337 + @query_builder.current_pipe_command_types.should be_include('unique')
  338 + end
  339 +
  340 + end
  341 +
  342 + end
  343 +
  344 + describe "#sort" do
  345 +
  346 + before(:each) do
  347 + @query_builder = Yql::QueryBuilder.new('yql.table.name')
  348 + end
  349 +
  350 + context "when sort field is not set" do
  351 +
  352 + it "should return nothing" do
  353 + @query_builder.sort.should be_nil
  354 + end
  355 +
  356 + it "should not be present in the current pipe commands" do
  357 + @query_builder.current_pipe_command_types.should_not be_include('sort')
  358 + end
  359 +
  360 + end
  361 +
  362 + context "when sort field pipe command is set" do
  363 +
  364 + before(:each) do
  365 + @query_builder.sort_field = 'field_name'
  366 + end
  367 +
  368 + context "when sort descending is not set" do
  369 + it "should set the unique pipe command" do
  370 + @query_builder.sort.should eql("sort(field='field_name')")
  371 + end
  372 + end
  373 +
  374 + context "when sort descending is set" do
  375 + it "should set the unique pipe command" do
  376 + @query_builder.sort_descending = true
  377 + @query_builder.sort.should eql("sort(field='field_name', descending='true')")
  378 + end
  379 + end
  380 +
  381 + it "should get added to the current pipe commands" do
  382 + @query_builder.current_pipe_command_types.should be_include('sort')
  383 + end
  384 +
  385 + end
  386 +
  387 + end
  388 +
  389 + describe "#sanitize" do
  390 +
  391 + before(:each) do
  392 + @query_builder = Yql::QueryBuilder.new('yql.table.name')
  393 + end
  394 +
  395 + context "when sanitize field is not set" do
  396 +
  397 + it "should return nothing" do
  398 + @query_builder.sanitize.should be_nil
  399 + end
  400 +
  401 + it "should not be present in the current pipe commands" do
  402 + @query_builder.current_pipe_command_types.should_not be_include('sanitize')
  403 + end
  404 +
  405 + end
  406 +
  407 + context "when sanitize pipe command is set" do
  408 +
  409 + context "and it is set to true" do
  410 +
  411 + before(:each) do
  412 + @query_builder.sanitize = true
  413 + end
  414 +
  415 + it "should set the sanitizie pipe command" do
  416 + @query_builder.sanitize.should eql("sanitize()")
  417 + end
  418 +
  419 + it "should get added to the current pipe commands" do
  420 + @query_builder.current_pipe_command_types.should be_include('sanitize')
  421 + end
  422 +
  423 + end
  424 +
  425 + context "when sanitize_field is set" do
  426 +
  427 + before(:each) do
  428 + @query_builder.sanitize_field = 'field_name'
  429 + end
  430 +
  431 + it "should set the sanitize pipe command" do
  432 + @query_builder.sanitize.should eql("sanitize(field='field_name')")
  433 + end
  434 +
  435 + it "should get added to the current pipe commands" do
  436 + @query_builder.current_pipe_command_types.should be_include('sanitize')
  437 + end
  438 +
  439 + end
  440 +
  441 + end
  442 +
  443 + end
  444 +
  445 + describe "#remove_pipe_command" do
  446 +
  447 + before(:each) do
  448 + @query_builder = Yql::QueryBuilder.new('yql.table.name')
  449 + @query_builder.stub!(:current_pipe_command_types).and_return(['sort', 'sanitize', 'truncate'])
  450 + end
  451 +
  452 + it "should remove the pipe command that is not present" do
  453 + @query_builder.remove_pipe_command('not_present')
  454 + @query_builder.current_pipe_command_types.should eql(['sort', 'sanitize', 'truncate'])
  455 + end
  456 +
  457 + it "should remove the pipe command that is present" do
  458 + @query_builder.remove_pipe_command('sort')
  459 + @query_builder.current_pipe_command_types.should eql(['sanitize', 'truncate'])
  460 + end
  461 +
  462 + end
  463 +
200 464 end

0 comments on commit 783ef6a

Please sign in to comment.
Something went wrong with that request. Please try again.