omnis 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/omnis/query.rb CHANGED
@@ -4,7 +4,7 @@ module Omnis
4
4
  module Query
5
5
 
6
6
  class Param
7
- attr_reader :name, :operator
7
+ attr_reader :name, :operator, :opts
8
8
  def initialize(name, operator, opts={}, &extractor)
9
9
  # raise ArgumentError("operator must be a descendant of Omnis::Operators::NullOperator") unless operator.is_a? Omnis::Operators::NullOperator
10
10
  extractor ||= ->params { params[name] }
@@ -19,7 +19,8 @@ module Omnis
19
19
 
20
20
  # extracts the value for a param, using the extractor lamba or the default value
21
21
  def extract(params)
22
- value = @extractor.(params) || default
22
+ value = @extractor.(params) if params.keys.include? name # only run extractor if the param was in the request (params)
23
+ value ||= default # if there is a default, apply it
23
24
  return value if value.is_a? Omnis::Operators::NullOperator
24
25
  return @operator.new(@name, value, @opts) unless value.nil?
25
26
  end
data/lib/omnis/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Omnis
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -49,17 +49,26 @@ describe Omnis::MongoQuery do
49
49
 
50
50
  fields %w[ref_anixe status]
51
51
  # if this param is in the query, fetch the field "ref_customer"
52
- param :ref_customer, Matches, :field => "ref_customer"
52
+ param :ref_customer, Matches, :field => 'ref_customer'
53
+ param(:date_booked, Between, :field => 'date_booked') {|src| Between.new(:date_booked, Time.at(0)..Time.at(1), :field => 'date_booked') }
53
54
  end
54
55
 
55
56
  it 'extra field not requested when param not present' do
56
- t = TestFieldsQuery.new({})
57
- t.to_mongo.opts.should == { :limit => 20, :skip => 0, :fields => ['ref_anixe', 'status']}
57
+ m = TestFieldsQuery.new({}).to_mongo
58
+ m.selector.should == {}
59
+ m.opts.should == { :limit => 20, :skip => 0, :fields => ['ref_anixe', 'status']}
58
60
  end
59
61
 
60
62
  it 'extra field is requested when param is in request' do
61
- t = TestFieldsQuery.new({"ref_customer" => "123"})
62
- t.to_mongo.opts.should == { :limit => 20, :skip => 0, :fields => ['ref_anixe', 'status', 'ref_customer']}
63
+ m = TestFieldsQuery.new({"ref_customer" => "123"}).to_mongo
64
+ m.selector.should == {:ref_customer => /123/i}
65
+ m.opts.should == { :limit => 20, :skip => 0, :fields => ['ref_anixe', 'status', 'ref_customer']}
66
+ end
67
+
68
+ it 'works with lambdas, but the lambda must provide the opts!' do
69
+ m = TestFieldsQuery.new({"date_booked" => "1NOV"}).to_mongo
70
+ m.selector
71
+ m.opts.should == { :limit => 20, :skip => 0, :fields => ['ref_anixe', 'status', 'date_booked']}
63
72
  end
64
73
  end
65
74
 
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.4.0
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-18 00:00:00.000000000 Z
12
+ date: 2012-11-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport