query_string_filter 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/lib/filter.treetop CHANGED
@@ -63,7 +63,7 @@ grammar Filter
63
63
  end
64
64
 
65
65
  rule key
66
- [a-z]+
66
+ [a-z_]+
67
67
  {
68
68
  def eval
69
69
  text_value
@@ -89,14 +89,36 @@ grammar Filter
89
89
  end
90
90
 
91
91
  rule string
92
- [A-Z_a-z0-9]+
92
+ double_quoted_string / single_quoted_string / unquoted_string
93
+ end
94
+
95
+ rule double_quoted_string
96
+ '"' (!'"' .)* '"'
93
97
  {
94
98
  def eval
95
- text_value
99
+ text_value[1 ... -1]
100
+ end
101
+ }
102
+ end
103
+
104
+ rule single_quoted_string
105
+ '\'' (!'\'' .)* '\''
106
+ {
107
+ def eval
108
+ text_value[1 ... -1]
96
109
  end
97
110
  }
98
111
  end
99
112
 
113
+ rule unquoted_string
114
+ [A-Za-z0-9_]+
115
+ {
116
+ def eval
117
+ text_value
118
+ end
119
+ }
120
+ end
121
+
100
122
  rule integer
101
123
  [0-9]+
102
124
  {
@@ -4,12 +4,16 @@ Treetop.load(File.dirname(__FILE__) + '/filter')
4
4
 
5
5
  class QueryStringFilter
6
6
 
7
+ class ParseError < RuntimeError; end
8
+
7
9
  def initialize
8
10
  @parser = FilterParser.new
9
11
  end
10
12
 
11
- def process(string)
12
- @parser.parse(string).eval
13
+ def parse(string)
14
+ parsed = @parser.parse(string)
15
+ raise ParseError, "unable to parse: #{string}" unless parsed
16
+ parsed.eval
13
17
  end
14
18
 
15
19
  end
@@ -5,12 +5,12 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{query_string_filter}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["David James"]
12
12
  s.date = %q{2009-10-27}
13
- s.description = %q{TODO: longer description of your gem}
13
+ s.description = %q{Convert a filter param in a query string to a conditions hash useful for MongoMapper searching}
14
14
  s.email = %q{djames@sunlightfoundation.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
@@ -34,7 +34,7 @@ Gem::Specification.new do |s|
34
34
  s.rdoc_options = ["--charset=UTF-8"]
35
35
  s.require_paths = ["lib"]
36
36
  s.rubygems_version = %q{1.3.5}
37
- s.summary = %q{TODO: one-line summary of your gem}
37
+ s.summary = %q{Convert from a filter query string param to a MongoMapper conditions hash}
38
38
  s.test_files = [
39
39
  "spec/query_string_filter_spec.rb",
40
40
  "spec/spec_helper.rb"
@@ -6,56 +6,87 @@ describe "QueryStringFilter" do
6
6
  @filter = QueryStringFilter.new
7
7
  end
8
8
 
9
- describe "basic" do
9
+ describe "failure" do
10
+ it "no operator" do
11
+ lambda {
12
+ @filter.parse("something")
13
+ }.should raise_error(QueryStringFilter::ParseError)
14
+ end
15
+ end
16
+
17
+ describe "keys" do
18
+ it "key with underscore" do
19
+ @filter.parse("with_underscore = test").should == {
20
+ :with_underscore => "test"
21
+ }
22
+ end
23
+
24
+ it "value with single quotes" do
25
+ @filter.parse("foo = 'bar camp'").should == {
26
+ :foo => "bar camp"
27
+ }
28
+ end
29
+ end
10
30
 
31
+ describe "basic operators" do
11
32
  it ": with string" do
12
33
  expected = {
13
34
  :name => /david/
14
35
  }
15
36
  ["name:david", "name : david"].each do |s|
16
- @filter.process(s).should == expected
37
+ @filter.parse(s).should == expected
17
38
  end
18
39
  end
19
40
 
20
41
  it ": with number" do
21
42
  expected = { :count => /77/ }
22
43
  ["count:77", "count : 77"].each do |s|
23
- @filter.process(s).should == expected
44
+ @filter.parse(s).should == expected
45
+ end
46
+ end
47
+
48
+ it "= with string" do
49
+ expected = {
50
+ :name => "David"
51
+ }
52
+ [
53
+ %(name=David),
54
+ %(name = David)
55
+ ].each do |s|
56
+ @filter.parse(s).should == expected
24
57
  end
25
58
  end
26
59
 
27
60
  it ">" do
28
61
  expected = { :value => { '$gt' => 12 } }
29
62
  ["value>12", "value > 12"].each do |s|
30
- @filter.process(s).should == expected
63
+ @filter.parse(s).should == expected
31
64
  end
32
65
  end
33
66
 
34
67
  it "<" do
35
68
  expected = { :value => { '$lt' => 12 } }
36
69
  ["value<12", "value < 12"].each do |s|
37
- @filter.process(s).should == expected
70
+ @filter.parse(s).should == expected
38
71
  end
39
72
  end
40
73
 
41
74
  it ">=" do
42
75
  expected = { :value => { '$gte' => 12 } }
43
76
  ["value>=12", "value >= 12"].each do |s|
44
- @filter.process(s).should == expected
77
+ @filter.parse(s).should == expected
45
78
  end
46
79
  end
47
80
 
48
81
  it "<=" do
49
82
  expected = { :value => { '$lte' => 12 } }
50
83
  ["value<=12", "value <= 12"].each do |s|
51
- @filter.process(s).should == expected
84
+ @filter.parse(s).should == expected
52
85
  end
53
86
  end
54
-
55
87
  end
56
88
 
57
89
  describe "compound" do
58
-
59
90
  it ": with 2 strings" do
60
91
  expected = {
61
92
  :title => /election|texas/
@@ -64,7 +95,7 @@ describe "QueryStringFilter" do
64
95
  "title:election,texas",
65
96
  "title : election , texas"
66
97
  ].each do |s|
67
- @filter.process(s).should == expected
98
+ @filter.parse(s).should == expected
68
99
  end
69
100
  end
70
101
 
@@ -75,10 +106,9 @@ describe "QueryStringFilter" do
75
106
  [
76
107
  "title:election,texas,bob",
77
108
  ].each do |s|
78
- @filter.process(s).should == expected
109
+ @filter.parse(s).should == expected
79
110
  end
80
111
  end
81
-
82
112
  end
83
113
 
84
114
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: query_string_filter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David James