Browse files

Adding support for regex values

  • Loading branch information...
1 parent 1bfffe2 commit b96587df997e9f41a044667d8e41541c29c8f498 @vicentemundim committed Jul 22, 2010
Showing with 16 additions and 2 deletions.
  1. +7 −1 lib/mongoid_query_string_interface.rb
  2. +1 −1 lib/version.rb
  3. +8 −0 spec/mongoid_query_string_interface_spec.rb
View
8 lib/mongoid_query_string_interface.rb
@@ -87,7 +87,7 @@ def operator_from(key)
end
def parse_value(value, operator)
- parse_date(value) or parse_integer(value) or parse_array(value, operator) or value
+ parse_date(value) or parse_integer(value) or parse_array(value, operator) or parse_regex(value) or value
end
def parse_date(date)
@@ -111,6 +111,12 @@ def parse_float(float)
def parse_array(value, operator)
split_and_strip(value) if array_operator?(operator)
end
+
+ def parse_regex(regex)
+ if match = regex.match(/^\/(.*)\/(i|m|x)?$/)
+ eval(match[0])
+ end
+ end
def array_operator?(operator)
ARRAY_CONDITIONAL_OPERATORS.map { |op| "$#{op}" }.include?(operator.to_s)
View
2 lib/version.rb
@@ -1,6 +1,6 @@
# encoding: utf-8
module Mongoid #:nodoc
module QueryStringInterface #:nodoc
- VERSION = "0.1.0"
+ VERSION = "0.1.1"
end
end
View
8 spec/mongoid_query_string_interface_spec.rb
@@ -150,6 +150,14 @@ class EmbeddedDocument
Document.filter_by('title' => document.title, 'controller' => 'documents', 'action' => 'index', 'format' => 'json').should == [document]
end
+ it 'should accept simple regex values' do
+ Document.filter_by('title' => '/ome Tit/').should == [document]
+ end
+
+ it 'should accept regex values with modifiers' do
+ Document.filter_by('title' => '/some title/i').should == [document]
+ end
+
context 'with conditional operators' do
it 'should use it when given as the last portion of attribute name' do
Document.filter_by('title.ne' => 'Some Other Title').should == [document]

0 comments on commit b96587d

Please sign in to comment.