omnis 0.6.1 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
- extracted_params = extract
35
- OpenStruct.new({ :selector => mongo_selector(extracted_params),
36
- :opts => mongo_opts(extracted_params)})
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 - 1
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(extracted_params)
69
- Hash[extracted_params.map { |operator| [operator.key, mongo_operator(operator)] }]
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(extracted_params)
73
- params_with_extra_fields = extracted_params.collect(&:opts).select {|e| e.has_key? :field}
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
@@ -66,6 +66,7 @@ module Omnis
66
66
  end
67
67
 
68
68
  # extracts operators from params
69
+ # add the name of the param to each extracted operators
69
70
  def extract
70
71
  self.class.params.map do |k,v|
71
72
  v.extract(@input_params).tap do |operator|
@@ -1,3 +1,3 @@
1
1
  module Omnis
2
- VERSION = "0.6.1"
2
+ VERSION = "0.6.3"
3
3
  end
@@ -32,15 +32,16 @@ describe Omnis::MongoQuery do
32
32
  end
33
33
 
34
34
  it "works altogether" do
35
- t = TestIntegrationQuery.new("ref_anixe" => "1abc", "contract" => "test", "product" => "HOT", "page" => "2", "date" => "2012-10-12")
36
- t.to_mongo.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
- }
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
- t.to_mongo.opts.should == { :limit => 20, :skip => 20, :fields => fields}
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 => 20, :fields => []}
92
+ t.to_mongo.opts.should == { :limit => 20, :skip => 40, :fields => []}
92
93
  end
93
94
  end
94
95
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omnis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.6.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: