Permalink
Browse files

Change existing and add new pipe commands

  • Loading branch information...
1 parent 9d2beca commit 783ef6a09eb0163c0d230a846103aeb3f7519db0 nas committed Jul 5, 2010
Showing with 292 additions and 15 deletions.
  1. +28 −15 lib/yql/query_builder.rb
  2. +264 −0 spec/yql/query_builder_spec.rb
View
@@ -2,9 +2,10 @@ module Yql
class QueryBuilder
- attr_accessor :table, :conditions, :limit, :truncate, :sanitize_field, :select,
- :sort_field, :current_pipe_command_types, :sort_descending,
- :per_page, :current_page
+ attr_accessor :table, :conditions, :limit, :select, :sort_descending,
+ :current_pipe_command_types, :per_page, :current_page
+
+ attr_reader :sort_field, :sanitize_field
def initialize(table, args = {})
@select = args[:select]
@@ -39,7 +40,7 @@ def find
self.limit = 1
"#{construct_query}"
end
-
+
# Can be optionally passed a limit for limiting the number of records returned
# object.find_all(:limit => 10)
# #=> will return only 10 records
@@ -75,22 +76,15 @@ def conditions
return "where #{cond}"
end
- %w{sort tail truncate reverse unique sanitize}.each do |method|
+ %w{sort_field tail truncate reverse unique sanitize_field}.each do |method|
self.send(:define_method, "#{method}=") do |param|
instance_variable_set("@#{method}", param)
- current_pipe_command_types << method unless current_pipe_command_types.include?(method)
+ pipe_command_type = method.sub('_field', '')
+ current_pipe_command_types << pipe_command_type unless current_pipe_command_types.include?(pipe_command_type)
end
end
# Option can be piped
- # Sorts the result set according to the specified field (column) in the result set.
- def sort
- return unless @sort_field
- return "sort(field='#{@sort_field}')" unless @sort_descending
- return "sort(field='#{@sort_field}', descending='true')"
- end
-
- # Option can be piped
# Gets the last count items
def tail
return unless @tail
@@ -118,14 +112,33 @@ def unique
"unique(field='#{@unique}')"
end
+ # set sanitize to true if you want to sanitize all fields
+ def sanitize=(sanitize)
+ @sanitize = sanitize
+ @sanitize_field = nil
+ if @sanitize
+ current_pipe_command_types << 'sanitize' unless current_pipe_command_types.include?('sanitize')
+ else
+ remove_pipe_command('sanitize')
+ end
+ end
+
# Option can be piped
# Sanitizes the output for HTML-safe rendering. To sanitize all returned fields, omit the field parameter.
def sanitize
- return unless @sanitize
+ return unless @sanitize || @sanitize_field
return "sanitize()" unless @sanitize_field
"sanitize(field='#{@sanitize_field}')"
end
+ # Option can be piped
+ # Sorts the result set according to the specified field (column) in the result set.
+ def sort
+ return unless @sort_field
+ return "sort(field='#{@sort_field}')" unless @sort_descending
+ return "sort(field='#{@sort_field}', descending='true')"
+ end
+
# Its always advisable to order the pipe when there are more than one
# pipe commands available else unexpected results might get returned
# reorder_pipe_command {:from => 1, :to => 0}
@@ -197,4 +197,268 @@
end
+ describe "#tail" do
+
+ before(:each) do
+ @query_builder = Yql::QueryBuilder.new('yql.table.name')
+ end
+
+ context "when tail pipe command is not set" do
+
+ it "should return nothing" do
+ @query_builder.tail.should be_nil
+ end
+
+ it "should not be present in the current pipe commands" do
+ @query_builder.current_pipe_command_types.should_not be_include('tail')
+ end
+
+ end
+
+ context "when tail pipe command is set" do
+
+ before(:each) do
+ @query_builder.tail = 4
+ end
+
+ it "should set the tail pipe command" do
+ @query_builder.tail.should eql("tail(count=4)")
+ end
+
+ it "should get added to the current pipe commands" do
+ @query_builder.current_pipe_command_types.should be_include('tail')
+ end
+
+ end
+
+ end
+
+ describe "#truncate" do
+
+ before(:each) do
+ @query_builder = Yql::QueryBuilder.new('yql.table.name')
+ end
+
+ context "when truncate pipe command is not set" do
+
+ it "should return nothing" do
+ @query_builder.truncate.should be_nil
+ end
+
+ it "should not be present in the current pipe commands" do
+ @query_builder.current_pipe_command_types.should_not be_include('truncate')
+ end
+
+ end
+
+ context "when truncate pipe command is set" do
+
+ before(:each) do
+ @query_builder.truncate = 4
+ end
+
+ it "should set the truncate pipe command" do
+ @query_builder.truncate.should eql("truncate(count=4)")
+ end
+
+ it "should get added to the current pipe commands" do
+ @query_builder.current_pipe_command_types.should be_include('truncate')
+ end
+
+ end
+
+ end
+
+ describe "#reverse" do
+
+ before(:each) do
+ @query_builder = Yql::QueryBuilder.new('yql.table.name')
+ end
+
+ context "when reverse pipe command is not set" do
+
+ it "should return nothing" do
+ @query_builder.reverse.should be_nil
+ end
+
+ it "should not be present in the current pipe commands" do
+ @query_builder.current_pipe_command_types.should_not be_include('reverse')
+ end
+
+ end
+
+ context "when reverse pipe command is set" do
+
+ before(:each) do
+ @query_builder.reverse = true
+ end
+
+ it "should set the reverse pipe command" do
+ @query_builder.reverse.should eql('reverse()')
+ end
+
+ it "should get added to the current pipe commands" do
+ @query_builder.current_pipe_command_types.should be_include('reverse')
+ end
+
+ end
+
+ end
+
+ describe "#unique" do
+
+ before(:each) do
+ @query_builder = Yql::QueryBuilder.new('yql.table.name')
+ end
+
+ context "when unique pipe command is not set" do
+
+ it "should return nothing" do
+ @query_builder.unique.should be_nil
+ end
+
+ it "should not be present in the current pipe commands" do
+ @query_builder.current_pipe_command_types.should_not be_include('unique')
+ end
+
+ end
+
+ context "when unique pipe command is set" do
+
+ before(:each) do
+ @query_builder.unique = 'field_name'
+ end
+
+ it "should set the unique pipe command" do
+ @query_builder.unique.should eql("unique(field='field_name')")
+ end
+
+ it "should get added to the current pipe commands" do
+ @query_builder.current_pipe_command_types.should be_include('unique')
+ end
+
+ end
+
+ end
+
+ describe "#sort" do
+
+ before(:each) do
+ @query_builder = Yql::QueryBuilder.new('yql.table.name')
+ end
+
+ context "when sort field is not set" do
+
+ it "should return nothing" do
+ @query_builder.sort.should be_nil
+ end
+
+ it "should not be present in the current pipe commands" do
+ @query_builder.current_pipe_command_types.should_not be_include('sort')
+ end
+
+ end
+
+ context "when sort field pipe command is set" do
+
+ before(:each) do
+ @query_builder.sort_field = 'field_name'
+ end
+
+ context "when sort descending is not set" do
+ it "should set the unique pipe command" do
+ @query_builder.sort.should eql("sort(field='field_name')")
+ end
+ end
+
+ context "when sort descending is set" do
+ it "should set the unique pipe command" do
+ @query_builder.sort_descending = true
+ @query_builder.sort.should eql("sort(field='field_name', descending='true')")
+ end
+ end
+
+ it "should get added to the current pipe commands" do
+ @query_builder.current_pipe_command_types.should be_include('sort')
+ end
+
+ end
+
+ end
+
+ describe "#sanitize" do
+
+ before(:each) do
+ @query_builder = Yql::QueryBuilder.new('yql.table.name')
+ end
+
+ context "when sanitize field is not set" do
+
+ it "should return nothing" do
+ @query_builder.sanitize.should be_nil
+ end
+
+ it "should not be present in the current pipe commands" do
+ @query_builder.current_pipe_command_types.should_not be_include('sanitize')
+ end
+
+ end
+
+ context "when sanitize pipe command is set" do
+
+ context "and it is set to true" do
+
+ before(:each) do
+ @query_builder.sanitize = true
+ end
+
+ it "should set the sanitizie pipe command" do
+ @query_builder.sanitize.should eql("sanitize()")
+ end
+
+ it "should get added to the current pipe commands" do
+ @query_builder.current_pipe_command_types.should be_include('sanitize')
+ end
+
+ end
+
+ context "when sanitize_field is set" do
+
+ before(:each) do
+ @query_builder.sanitize_field = 'field_name'
+ end
+
+ it "should set the sanitize pipe command" do
+ @query_builder.sanitize.should eql("sanitize(field='field_name')")
+ end
+
+ it "should get added to the current pipe commands" do
+ @query_builder.current_pipe_command_types.should be_include('sanitize')
+ end
+
+ end
+
+ end
+
+ end
+
+ describe "#remove_pipe_command" do
+
+ before(:each) do
+ @query_builder = Yql::QueryBuilder.new('yql.table.name')
+ @query_builder.stub!(:current_pipe_command_types).and_return(['sort', 'sanitize', 'truncate'])
+ end
+
+ it "should remove the pipe command that is not present" do
+ @query_builder.remove_pipe_command('not_present')
+ @query_builder.current_pipe_command_types.should eql(['sort', 'sanitize', 'truncate'])
+ end
+
+ it "should remove the pipe command that is present" do
+ @query_builder.remove_pipe_command('sort')
+ @query_builder.current_pipe_command_types.should eql(['sanitize', 'truncate'])
+ end
+
+ end
+
end

0 comments on commit 783ef6a

Please sign in to comment.