ar_query 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
File without changes
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ # Include hook code here
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
data/lib/ar_query.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'delegate'
2
+
3
+ class ARQuery < Hash
4
+ attr_accessor :bind_vars, :boolean_join
5
+ attr_reader :condition_sqls
6
+
7
+ def initialize(initial_values={})
8
+ super nil
9
+ initial_values.each do |k,v| self[k] = v; end
10
+ @bind_vars = []
11
+ @condition_sqls = ConditionSQLs.new
12
+ @boolean_join = :and
13
+ end
14
+
15
+ def []( key )
16
+ if (key == :conditions) && !@condition_sqls.empty?
17
+ join_str = @boolean_join == :and ? ' AND ' : ' OR '
18
+ condition_sql =
19
+ @condition_sqls.map { |c_sql| "(#{c_sql})" }.join(join_str)
20
+ @bind_vars.empty? ? condition_sql : [ condition_sql, *@bind_vars ]
21
+ else
22
+ super
23
+ end
24
+ end
25
+
26
+ class ConditionSQLs < Array
27
+ def <<(elt)
28
+ if elt.is_a?(String)
29
+ super
30
+ else
31
+ raise(
32
+ ArgumentError,
33
+ "Tried appending #{elt.inspect} to ARQuery#condition_sqls: Only strings are allowed"
34
+ )
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,108 @@
1
+ require File.dirname(__FILE__) + '/../lib/ar_query'
2
+
3
+ describe ARQuery do
4
+ it 'should be a kind of Hash' do
5
+ @ar_query = ARQuery.new
6
+ # This is required because ActiveRecord.find uses args.extract_options!
7
+ @ar_query.is_a?(::Hash).should be_true
8
+ end
9
+
10
+ describe '#initialize with no values' do
11
+ before :all do
12
+ @ar_query = ARQuery.new
13
+ end
14
+
15
+ it 'should not have conditions' do
16
+ @ar_query[:conditions].should be_nil
17
+ end
18
+ end
19
+
20
+ describe '#initialize with values' do
21
+ before :all do
22
+ @ar_query = ARQuery.new(:order => 'id desc', :limit => 25)
23
+ end
24
+
25
+ it 'should have those values in the hash' do
26
+ @ar_query[:order].should == 'id desc'
27
+ @ar_query[:limit].should == 25
28
+ end
29
+
30
+ it 'should not have other values in the hash' do
31
+ @ar_query[:conditions].should be_nil
32
+ end
33
+ end
34
+
35
+ describe "#condition_sqls <<" do
36
+ before :all do
37
+ @ar_query = ARQuery.new
38
+ @ar_query.condition_sqls << "fname is not null"
39
+ @ar_query.condition_sqls << "lname is not null"
40
+ end
41
+
42
+ it 'should join the conditions with an AND' do
43
+ @ar_query[:conditions].should ==
44
+ "(fname is not null) AND (lname is not null)"
45
+ end
46
+
47
+ it "should prevent you from appending nil" do
48
+ lambda { @ar_query.condition_sqls << nil }.should raise_error(
49
+ ArgumentError,
50
+ "Tried appending nil to ARQuery#condition_sqls: Only strings are allowed"
51
+ )
52
+ end
53
+
54
+ it "should prevent you from appending a value besides a string" do
55
+ lambda { @ar_query.condition_sqls << 55 }.should raise_error(
56
+ ArgumentError,
57
+ "Tried appending 55 to ARQuery#condition_sqls: Only strings are allowed"
58
+ )
59
+ end
60
+ end
61
+
62
+ describe '#condition_sqls << with OR as the boolean join' do
63
+ before :all do
64
+ @ar_query = ARQuery.new
65
+ @ar_query.boolean_join = :or
66
+ @ar_query.condition_sqls << "fname is not null"
67
+ @ar_query.condition_sqls << "lname is not null"
68
+ end
69
+
70
+ it 'should join the conditions with an OR' do
71
+ @ar_query[:conditions].should ==
72
+ "(fname is not null) OR (lname is not null)"
73
+ end
74
+ end
75
+
76
+ describe '[:conditions]' do
77
+ describe 'with bind vars' do
78
+ before :all do
79
+ @ar_query = ARQuery.new
80
+ @ar_query.condition_sqls << "fname = ?"
81
+ @ar_query.condition_sqls << "lname = ?"
82
+ end
83
+
84
+ describe 'using appends' do
85
+ before :all do
86
+ @ar_query.bind_vars << 'Francis'
87
+ @ar_query.bind_vars << 'Hwang'
88
+ end
89
+
90
+ it 'should put the bind_vars at the end of the conditions array' do
91
+ @ar_query[:conditions].should ==
92
+ [ "(fname = ?) AND (lname = ?)", 'Francis', 'Hwang' ]
93
+ end
94
+ end
95
+
96
+ describe 'using assignment' do
97
+ before :all do
98
+ @ar_query.bind_vars = %w( Francis Hwang )
99
+ end
100
+
101
+ it 'should put the bind_vars at the end of the conditions array' do
102
+ @ar_query[:conditions].should ==
103
+ [ "(fname = ?) AND (lname = ?)", 'Francis', 'Hwang' ]
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
File without changes
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ar_query
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Francis Hwang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-19 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A utility class for building options for ActiveRecord.find.
17
+ email: sera@fhwang.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/ar_query.rb
26
+ - MIT-LICENSE
27
+ - README
28
+ - init.rb
29
+ - install.rb
30
+ - uninstall.rb
31
+ - spec/ar_query_spec.rb
32
+ - tasks/ar_query_tasks.rake
33
+ has_rdoc: false
34
+ homepage: http://github.com/fhwang/ar_query
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.3.1
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: A utility class for building options for ActiveRecord.find.
59
+ test_files: []
60
+