itrigga-param_fu 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.1.0
@@ -32,6 +32,25 @@ module Trigga
32
32
  raise ArgumentError.new( "at least one of the arguments #{keys.inspect} is required" ) if present.empty?
33
33
  return present
34
34
  end
35
+
36
+ def id_or_name_condition(table, val)
37
+ condition = {:where => [], :values=>[]}
38
+ unless val.to_s.empty?
39
+ unless val.match(/[^0-9]/)
40
+ condition[:where] << "#{table}.id = ?"
41
+ condition[:values] << val.to_i
42
+ else
43
+ condition[:where] << "#{table}.name LIKE ?"
44
+ condition[:values] << "#{'%' + val.gsub(/\(\d+\)/,"") + '%'}"
45
+ end
46
+ end
47
+ condition
48
+ end
49
+
50
+ # fallback when we don't have ActiveSupport's pluralize method available
51
+ def to_plural(s)
52
+ (s.match(/[aeiou]$/i) ? s + 's' : s + 'es' )
53
+ end
35
54
  end
36
55
 
37
56
  module InstanceMethods
@@ -100,6 +100,41 @@ describe ::Trigga::ParamFu do
100
100
  end
101
101
  end
102
102
  end
103
+
104
+ describe "id_or_name_condition" do
105
+
106
+ it "should return a hash" do
107
+ Tester.id_or_name_condition('mymodel', 'string_value').should be_a Hash
108
+ end
109
+
110
+ it "should have a key for :where" do
111
+ Tester.id_or_name_condition('mymodel', 'string_value').keys.should include(:where)
112
+ end
113
+
114
+ it "should have a key for :values" do
115
+ Tester.id_or_name_condition('mymodel', 'string_value').keys.should include(:values)
116
+ end
117
+
118
+ context "when given a string value" do
119
+ it "should include '(table).name like ?' in the where clause" do
120
+ Tester.id_or_name_condition('mymodel', 'string_value')[:where][0].should =~ /mymodel\.name\s*like\s*\?/i
121
+ end
122
+
123
+ it "should include the given value in values, wrapped in wildcard markers" do
124
+ Tester.id_or_name_condition('mymodel', 'string_value')[:values][0].should == '%string_value%'
125
+ end
126
+ end
127
+ context "when given an integer value" do
128
+
129
+ it "should include '(table).id == ?' in the where clause" do
130
+ Tester.id_or_name_condition('mymodel', '2')[:where][0].should =~ /mymodel\.id\s*=\s*\?/i
131
+ end
132
+
133
+ it "should include the given value in values, as an integer" do
134
+ Tester.id_or_name_condition('mymodel', '2')[:values][0].should == 2
135
+ end
136
+ end
137
+ end
103
138
  end
104
139
 
105
140
  describe "instance_methods" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: itrigga-param_fu
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease: false
4
+ hash: 27
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 1
10
- version: 0.0.1
9
+ - 0
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Al Davidson
@@ -16,12 +16,13 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-10-07 00:00:00 +01:00
19
+ date: 2011-10-19 00:00:00 +01:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
23
23
  prerelease: false
24
24
  name: rspec
25
+ type: :development
25
26
  version_requirements: &id001 !ruby/object:Gem::Requirement
26
27
  none: false
27
28
  requirements:
@@ -34,10 +35,10 @@ dependencies:
34
35
  - 0
35
36
  version: 1.3.0
36
37
  requirement: *id001
37
- type: :development
38
38
  - !ruby/object:Gem::Dependency
39
39
  prerelease: false
40
40
  name: rspec-rails
41
+ type: :development
41
42
  version_requirements: &id002 !ruby/object:Gem::Requirement
42
43
  none: false
43
44
  requirements:
@@ -50,10 +51,10 @@ dependencies:
50
51
  - 2
51
52
  version: 1.3.2
52
53
  requirement: *id002
53
- type: :development
54
54
  - !ruby/object:Gem::Dependency
55
55
  prerelease: false
56
56
  name: bundler
57
+ type: :development
57
58
  version_requirements: &id003 !ruby/object:Gem::Requirement
58
59
  none: false
59
60
  requirements:
@@ -66,10 +67,10 @@ dependencies:
66
67
  - 0
67
68
  version: 1.0.0
68
69
  requirement: *id003
69
- type: :development
70
70
  - !ruby/object:Gem::Dependency
71
71
  prerelease: false
72
72
  name: jeweler
73
+ type: :development
73
74
  version_requirements: &id004 !ruby/object:Gem::Requirement
74
75
  none: false
75
76
  requirements:
@@ -82,10 +83,10 @@ dependencies:
82
83
  - 4
83
84
  version: 1.6.4
84
85
  requirement: *id004
85
- type: :development
86
86
  - !ruby/object:Gem::Dependency
87
87
  prerelease: false
88
88
  name: rcov
89
+ type: :development
89
90
  version_requirements: &id005 !ruby/object:Gem::Requirement
90
91
  none: false
91
92
  requirements:
@@ -96,7 +97,6 @@ dependencies:
96
97
  - 0
97
98
  version: "0"
98
99
  requirement: *id005
99
- type: :development
100
100
  description: Validates parameters to methods are present and are the required types
101
101
  email: support@itrigga.com
102
102
  executables: []
@@ -149,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
149
  requirements: []
150
150
 
151
151
  rubyforge_project:
152
- rubygems_version: 1.3.7
152
+ rubygems_version: 1.4.2
153
153
  signing_key:
154
154
  specification_version: 3
155
155
  summary: Validates parameters to methods