cw_condition_merge 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gemtest ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'rake/testtask'
2
+ Rake::TestTask.new do |t|
3
+ t.libs << 'test'
4
+ end
5
+
6
+ desc "Run Tests"
7
+ task :default => :test
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/cw_condition_merge/cw_condition_merge.rb'
@@ -0,0 +1,67 @@
1
+ module CW
2
+
3
+ # Merge conditions
4
+ #
5
+ # This case has happened to me so frequently, I decided to write a function to perform this logic for me.
6
+ #
7
+ # Say you have an Active Record query you want to make. You normally want to find all the Widgets with customizations, but this time, you want to search for customizations that are pink:
8
+ #
9
+ # Widget.find(:all, :conditions => 'customized' )
10
+ # Widget.find(:all, :conditions => ['color = ?', 'pink'] )
11
+ #
12
+ # But you want to combine them:
13
+ # Widget.find(:all, :conditions => ['customized AND color = ?','pink'] )
14
+ #
15
+ # Looks simple if you're doing this all hard-coded style, but if you want more flexible models where you're using functions to generate your conditions and then chaining up conditions, as I often do, then you'll need something to combine or chain up those conditions
16
+ #
17
+ # Enter ConditionMerge. It handles the 3 big cases: You're merging into a set of missing conditions (no conditions or nil conditions), conditions specified only as a string, or conditions as an Array.
18
+ class ConditionMerge
19
+
20
+ def self.and( src, with )
21
+ merge( src, with, 'AND' )
22
+ end
23
+ def self.or( src, with )
24
+ merge( src, with, 'OR' )
25
+ end
26
+ def self.and_not( src, with )
27
+ merge( src, with, 'AND NOT' )
28
+ end
29
+ def self.or_not( src, with )
30
+ merge( src, with, 'OR NOT' )
31
+ end
32
+
33
+ def self.merge( src, with, join_clause )
34
+ if src.nil?
35
+ return with
36
+ end
37
+ if with.nil?
38
+ return src
39
+ end
40
+
41
+ raise ArgumentError.new("src can be nil, or a String or an Array. It cannot be a #{src.class.name}") unless src.is_a?(Array) or src.is_a?(String)
42
+ raise ArgumentError.new("with can be nil, or a String or an Array. It cannot be a #{with.class.name}") unless with.is_a?(Array) or with.is_a?(String)
43
+ raise ArgumentError.new("join_clause must be a String, not an #{join_clause.class.name}") unless join_clause.is_a?(String)
44
+
45
+ if src.is_a?(String)
46
+ if src.strip.empty?
47
+ # coerse to array for conformity
48
+ if with.is_a?(String)
49
+ return [with]
50
+ else
51
+ return with
52
+ end
53
+ end
54
+ src = [src]
55
+ end
56
+ if with.is_a?(String)
57
+ return src if with.strip.empty?
58
+ with = [with]
59
+ end
60
+
61
+ src[0] = '(' + src[0] + ') ' + join_clause + ' (' + with[0] + ')'
62
+ src.concat( with[1..-1] ) if with.size > 1
63
+ src
64
+ end
65
+
66
+ end # ConditionMerge
67
+ end # CW
@@ -0,0 +1,47 @@
1
+ require 'test/unit'
2
+ require 'cw_condition_merge'
3
+
4
+ class PaginationOrderTest < Test::Unit::TestCase
5
+ def setup
6
+ end
7
+ def teardown
8
+ end
9
+
10
+ def test_nil_src
11
+ assert_equal 'customized', CW::ConditionMerge.and( nil, 'customized' )
12
+ end
13
+ def test_nil_with
14
+ assert_equal 'customized', CW::ConditionMerge.and( 'customized', nil )
15
+ end
16
+
17
+ def test_and
18
+ assert_equal ['(customized) AND (color = ?)','pink'], CW::ConditionMerge.and( 'customized', ['color = ?','pink'] )
19
+ end
20
+ def test_or
21
+ assert_equal ['(customized) OR (color = ?)','pink'], CW::ConditionMerge.or( 'customized', ['color = ?','pink'] )
22
+ end
23
+ def test_and_not
24
+ assert_equal ['(customized) AND NOT (color = ?)','pink'], CW::ConditionMerge.and_not( 'customized', ['color = ?','pink'] )
25
+ end
26
+ def test_or_not
27
+ assert_equal ['(customized) OR NOT (color = ?)','pink'], CW::ConditionMerge.or_not( 'customized', ['color = ?','pink'] )
28
+ end
29
+
30
+ def test_empty_condition
31
+ assert_equal ['customized'], CW::ConditionMerge.and( 'customized', '' )
32
+ assert_equal ['customized'], CW::ConditionMerge.and( '', 'customized' )
33
+ end
34
+
35
+ def test_array_string
36
+ assert_equal ['(color = ?) AND (customized)','pink'], CW::ConditionMerge.and( ['color = ?','pink'], 'customized' )
37
+ end
38
+ def test_array_array
39
+ assert_equal ['(color = ?) AND (customized = ?)','pink','ye'], CW::ConditionMerge.and( ['color = ?','pink'], ['customized = ?','ye'] )
40
+ end
41
+
42
+ def test_bad_arguments
43
+ assert_raises( ArgumentError ) { CW::ConditionMerge.and( 3, 'customized' ) }
44
+ assert_raises( ArgumentError ) { CW::ConditionMerge.and( 'customized', 5 ) }
45
+ assert_raises( ArgumentError ) { CW::ConditionMerge.merge( ['color = ?','pink'], 'customized', ['clause'] ) }
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cw_condition_merge
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Christopher Wojno
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-17 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Allows one to "merge" condition hashes used in active record in a safe and uniform manner. Originally designed for Rails 2.3.14
22
+ email: cw_condition_merge.gem@wojnosystems.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - .gemtest
31
+ - lib/cw_condition_merge/cw_condition_merge.rb
32
+ - lib/cw_condition_merge.rb
33
+ - Rakefile
34
+ - test/test_cw_condition_merge.rb
35
+ homepage: http://www.wojnosystems.com
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ hash: 3
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.10
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Combine ActiveRecord condition hashes
68
+ test_files: []
69
+