yaml_conditions 0.0.0.1 → 0.0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -24,11 +24,11 @@ Below I summarized some samples so you can see how I use it.
24
24
 
25
25
  Company.find(:first, :yaml_conditions => { :data => { { :street => '5551 LEOPARD ST' }, :status => :active }, :conditions => [ 'branches_counter > ?', 3] )
26
26
 
27
- As you can see from the last sample, both yaml_conditions and conditions will be merged in order to find the expected results.
27
+ As you can see on this sample, both yaml_conditions and conditions will be merged in order to find the expected results.
28
28
 
29
29
  Company.find(:last, :yaml_conditions => { :data => { :user => { :last_name => 'Marcelo', :address => { :city => Yaml::NotSerializedField.new('Rio Negro') } } } })
30
30
 
31
- On this last sample, the city attribute can be traversed on memory but it is not saved on the serialized field (i.e. handler), so we want to indicate that to avoid building an invalid SQL.
31
+ On this last sample, the city attribute will be used to filter the objects on memory but it is not used to build the SQL query. This can be handy if we want to filter based on a field (i.e. city) that is not serialized within the yaml object within the database, but can be traversed through messaging between objects.
32
32
 
33
33
  BTW: All methods ActiveRecord::Base#{last, first, all} rely on ActiveRecord::Base#find, so we can use yaml_conditions with these methods too. As an example:
34
34
  Company.all(:yaml_conditions => { :data => { :address => { :street => '5551 LEAOPARD ST' } } }) behaves the same way as the first sample explained below.
@@ -43,11 +43,11 @@ So, I added some custom magic for filtering Delayed::Job objects, like this:
43
43
 
44
44
  Delayed::Job.first(:yaml_conditions => { :handler => { :args => [ { :user => { :last_name => 'Marcelo', :address => { :city => 'Rio Negro' } } } ] } })
45
45
 
46
- As you probably realize, '*' is interpreted (for Delayed::Job instances) as a wildcard.
46
+ As you probably realize, '*' is interpreted as a wildcard.
47
47
 
48
48
  == Installation
49
49
 
50
- $ ruby script/plugin install git://github.com/marklazz/yaml_conditions.git)
50
+ $ ruby script/plugin install git://github.com/marklazz/yaml_conditions.git
51
51
 
52
52
  OR install it as a gem
53
53
 
data/Rakefile CHANGED
@@ -3,7 +3,7 @@ require 'echoe'
3
3
 
4
4
  # PACKAGING ============================================================
5
5
 
6
- Echoe.new('yaml_conditions', '0.0.0.1') do |p|
6
+ Echoe.new('yaml_conditions', '0.0.0.2') do |p|
7
7
  p.description = ''
8
8
  p.url = 'http://github.com/marklazz/yaml_conditions'
9
9
  p.author = 'Marcelo Giorgi'
data/TESTS.rdoc CHANGED
@@ -1,4 +1,4 @@
1
- Before running the tests you should create a user yaml_conditions for mysql (and for postgresql too if you want to run them there).
1
+ Before running the tests, you must create a user called yaml_conditions (with no password) for mysql (and/or postgresql).
2
2
 
3
3
  Then to run the suite you should be able to do:
4
4
 
@@ -7,6 +7,7 @@ rake spec (which will run both suites for mysql and postgres)
7
7
  Or run them individually:
8
8
 
9
9
  rake mysql:spec
10
+
10
11
  rake postgres:spec
11
12
 
12
13
  Enjoy!
@@ -14,13 +14,14 @@ module Orms
14
14
 
15
15
  CLASS_STRING_FORMAT = /^CLASS\:([A-Z][\w\:]+)$/
16
16
  AR_STRING_FORMAT = /^AR\:([A-Z][\w\:]+)\:(\d+)$/
17
+ LOAD_AR_STRING_FORMAT = /^LOAD;([A-Z][\w\:]+);(\d+)$/
17
18
 
18
19
  def __delayed_job_ar_to_string_(obj)
19
- "AR:#{obj.class}:#{obj.id}"
20
+ Delayed::PerformableMethod.new(obj, :to_s, []).object
20
21
  end
21
22
 
22
23
  def __delayed_job_class_to_string_(obj)
23
- "CLASS:#{obj.name}"
24
+ Delayed::PerformableMethod.new(obj, :to_s, []).object
24
25
  end
25
26
 
26
27
  def __serialize__to_yaml_value__(arg)
@@ -35,6 +36,7 @@ module Orms
35
36
  case arg
36
37
  when CLASS_STRING_FORMAT then $1.constantize
37
38
  when AR_STRING_FORMAT then $1.constantize.find($2)
39
+ when LOAD_AR_STRING_FORMAT then $1.constantize.find($2)
38
40
  else super(arg)
39
41
  end
40
42
  end
@@ -144,10 +144,11 @@ describe Orms::ActiveRecordVersion2 do
144
144
  struct_instance = @struct.new(:new_email, @user.to_yaml, 'foo@gmail.com', 4, 0.005)
145
145
  @job = Job.create(:name => 'job1', :data => struct_instance)
146
146
  @yaml_conditions = {}
147
+ @selector = :first
147
148
  @conditions = {}
148
149
  end
149
150
 
150
- subject { Job.find(:first, :yaml_conditions => @yaml_conditions, :conditions => @conditions) }
151
+ subject { Job.find(@selector, :yaml_conditions => @yaml_conditions, :conditions => @conditions) }
151
152
 
152
153
  context 'filter for the proper class but wrong float' do
153
154
  before do
@@ -281,13 +282,13 @@ describe Orms::ActiveRecordVersion2 do
281
282
  before do
282
283
  period = Period.create(:year => 2000)
283
284
  user_data = UserData.create(:social_number => 123, :title => 'Computer Engineer', :period => period)
284
- user = User.create(:name => 'marcelo', :details => user_data)
285
- @complex_job = Job.create(:name => 'job1', :data => { :owner => user })
285
+ user = User.create(:name => 'marklazz', :details => user_data)
286
+ @complex_job = Job.create(:name => 'cpmplex_job', :data => { :owner => user })
286
287
  end
287
288
 
288
289
  context 'query using correct social_number' do
289
290
  before do
290
- @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marcelo', :details => { :social_number => 123 } } } }
291
+ @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marklazz', :details => { :social_number => 123 } } } }
291
292
  end
292
293
 
293
294
  it { should == @complex_job }
@@ -295,7 +296,7 @@ describe Orms::ActiveRecordVersion2 do
295
296
 
296
297
  context 'query using correct social_number' do
297
298
  before do
298
- @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marcelo', :details => { :social_number => 124 } } } }
299
+ @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marklazz', :details => { :social_number => 124 } } } }
299
300
  end
300
301
 
301
302
  it { should be_nil }
@@ -303,7 +304,7 @@ describe Orms::ActiveRecordVersion2 do
303
304
 
304
305
  context 'query using the UserData class' do
305
306
  before do
306
- @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marcelo', :details => { :class => UserData } } } }
307
+ @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marklazz', :details => { :class => UserData } } } }
307
308
  end
308
309
 
309
310
  it { should == @complex_job }
@@ -311,7 +312,7 @@ describe Orms::ActiveRecordVersion2 do
311
312
 
312
313
  context 'query using the UserData class and title and social_number' do
313
314
  before do
314
- @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marcelo', :details => { :class => UserData, :title => 'Computer Engineer' } } } }
315
+ @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marklazz', :details => { :class => UserData, :title => 'Computer Engineer' } } } }
315
316
  end
316
317
 
317
318
  it { should == @complex_job }
@@ -319,7 +320,7 @@ describe Orms::ActiveRecordVersion2 do
319
320
 
320
321
  context 'query using the UserData class, social_number and wrong title' do
321
322
  before do
322
- @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marcelo', :details => { :class => UserData, :title => 'Teacher' } } } }
323
+ @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marklazz', :details => { :class => UserData, :title => 'Teacher' } } } }
323
324
  end
324
325
 
325
326
  it { should be_nil }
@@ -327,15 +328,85 @@ describe Orms::ActiveRecordVersion2 do
327
328
 
328
329
  context 'query using the UserData class and title and social_number' do
329
330
  before do
330
- @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marcelo', :details => { :class => UserData, :title => 'Computer Engineer', :period => { :class => Period, :year => 2000 } } } } }
331
+ @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marklazz', :details => { :class => UserData, :title => 'Computer Engineer', :period => { :class => Period, :year => 2000 } } } } }
331
332
  end
332
333
 
333
334
  it { should == @complex_job }
334
335
  end
335
336
 
336
- context 'query using NotSerializedField' do
337
+ context "query using wrong value on a NotSerializedField" do
338
+ before do
339
+ @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marklazz', :details => { :class => UserData, :title => 'Computer Engineer', :period => { :class => Period, :year => Yaml::NotSerializedField.new(1542) } } } } }
340
+ end
341
+
342
+ it { should be_nil }
343
+
344
+ it 'should not include the NotSerializedField in the generated sql' do
345
+ Job.__build_yaml_conditions__(@yaml_conditions).should_not include('city')
346
+ Job.__build_yaml_conditions__(@yaml_conditions).should_not include('1542')
347
+ end
348
+
349
+ it 'should include the standard fields in the generated sql' do
350
+ Job.__build_yaml_conditions__(@yaml_conditions).should include('name')
351
+ Job.__build_yaml_conditions__(@yaml_conditions).should include('marklazz')
352
+ end
353
+ end
354
+
355
+ context "query using NotSerializedField for an attribute not present on yaml's column" do
356
+ before do
357
+ @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marklazz', :details => { :class => UserData, :title => 'Computer Engineer', :period => { :class => Period, :year => Yaml::NotSerializedField.new(2000) } } } } }
358
+ end
359
+
360
+ it { should == @complex_job }
361
+
362
+ it 'should not include the NotSerializedField in the generated sql' do
363
+ Job.__build_yaml_conditions__(@yaml_conditions).should_not include('2000')
364
+ Job.__build_yaml_conditions__(@yaml_conditions).should_not include('city')
365
+ end
366
+
367
+ it 'should include the standard fields in the generated sql' do
368
+ Job.__build_yaml_conditions__(@yaml_conditions).should include('marklazz')
369
+ Job.__build_yaml_conditions__(@yaml_conditions).should include('name')
370
+ end
371
+ end
372
+
373
+ context "query using NotSerializedField for an attribute present on yaml's column" do
374
+ before do
375
+ @yaml_conditions = { :data => { :class => Hash, :owner => { :name => Yaml::NotSerializedField.new('marklazz'), :details => { :class => UserData, :title => 'Computer Engineer' } } } }
376
+ end
377
+
378
+ it { should == @complex_job }
379
+
380
+ it 'should not include the NotSerializedField in the generated sql' do
381
+ Job.__build_yaml_conditions__(@yaml_conditions).should_not include('name')
382
+ Job.__build_yaml_conditions__(@yaml_conditions).should_not include('marklazz')
383
+ end
384
+ end
385
+
386
+ context "query using wildcard and on nested structure" do
387
+ before do
388
+ 2.times do
389
+ user_data = UserData.create(:social_number => rand(100), :title => 'Computer Engineer')
390
+ user = User.create(:name => "marklazz_#{rand(100)}", :details => user_data)
391
+ job = Job.create(:name => "cpmplex_job_#{rand(100)}", :data => { :owner => user })
392
+ end
393
+ user_data = UserData.create(:social_number => rand(100), :title => 'Teacher')
394
+ user = User.create(:name => "marklazz_#{rand(100)}", :details => user_data)
395
+ job = Job.create(:name => "cpmplex_job_#{rand(100)}", :data => { :owner => user })
396
+ @selector = :all
397
+ @yaml_conditions = { :data => { :class => Hash, :owner => { :name => '*', :details => { :class => UserData, :title => 'Computer Engineer' } } } }
398
+ end
399
+
400
+ it 'should have a total of 5 job' do
401
+ Job.count.should == 5
402
+ end
403
+
404
+ its(:length) { should == 3 }
405
+ end
406
+
407
+ context "query using wildcard and NotSerializedField" do
337
408
  before do
338
- @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marcelo', :details => { :class => UserData, :title => 'Computer Engineer', :period => { :class => Period, :year => Yaml::NotSerializedField.new(2000) } } } } }
409
+ @yaml_conditions = { :data => { :class => Hash, :owner => { :name => 'marklazz', :details => { :class => UserData, :title => 'Computer Engineer', :period => { :class => Period, :year => '*' } } } } }
339
410
  end
340
411
 
341
412
  it { should == @complex_job }
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{yaml_conditions}
5
- s.version = "0.0.0.1"
5
+ s.version = "0.0.0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Marcelo Giorgi"]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml_conditions
3
3
  version: !ruby/object:Gem::Version
4
- hash: 77
4
+ hash: 75
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
9
  - 0
10
- - 1
11
- version: 0.0.0.1
10
+ - 2
11
+ version: 0.0.0.2
12
12
  platform: ruby
13
13
  authors:
14
14
  - Marcelo Giorgi