fhwang-ar_query 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,25 @@
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 = []
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
+ end
@@ -0,0 +1,94 @@
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
+ end
47
+
48
+ describe '#condition_sqls << with OR as the boolean join' do
49
+ before :all do
50
+ @ar_query = ARQuery.new
51
+ @ar_query.boolean_join = :or
52
+ @ar_query.condition_sqls << "fname is not null"
53
+ @ar_query.condition_sqls << "lname is not null"
54
+ end
55
+
56
+ it 'should join the conditions with an OR' do
57
+ @ar_query[:conditions].should ==
58
+ "(fname is not null) OR (lname is not null)"
59
+ end
60
+ end
61
+
62
+ describe '[:conditions]' do
63
+ describe 'with bind vars' do
64
+ before :all do
65
+ @ar_query = ARQuery.new
66
+ @ar_query.condition_sqls << "fname = ?"
67
+ @ar_query.condition_sqls << "lname = ?"
68
+ end
69
+
70
+ describe 'using appends' do
71
+ before :all do
72
+ @ar_query.bind_vars << 'Francis'
73
+ @ar_query.bind_vars << 'Hwang'
74
+ end
75
+
76
+ it 'should put the bind_vars at the end of the conditions array' do
77
+ @ar_query[:conditions].should ==
78
+ [ "(fname = ?) AND (lname = ?)", 'Francis', 'Hwang' ]
79
+ end
80
+ end
81
+
82
+ describe 'using assignment' do
83
+ before :all do
84
+ @ar_query.bind_vars = %w( Francis Hwang )
85
+ end
86
+
87
+ it 'should put the bind_vars at the end of the conditions array' do
88
+ @ar_query[:conditions].should ==
89
+ [ "(fname = ?) AND (lname = ?)", 'Francis', 'Hwang' ]
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
File without changes
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fhwang-ar_query
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francis Hwang
@@ -22,7 +22,17 @@ extensions: []
22
22
  extra_rdoc_files: []
23
23
 
24
24
  files:
25
- - ./lib/ar_query.rb
25
+ - init.rb
26
+ - install.rb
27
+ - lib
28
+ - lib/ar_query.rb
29
+ - MIT-LICENSE
30
+ - README
31
+ - spec
32
+ - spec/ar_query_spec.rb
33
+ - tasks
34
+ - tasks/ar_query_tasks.rake
35
+ - uninstall.rb
26
36
  has_rdoc: false
27
37
  homepage: http://github.com/fhwang/ar_query/tree/master
28
38
  post_install_message: