omnis 0.6.1 → 0.6.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/omnis/mongo_query.rb +15 -8
- data/lib/omnis/query.rb +1 -0
- data/lib/omnis/version.rb +1 -1
- data/spec/lib/omnis/mongo_query_spec.rb +9 -8
- metadata +1 -1
data/lib/omnis/mongo_query.rb
CHANGED
@@ -30,16 +30,19 @@ module Omnis
|
|
30
30
|
module InstanceMethods
|
31
31
|
include Omnis::Operators
|
32
32
|
|
33
|
+
attr_reader :extracted_operators
|
34
|
+
|
33
35
|
def to_mongo
|
34
|
-
|
35
|
-
OpenStruct.new({ :selector
|
36
|
-
:opts
|
36
|
+
extracted_operators = extract
|
37
|
+
OpenStruct.new({ :selector => mongo_selector(extracted_operators),
|
38
|
+
:opts => mongo_opts(extracted_operators),
|
39
|
+
:param_names => extracted_param_names(extracted_operators)})
|
37
40
|
end
|
38
41
|
|
39
42
|
private
|
40
43
|
def page
|
41
44
|
return 0 unless @input_params.has_key? page_param_name
|
42
|
-
page_num = @input_params[page_param_name].to_i
|
45
|
+
page_num = @input_params[page_param_name].to_i
|
43
46
|
return 0 if page_num < 0
|
44
47
|
return page_num
|
45
48
|
end
|
@@ -65,16 +68,20 @@ module Omnis
|
|
65
68
|
end
|
66
69
|
end
|
67
70
|
|
68
|
-
def mongo_selector(
|
69
|
-
Hash[
|
71
|
+
def mongo_selector(extracted_operators)
|
72
|
+
Hash[extracted_operators.map { |operator| [operator.key, mongo_operator(operator)] }]
|
70
73
|
end
|
71
74
|
|
72
|
-
def mongo_opts(
|
73
|
-
params_with_extra_fields =
|
75
|
+
def mongo_opts(extracted_operators)
|
76
|
+
params_with_extra_fields = extracted_operators.collect(&:opts).select {|e| e.has_key? :field}
|
74
77
|
extra_fields = params_with_extra_fields.map {|e| e[:field]}
|
75
78
|
fields = self.class.field_list + extra_fields
|
76
79
|
{ :limit => items_per_page, :skip => skip, :fields => fields }
|
77
80
|
end
|
81
|
+
|
82
|
+
def extracted_param_names(extracted_operators)
|
83
|
+
extracted_operators.map {|e| e.opts[:param_name] }
|
84
|
+
end
|
78
85
|
end
|
79
86
|
end
|
80
87
|
end
|
data/lib/omnis/query.rb
CHANGED
data/lib/omnis/version.rb
CHANGED
@@ -32,15 +32,16 @@ describe Omnis::MongoQuery do
|
|
32
32
|
end
|
33
33
|
|
34
34
|
it "works altogether" do
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
35
|
+
m = TestIntegrationQuery.new("ref_anixe" => "1abc", "contract" => "test", "product" => "HOT", "page" => "2", "date" => "2012-10-12").to_mongo
|
36
|
+
m.selector.should == { :ref_anixe => "1abc",
|
37
|
+
:contract => /test/i,
|
38
|
+
:product => /^HOT/i,
|
39
|
+
:date => { :'$gte' => Time.local(2012, 10, 12, 0, 0, 0), :'$lt' => Time.local(2012, 10, 12, 23, 59, 59, 999999.999)}
|
40
|
+
}
|
41
41
|
|
42
42
|
fields = %w[ref_anixe contract description status product agency passengers date_status_modified services]
|
43
|
-
|
43
|
+
m.opts.should == { :limit => 20, :skip => 40, :fields => fields}
|
44
|
+
m.param_names.should == [:ref_anixe, :date, :contract, :product]
|
44
45
|
end
|
45
46
|
|
46
47
|
context 'fields' do
|
@@ -88,7 +89,7 @@ describe Omnis::MongoQuery do
|
|
88
89
|
end
|
89
90
|
it "page defaults and page given" do
|
90
91
|
t = TestPageDefaultQuery.new({"page" => 2})
|
91
|
-
t.to_mongo.opts.should == { :limit => 20, :skip =>
|
92
|
+
t.to_mongo.opts.should == { :limit => 20, :skip => 40, :fields => []}
|
92
93
|
end
|
93
94
|
end
|
94
95
|
|