aub-record_filter 0.9.4 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 9
4
- :patch: 4
4
+ :patch: 5
@@ -15,7 +15,7 @@ module RecordFilter
15
15
  dsl_conjunction.steps.each do |step|
16
16
  case step
17
17
  when DSL::Restriction
18
- result.add_restriction(step.column, step.operator, step.value, :negated => step.negated)
18
+ handle_restriction_step(step, result, table, true)
19
19
  when DSL::Conjunction
20
20
  result.add_conjunction(create_from(step, table))
21
21
  when DSL::Join
@@ -50,7 +50,6 @@ module RecordFilter
50
50
  check_column_exists!(column_name)
51
51
  restriction = RestrictionFactory.build(operator, "#{@table_name}.#{column_name}", value, options)
52
52
  self << restriction
53
- restriction
54
53
  end
55
54
 
56
55
  def add_conjunction(conjunction)
@@ -128,6 +127,17 @@ module RecordFilter
128
127
  raise ColumnNotFoundException.new("The column #{column_name} was not found in #{@table.table_name}.")
129
128
  end
130
129
  end
130
+
131
+ def self.handle_restriction_step(step, conjunction, table, follow_conjunctions=true)
132
+ if ((cr = step.conjuncted_restriction) && follow_conjunctions)
133
+ restriction_conjunction = create_from(DSL::Conjunction.new(nil, step.conjuncted_restriction_type), table)
134
+ handle_restriction_step(step, restriction_conjunction, table, false)
135
+ handle_restriction_step(cr, restriction_conjunction, table, true)
136
+ conjunction.add_conjunction(restriction_conjunction)
137
+ else
138
+ conjunction.add_restriction(step.column, step.operator, step.value, :negated => step.negated)
139
+ end
140
+ end
131
141
  end
132
142
 
133
143
  class AnyOf < Base
@@ -23,6 +23,7 @@ module RecordFilter
23
23
  class Restriction
24
24
 
25
25
  attr_reader :column, :negated, :operator, :value # :nodoc:
26
+ attr_reader :conjuncted_restriction_type, :conjuncted_restriction # :nodoc:
26
27
 
27
28
  DEFAULT_VALUE = Object.new
28
29
 
@@ -47,6 +48,7 @@ module RecordFilter
47
48
  # @public
48
49
  def initialize(column, value=DEFAULT_VALUE) # :nodoc:
49
50
  @column, @negated, @operator = column, false, nil
51
+ @conjuncted_restriction, @conjuncted_restriction_type = nil, nil
50
52
  take_value(value)
51
53
  end
52
54
 
@@ -258,6 +260,46 @@ module RecordFilter
258
260
  self
259
261
  end
260
262
 
263
+ # Create a secondary OR restriction that will be conjuncted with the current one. This
264
+ # function allows you to easily create restrictions on one line that would otherwise
265
+ # require the use of any_of and is intended as a space-saver.
266
+ #
267
+ # ==== Example
268
+ #
269
+ # with(:expired_at).greater_than(Time.now).or.is_null
270
+ #
271
+ # # conditions => ['expired_at > ? OR expired_at IS NULL', Time.now]
272
+ #
273
+ # ==== Returns
274
+ # Restriction::
275
+ # A new restriction object that this one will be OR'ed with.
276
+ #
277
+ # @public
278
+ def or
279
+ @conjuncted_restriction_type = :any_of
280
+ @conjuncted_restriction = Restriction.new(@column)
281
+ end
282
+
283
+ # Create a secondary AND restriction that will be conjuncted with the current one. This
284
+ # function allows you to easily create restrictions on one line that would otherwise
285
+ # require the use of all_of and is intended as a space-saver.
286
+ #
287
+ # ==== Example
288
+ #
289
+ # with(:expired_at).greater_than(Time.now).and.less_than(3.days.from_now)
290
+ #
291
+ # # conditions => ['expired_at > ? AND expired_at < ?', Time.now, 3.days.from_now]
292
+ #
293
+ # ==== Returns
294
+ # Restriction::
295
+ # A new restriction object that this one will be OR'ed with.
296
+ #
297
+ # @public
298
+ def and
299
+ @conjuncted_restriction_type = :all_of
300
+ @conjuncted_restriction = Restriction.new(@column)
301
+ end
302
+
261
303
  protected
262
304
 
263
305
  def take_value(value) # :nodoc:
@@ -214,4 +214,25 @@ describe 'RecordFilter restrictions' do
214
214
  end.inspect
215
215
  Post.last_find.should == { :conditions => [%q("posts".permalink <> ?), 'Post'] }
216
216
  end
217
+
218
+ it 'should work correctly for one-line ORs with is_null' do
219
+ Post.filter do
220
+ with(:permalink, 'abc').or.is_null
221
+ end.inspect
222
+ Post.last_find.should == { :conditions => [%q(("posts".permalink = ?) OR ("posts".permalink IS NULL)), 'abc'] }
223
+ end
224
+
225
+ it 'should work correctly for one-line ANDs with is_null' do
226
+ Post.filter do
227
+ with(:id).gt(56).and.lt(200)
228
+ end.inspect
229
+ Post.last_find.should == { :conditions => [%q(("posts".id > ?) AND ("posts".id < ?)), 56, 200] }
230
+ end
231
+
232
+ it 'should work properly with multiple one-line conjunctions' do
233
+ Post.filter do
234
+ with(:id).gt(56).and.lt(200).or.gt(500).or.lt(15)
235
+ end.inspect
236
+ Post.last_find.should == { :conditions => [%q(("posts".id > ?) AND (("posts".id < ?) OR (("posts".id > ?) OR ("posts".id < ?)))), 56, 200, 500, 15] }
237
+ end
217
238
  end
data/spec/test.db CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aub-record_filter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4
4
+ version: 0.9.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aubrey Holland
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-05-09 00:00:00 -07:00
13
+ date: 2009-05-11 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -66,7 +66,7 @@ files:
66
66
  - spec/test.db
67
67
  - test/performance_test.rb
68
68
  - test/test.db
69
- has_rdoc: false
69
+ has_rdoc: true
70
70
  homepage: http://github.com/aub/record_filter/tree/master
71
71
  post_install_message:
72
72
  rdoc_options: