ripplr 0.0.7.beta → 0.0.8.beta

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -33,6 +33,7 @@ And then create, index and search for your documents like so:
33
33
  ```ruby
34
34
  todays_wod = Wod.create :description => 'Lawnmower 8x35lbs. Sqt Jumps 10x. Lunge Twist 20x10lbs ...', :performed_at = Time.now
35
35
  todays_wod.index #add the index for your document
36
- Wod.search(:description, "jumps") # Search WODs for descriptions that contain jump, returns an array of matching WODs
36
+ criteria = Wod.where(:description => "jumps") # Builds a criteria object
37
+ # Iterating over a criteria object, calling count(size, or length), or calling to_a executes the search
37
38
  ```
38
39
 
@@ -1,5 +1,6 @@
1
1
  require 'yaml'
2
2
  require 'ripplr/queryable'
3
+ require 'ripplr/null_object'
3
4
  require 'ripplr/query_field'
4
5
  require 'ripplr/indexers'
5
6
  require 'ripplr/criteria'
@@ -11,6 +11,21 @@ module Ripplr
11
11
  self
12
12
  end
13
13
 
14
+ def order_by(field)
15
+ @order_by_field = @target.queryable_field(field)
16
+ self
17
+ end
18
+
19
+ def ascending
20
+ @order_by_direction = " asc"
21
+ self
22
+ end
23
+
24
+ def descending
25
+ @order_by_direction = " desc"
26
+ self
27
+ end
28
+
14
29
  def each(&block)
15
30
  results.each do |result|
16
31
  yield result
@@ -30,7 +45,7 @@ module Ripplr
30
45
  def execute
31
46
  return @target.list if condition.nil?
32
47
 
33
- @indexer.search @target, query
48
+ @indexer.search @target, query, options
34
49
  end
35
50
 
36
51
  def conditions
@@ -43,6 +58,17 @@ module Ripplr
43
58
  @results
44
59
  end
45
60
 
61
+ def options
62
+ Maybe(ordering) { Hash.new }
63
+ end
64
+
65
+ def ordering
66
+ return NullObject.new if @order_by_field.nil?
67
+ sort = { :sort => "#{@order_by_field.to_s}" }
68
+ sort[:sort] += @order_by_direction unless @order_by_direction.nil?
69
+ sort
70
+ end
71
+
46
72
  def condition
47
73
  @condition
48
74
  end
@@ -0,0 +1,20 @@
1
+ class NullObject
2
+ def initialize
3
+ @origin = caller.first ### SETS ORIGIN FOR INSPECT INFO
4
+ end
5
+
6
+ def method_missing(*args, &block)
7
+ self
8
+ end
9
+
10
+ def nil?; true; end
11
+ end
12
+
13
+
14
+ def Maybe(value)
15
+ if value.nil? then
16
+ block_given? ? yield : NullObject.new
17
+ else
18
+ value
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module Ripplr
2
- VERSION = "0.0.7.beta"
2
+ VERSION = "0.0.8.beta"
3
3
  end
@@ -10,14 +10,14 @@ describe Ripplr::Criteria do
10
10
 
11
11
  describe "searching with a query against a field" do
12
12
  Given (:indexer) { mock }
13
- Given { indexer.should_receive(:search).with(Person,"first_name_text: \"skrillex\"").and_return ["Awesome"] }
13
+ Given { indexer.should_receive(:search).with(Person,"first_name_text: \"skrillex\"", {}).and_return ["Awesome"] }
14
14
  When (:results) { Ripplr::Criteria.new(Person, indexer).where(:first_name => "skrillex").execute }
15
15
  Then { results.should == ["Awesome"] }
16
16
  end
17
17
 
18
18
  describe "searching with a query against a different field" do
19
19
  Given (:indexer) { mock }
20
- Given { indexer.should_receive(:search).with(Person,"last_name_text: \"Auerbach\"").and_return ["Dan"] }
20
+ Given { indexer.should_receive(:search).with(Person,"last_name_text: \"Auerbach\"", {}).and_return ["Dan"] }
21
21
  When (:results) { Ripplr::Criteria.new(Person, indexer).where(:last_name => "Auerbach").execute }
22
22
  Then { results.should == ["Dan"] }
23
23
  end
@@ -29,7 +29,7 @@ describe Ripplr::Criteria do
29
29
  describe "treating a criteria object like a collection executes the query" do
30
30
  Given (:criteria) { Ripplr::Criteria.new(Person, indexer).where(:first_name => "Dan") }
31
31
  Given (:indexer) { mock }
32
- Given { indexer.should_receive(:search).with(Person,"first_name_text: \"Dan\"").once.and_return(["Dan"]) }
32
+ Given { indexer.should_receive(:search).with(Person,"first_name_text: \"Dan\"", {}).once.and_return(["Dan"]) }
33
33
 
34
34
  context "by calling #each" do
35
35
  Given (:iterated) { Array.new }
@@ -61,4 +61,40 @@ describe Ripplr::Criteria do
61
61
  end
62
62
  end
63
63
 
64
+ describe "adding a sort to a query" do
65
+ Given (:indexer) { mock }
66
+ Given (:criteria) { Ripplr::Criteria.new(Person, indexer).where(:first_name => 'Patrick') }
67
+
68
+ describe "is reflective" do
69
+ Then { criteria.order_by(:last_name).should == criteria }
70
+ end
71
+
72
+ context "when sorting by a queryable field" do
73
+ Given { indexer.should_receive(:search).with(Person, "first_name_text: \"Patrick\"", :sort => "created_at_dt").and_return [1,2,3] }
74
+ When(:result) { criteria.order_by(:created_at) }
75
+ Then { criteria.execute.should == [1,2,3] }
76
+ end
77
+
78
+ context "when sorting by a non queryable field" do
79
+ Then { expect { criteria.order_by(:junk_field) }.to raise_error RuntimeError }
80
+ end
81
+
82
+ context "when sorting in descending order" do
83
+ Given { indexer.should_receive(:search).with(Person, "first_name_text: \"Patrick\"", :sort => "created_at_dt desc").and_return [3,2,1] }
84
+ When(:result) { criteria.order_by(:created_at).descending }
85
+ Then { criteria.execute.should == [3,2,1] }
86
+ end
87
+
88
+ context "when sorting in ascending order (force default)" do
89
+ Given { indexer.should_receive(:search).with(Person, "first_name_text: \"Patrick\"", :sort => "created_at_dt asc").and_return [1,2,3] }
90
+ When(:result) { criteria.order_by(:created_at).ascending }
91
+ Then { criteria.execute.should == [1,2,3] }
92
+ end
93
+
94
+ context "when trying to confuse the sort direction" do
95
+ Given { indexer.should_receive(:search).with(Person, "first_name_text: \"Patrick\"", :sort => "created_at_dt desc").and_return [3,2,1] }
96
+ When(:result) { criteria.order_by(:created_at).ascending.descending }
97
+ Then { criteria.execute.should == [3,2,1] }
98
+ end
99
+ end
64
100
  end
@@ -2,9 +2,11 @@ class Person
2
2
  include Ripple::Document
3
3
  property :first_name, String
4
4
  property :last_name, String
5
+ timestamps!
5
6
 
6
7
  include Ripplr::Queryable
7
8
  queryable do
8
9
  text :first_name, :last_name
10
+ time :created_at
9
11
  end
10
12
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ripplr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7.beta
4
+ version: 0.0.8.beta
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -79,6 +79,7 @@ files:
79
79
  - lib/ripplr/indexers/ripple.rb
80
80
  - lib/ripplr/indexers.rb
81
81
  - lib/ripplr/locale/en.yml
82
+ - lib/ripplr/null_object.rb
82
83
  - lib/ripplr/query_field.rb
83
84
  - lib/ripplr/queryable.rb
84
85
  - lib/ripplr/translation.rb