mongoid_query_string_interface 0.1.7 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +15 -0
- data/Gemfile.lock +75 -0
- data/lib/mongoid/parsers/array_parser.rb +25 -0
- data/lib/mongoid/parsers/boolean_and_nil_parser.rb +30 -0
- data/lib/mongoid/parsers/date_time_parser.rb +17 -0
- data/lib/mongoid/parsers/number_parser.rb +28 -0
- data/lib/mongoid/parsers/regex_parser.rb +17 -0
- data/lib/mongoid/query_string_interface.rb +212 -0
- data/lib/mongoid_query_string_interface.rb +1 -220
- data/lib/version.rb +1 -1
- metadata +55 -16
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
gem "rake", "0.8.7"
|
6
|
+
gem 'bson'
|
7
|
+
gem 'bson_ext'
|
8
|
+
|
9
|
+
platforms :mri_18 do
|
10
|
+
gem "ruby-debug"
|
11
|
+
end
|
12
|
+
|
13
|
+
platforms :mri_19 do
|
14
|
+
gem "ruby-debug19", :require => 'ruby-debug' if RUBY_VERSION < "1.9.3"
|
15
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mongoid_query_string_interface (0.2.0)
|
5
|
+
json (>= 1.4.6)
|
6
|
+
mongoid (~> 2.0.0.rc)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activemodel (3.0.3)
|
12
|
+
activesupport (= 3.0.3)
|
13
|
+
builder (~> 2.1.2)
|
14
|
+
i18n (~> 0.4)
|
15
|
+
activesupport (3.0.3)
|
16
|
+
archive-tar-minitar (0.5.2)
|
17
|
+
bson (1.2.0)
|
18
|
+
bson_ext (1.2.0)
|
19
|
+
builder (2.1.2)
|
20
|
+
columnize (0.3.2)
|
21
|
+
database_cleaner (0.6.1)
|
22
|
+
diff-lcs (1.1.2)
|
23
|
+
i18n (0.5.0)
|
24
|
+
json (1.4.6)
|
25
|
+
linecache (0.43)
|
26
|
+
linecache19 (0.5.11)
|
27
|
+
ruby_core_source (>= 0.1.4)
|
28
|
+
mongo (1.2.0)
|
29
|
+
bson (>= 1.2.0)
|
30
|
+
mongoid (2.0.0.rc.7)
|
31
|
+
activemodel (~> 3.0)
|
32
|
+
mongo (~> 1.2)
|
33
|
+
tzinfo (~> 0.3.22)
|
34
|
+
will_paginate (~> 3.0.pre)
|
35
|
+
rake (0.8.7)
|
36
|
+
rspec (2.4.0)
|
37
|
+
rspec-core (~> 2.4.0)
|
38
|
+
rspec-expectations (~> 2.4.0)
|
39
|
+
rspec-mocks (~> 2.4.0)
|
40
|
+
rspec-core (2.4.0)
|
41
|
+
rspec-expectations (2.4.0)
|
42
|
+
diff-lcs (~> 1.1.2)
|
43
|
+
rspec-mocks (2.4.0)
|
44
|
+
ruby-debug (0.10.4)
|
45
|
+
columnize (>= 0.1)
|
46
|
+
ruby-debug-base (~> 0.10.4.0)
|
47
|
+
ruby-debug-base (0.10.4)
|
48
|
+
linecache (>= 0.3)
|
49
|
+
ruby-debug-base19 (0.11.24)
|
50
|
+
columnize (>= 0.3.1)
|
51
|
+
linecache19 (>= 0.5.11)
|
52
|
+
ruby_core_source (>= 0.1.4)
|
53
|
+
ruby-debug19 (0.11.6)
|
54
|
+
columnize (>= 0.3.1)
|
55
|
+
linecache19 (>= 0.5.11)
|
56
|
+
ruby-debug-base19 (>= 0.11.19)
|
57
|
+
ruby_core_source (0.1.4)
|
58
|
+
archive-tar-minitar (>= 0.5.2)
|
59
|
+
tzinfo (0.3.24)
|
60
|
+
will_paginate (3.0.pre2)
|
61
|
+
|
62
|
+
PLATFORMS
|
63
|
+
ruby
|
64
|
+
|
65
|
+
DEPENDENCIES
|
66
|
+
bson
|
67
|
+
bson_ext
|
68
|
+
database_cleaner (>= 0.5.0)
|
69
|
+
json (>= 1.4.6)
|
70
|
+
mongoid (~> 2.0.0.rc)
|
71
|
+
mongoid_query_string_interface!
|
72
|
+
rake (= 0.8.7)
|
73
|
+
rspec (>= 2.3.0)
|
74
|
+
ruby-debug
|
75
|
+
ruby-debug19
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module QueryStringInterface
|
3
|
+
module Parsers
|
4
|
+
class ArrayParser
|
5
|
+
ARRAY_SEPARATOR = '|'
|
6
|
+
ARRAY_CONDITIONAL_OPERATORS = [:$all, :$in, :$nin]
|
7
|
+
|
8
|
+
def parseable?(value, operator)
|
9
|
+
operator && ARRAY_CONDITIONAL_OPERATORS.include?(operator.to_sym)
|
10
|
+
end
|
11
|
+
|
12
|
+
def parse(value)
|
13
|
+
value.split(ARRAY_SEPARATOR).map(&:strip).map do |item|
|
14
|
+
regex_parser.parse(item) or item
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def regex_parser
|
20
|
+
@regex_parser ||= RegexParser.new
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module QueryStringInterface
|
3
|
+
module Parsers
|
4
|
+
class BooleanAndNilParser
|
5
|
+
def parseable?(value, operator)
|
6
|
+
!value.nil? && !value.empty?
|
7
|
+
end
|
8
|
+
|
9
|
+
def parse(value)
|
10
|
+
if boolean?(value)
|
11
|
+
value.strip == 'true'
|
12
|
+
elsif value.nil? or value.empty? or nil_value?(value)
|
13
|
+
nil
|
14
|
+
else
|
15
|
+
value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
def boolean?(value)
|
21
|
+
value && ['true', 'false'].include?(value.strip)
|
22
|
+
end
|
23
|
+
|
24
|
+
def nil_value?(value)
|
25
|
+
value && ['nil', 'null'].include?(value.strip)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module QueryStringInterface
|
3
|
+
module Parsers
|
4
|
+
class DateTimeParser
|
5
|
+
def parseable?(value, operator)
|
6
|
+
not parse(value).nil?
|
7
|
+
end
|
8
|
+
|
9
|
+
def parse(value)
|
10
|
+
value.to_time and Time.parse(value)
|
11
|
+
rescue Exception
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module QueryStringInterface
|
3
|
+
module Parsers
|
4
|
+
class NumberParser
|
5
|
+
def parseable?(value, operator)
|
6
|
+
integer?(value) or float?(value)
|
7
|
+
end
|
8
|
+
|
9
|
+
def parse(value)
|
10
|
+
if integer?(value)
|
11
|
+
value.to_i
|
12
|
+
elsif float?(value)
|
13
|
+
value.to_f
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
def integer?(value)
|
19
|
+
value =~ /^\d+$/
|
20
|
+
end
|
21
|
+
|
22
|
+
def float?(value)
|
23
|
+
value =~ /^(\d+)(\.?\d*)$/
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module QueryStringInterface
|
3
|
+
module Parsers
|
4
|
+
class RegexParser
|
5
|
+
def parseable?(value, operator)
|
6
|
+
value =~ /^\/(.*)\/(i|m|x)?$/
|
7
|
+
end
|
8
|
+
|
9
|
+
def parse(value)
|
10
|
+
if value =~ /^\/(.*)\/(i|m|x)?$/
|
11
|
+
eval($&)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,212 @@
|
|
1
|
+
require File.expand_path(File.join('parsers', 'date_time_parser'), File.dirname(__FILE__))
|
2
|
+
require File.expand_path(File.join('parsers', 'number_parser'), File.dirname(__FILE__))
|
3
|
+
require File.expand_path(File.join('parsers', 'array_parser'), File.dirname(__FILE__))
|
4
|
+
require File.expand_path(File.join('parsers', 'regex_parser'), File.dirname(__FILE__))
|
5
|
+
require File.expand_path(File.join('parsers', 'boolean_and_nil_parser'), File.dirname(__FILE__))
|
6
|
+
|
7
|
+
module Mongoid
|
8
|
+
module QueryStringInterface
|
9
|
+
CONDITIONAL_OPERATORS = [:all, :exists, :gte, :gt, :in, :lte, :lt, :ne, :nin, :size, :near, :within]
|
10
|
+
SORTING_OPERATORS = [:asc, :desc]
|
11
|
+
OR_OPERATOR = :or
|
12
|
+
|
13
|
+
ATTRIBUTE_REGEX = /(.*)\.(#{(CONDITIONAL_OPERATORS + SORTING_OPERATORS + [OR_OPERATOR]).join('|')})/
|
14
|
+
|
15
|
+
PARSERS = [
|
16
|
+
Mongoid::QueryStringInterface::Parsers::DateTimeParser.new,
|
17
|
+
Mongoid::QueryStringInterface::Parsers::NumberParser.new,
|
18
|
+
Mongoid::QueryStringInterface::Parsers::ArrayParser.new,
|
19
|
+
Mongoid::QueryStringInterface::Parsers::RegexParser.new,
|
20
|
+
Mongoid::QueryStringInterface::Parsers::BooleanAndNilParser.new
|
21
|
+
]
|
22
|
+
|
23
|
+
def filter_by(params={})
|
24
|
+
params = hash_with_indifferent_access(params)
|
25
|
+
filter_only_by(params).order_by(*sorting_options(params)).paginate(pagination_options(params))
|
26
|
+
end
|
27
|
+
|
28
|
+
def filter_only_by(params={})
|
29
|
+
where(filtering_options(hash_with_indifferent_access(params)))
|
30
|
+
end
|
31
|
+
|
32
|
+
def paginated_collection_with_filter_by(params={})
|
33
|
+
params = hash_with_indifferent_access(params)
|
34
|
+
|
35
|
+
pagination = pagination_options(params)
|
36
|
+
pager = WillPaginate::Collection.new pagination[:page], pagination[:per_page], where(filtering_options(params)).count
|
37
|
+
|
38
|
+
[:total_entries, :total_pages, :per_page, :offset, :previous_page, :current_page, :next_page].inject({}) do |result, attr|
|
39
|
+
result[attr] = pager.send(attr)
|
40
|
+
result
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def default_filtering_options
|
45
|
+
{}
|
46
|
+
end
|
47
|
+
|
48
|
+
def default_sorting_options
|
49
|
+
[]
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
def pagination_options(options)
|
54
|
+
options.reverse_merge :per_page => 12, :page => 1
|
55
|
+
end
|
56
|
+
|
57
|
+
def filtering_options(options)
|
58
|
+
default_filtering_options.merge(parse_operators(only_filtering(options)))
|
59
|
+
end
|
60
|
+
|
61
|
+
def sorting_options(options)
|
62
|
+
options = only_sorting(options)
|
63
|
+
|
64
|
+
sorting_options = []
|
65
|
+
sorting_options.concat(parse_order_by(options))
|
66
|
+
sorting_options.concat(parse_sorting(options))
|
67
|
+
|
68
|
+
sorting_options.empty? ? default_sorting_options : sorting_options
|
69
|
+
end
|
70
|
+
|
71
|
+
def parse_operators(options)
|
72
|
+
options.inject({}) do |result, item|
|
73
|
+
key, value = item
|
74
|
+
|
75
|
+
attribute = attribute_from(key)
|
76
|
+
|
77
|
+
if or_attribute?(attribute)
|
78
|
+
parse_or_attribute(attribute, key, value, result)
|
79
|
+
else
|
80
|
+
parse_normal_attribute(attribute, key, value, result)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def parse_or_attribute(attribute, key, value, result)
|
86
|
+
result["$or"] = ::JSON.parse(unescape(value)).map do |filters|
|
87
|
+
parse_operators(filters)
|
88
|
+
end
|
89
|
+
|
90
|
+
result
|
91
|
+
end
|
92
|
+
|
93
|
+
def parse_normal_attribute(attribute, key, value, result)
|
94
|
+
operator = operator_from(key)
|
95
|
+
value = parse_value(value, operator)
|
96
|
+
|
97
|
+
if operator
|
98
|
+
filter = { operator => value }
|
99
|
+
|
100
|
+
if result.has_key?(attribute)
|
101
|
+
result[attribute].merge!(filter)
|
102
|
+
else
|
103
|
+
result[attribute] = filter
|
104
|
+
end
|
105
|
+
else
|
106
|
+
result[attribute] = value
|
107
|
+
end
|
108
|
+
|
109
|
+
result
|
110
|
+
end
|
111
|
+
|
112
|
+
def attribute_from(key)
|
113
|
+
if key =~ ATTRIBUTE_REGEX
|
114
|
+
$1.to_sym
|
115
|
+
else
|
116
|
+
key.to_sym
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def or_attribute?(attribute)
|
121
|
+
OR_OPERATOR == attribute
|
122
|
+
end
|
123
|
+
|
124
|
+
def operator_from(key)
|
125
|
+
if match = key.match(/.*\.(#{CONDITIONAL_OPERATORS.join('|')})/)
|
126
|
+
"$#{match[1]}".to_sym
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def unescape(value)
|
131
|
+
URI.unescape(value)
|
132
|
+
end
|
133
|
+
|
134
|
+
def parse_value(value, operator)
|
135
|
+
if value.is_a?(String)
|
136
|
+
value = unescape(value)
|
137
|
+
|
138
|
+
PARSERS.each do |parser|
|
139
|
+
return parser.parse(value) if parser.parseable?(value, operator)
|
140
|
+
end
|
141
|
+
|
142
|
+
return nil
|
143
|
+
else
|
144
|
+
value
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def hash_with_indifferent_access(params)
|
149
|
+
params.is_a?(HashWithIndifferentAccess) ? params : HashWithIndifferentAccess.new(params)
|
150
|
+
end
|
151
|
+
|
152
|
+
def only_filtering(options)
|
153
|
+
options.except(*only_sorting(options).keys).except(:per_page, :page, :action, :controller, :format, :order_by)
|
154
|
+
end
|
155
|
+
|
156
|
+
def only_sorting(options)
|
157
|
+
options.inject({}) do |result, item|
|
158
|
+
key, value = item
|
159
|
+
result[key] = value if sorting_parameter?(key, value)
|
160
|
+
result
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def sorting_parameter?(key, value)
|
165
|
+
order_by_parameter?(key) or sorting_key_parameter?(key) or sorting_value_parameter?(value)
|
166
|
+
end
|
167
|
+
|
168
|
+
def order_by_parameter?(key)
|
169
|
+
key.to_s == 'order_by'
|
170
|
+
end
|
171
|
+
|
172
|
+
def sorting_key_parameter?(key)
|
173
|
+
key.match(/(.*)\.(#{SORTING_OPERATORS.join('|')})/)
|
174
|
+
end
|
175
|
+
|
176
|
+
def sorting_value_parameter?(value)
|
177
|
+
value.present? && SORTING_OPERATORS.include?(value.to_sym)
|
178
|
+
end
|
179
|
+
|
180
|
+
def parse_order_by(options)
|
181
|
+
sorting_options = []
|
182
|
+
|
183
|
+
if order_by = options.delete('order_by')
|
184
|
+
if match = order_by.match(/(.*)\.(#{SORTING_OPERATORS.join('|')})/)
|
185
|
+
sorting_options << match[1].to_sym.send(match[2])
|
186
|
+
else
|
187
|
+
sorting_options << order_by.to_sym.asc
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
sorting_options
|
192
|
+
end
|
193
|
+
|
194
|
+
def parse_sorting(options)
|
195
|
+
options.inject([]) do |result, item|
|
196
|
+
key, value = item
|
197
|
+
|
198
|
+
attribute = attribute_from(key)
|
199
|
+
sorting_operator = sorting_operator_from(key)
|
200
|
+
|
201
|
+
result << attribute.send(sorting_operator || value)
|
202
|
+
result
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
def sorting_operator_from(key)
|
207
|
+
if match = key.match(/.*\.(#{SORTING_OPERATORS.join('|')})/)
|
208
|
+
match[1].to_sym
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
@@ -1,220 +1 @@
|
|
1
|
-
|
2
|
-
module QueryStringInterface
|
3
|
-
CONDITIONAL_OPERATORS = [:all, :exists, :gte, :gt, :in, :lte, :lt, :ne, :nin, :size, :near, :within]
|
4
|
-
ARRAY_CONDITIONAL_OPERATORS = [:all, :in, :nin]
|
5
|
-
SORTING_OPERATORS = [:asc, :desc]
|
6
|
-
|
7
|
-
def filter_by(params={})
|
8
|
-
params = hash_with_indifferent_access(params)
|
9
|
-
filter_only_by(params).order_by(*sorting_options(params)).paginate(pagination_options(params))
|
10
|
-
end
|
11
|
-
|
12
|
-
def filter_only_by(params={})
|
13
|
-
where(filtering_options(hash_with_indifferent_access(params)))
|
14
|
-
end
|
15
|
-
|
16
|
-
def paginated_collection_with_filter_by(params={})
|
17
|
-
params = hash_with_indifferent_access(params)
|
18
|
-
|
19
|
-
pagination = pagination_options(params)
|
20
|
-
pager = WillPaginate::Collection.new pagination[:page], pagination[:per_page], where(filtering_options(params)).count
|
21
|
-
|
22
|
-
[:total_entries, :total_pages, :per_page, :offset, :previous_page, :current_page, :next_page].inject({}) do |result, attr|
|
23
|
-
result[attr] = pager.send(attr)
|
24
|
-
result
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def default_filtering_options
|
29
|
-
{}
|
30
|
-
end
|
31
|
-
|
32
|
-
def default_sorting_options
|
33
|
-
[]
|
34
|
-
end
|
35
|
-
|
36
|
-
private
|
37
|
-
def pagination_options(options)
|
38
|
-
options.reverse_merge :per_page => 12, :page => 1
|
39
|
-
end
|
40
|
-
|
41
|
-
def filtering_options(options)
|
42
|
-
default_filtering_options.merge(parse_operators(only_filtering(options)))
|
43
|
-
end
|
44
|
-
|
45
|
-
def sorting_options(options)
|
46
|
-
options = only_sorting(options)
|
47
|
-
|
48
|
-
sorting_options = []
|
49
|
-
sorting_options.concat(parse_order_by(options))
|
50
|
-
sorting_options.concat(parse_sorting(options))
|
51
|
-
|
52
|
-
sorting_options.empty? ? default_sorting_options : sorting_options
|
53
|
-
end
|
54
|
-
|
55
|
-
def parse_operators(options)
|
56
|
-
options.inject({}) do |result, item|
|
57
|
-
key, value = item
|
58
|
-
|
59
|
-
attribute = attribute_from(key)
|
60
|
-
operator = operator_from(key)
|
61
|
-
value = parse_value(unescape(value), operator)
|
62
|
-
|
63
|
-
if operator
|
64
|
-
filter = { operator => value }
|
65
|
-
|
66
|
-
if result.has_key?(attribute)
|
67
|
-
result[attribute].merge!(filter)
|
68
|
-
else
|
69
|
-
result[attribute] = filter
|
70
|
-
end
|
71
|
-
else
|
72
|
-
result[attribute] = value
|
73
|
-
end
|
74
|
-
|
75
|
-
result
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
def attribute_from(key)
|
80
|
-
if match = key.match(/(.*)\.(#{(CONDITIONAL_OPERATORS + SORTING_OPERATORS).join('|')})/)
|
81
|
-
match[1].to_sym
|
82
|
-
else
|
83
|
-
key.to_sym
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def operator_from(key)
|
88
|
-
if match = key.match(/.*\.(#{CONDITIONAL_OPERATORS.join('|')})/)
|
89
|
-
"$#{match[1]}".to_sym
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
def unescape(value)
|
94
|
-
URI.unescape(value)
|
95
|
-
end
|
96
|
-
|
97
|
-
def parse_value(value, operator)
|
98
|
-
parse_date(value) or
|
99
|
-
parse_integer(value) or
|
100
|
-
parse_float(value) or
|
101
|
-
parse_array(value, operator) or
|
102
|
-
parse_regex(value) or
|
103
|
-
parse_boolean_and_nil(value)
|
104
|
-
end
|
105
|
-
|
106
|
-
def parse_date(date)
|
107
|
-
date.to_time and Time.parse(date)
|
108
|
-
rescue Exception
|
109
|
-
nil
|
110
|
-
end
|
111
|
-
|
112
|
-
def parse_integer(integer)
|
113
|
-
if match = integer.match(/^\d+$/)
|
114
|
-
match[0].to_i
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
def parse_float(float)
|
119
|
-
if match = float.match(/^(\d+)(\.?\d*)$/)
|
120
|
-
match[0].to_f
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
def parse_array(value, operator)
|
125
|
-
if array_operator?(operator)
|
126
|
-
split_and_strip(value).map { |item| parse_regex(item) or item }
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
def parse_regex(regex)
|
131
|
-
if match = regex.match(/^\/(.*)\/(i|m|x)?$/)
|
132
|
-
eval(match[0])
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
def parse_boolean_and_nil(value)
|
137
|
-
unless value.nil? || value.empty?
|
138
|
-
if ['true', 'false'].include?(value.strip)
|
139
|
-
value.strip == 'true'
|
140
|
-
elsif value.strip == 'nil'
|
141
|
-
nil
|
142
|
-
else
|
143
|
-
value
|
144
|
-
end
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
def array_operator?(operator)
|
149
|
-
ARRAY_CONDITIONAL_OPERATORS.map { |op| "$#{op}" }.include?(operator.to_s)
|
150
|
-
end
|
151
|
-
|
152
|
-
def split_and_strip(values)
|
153
|
-
values.split('|').map(&:strip)
|
154
|
-
end
|
155
|
-
|
156
|
-
def hash_with_indifferent_access(params)
|
157
|
-
params.is_a?(HashWithIndifferentAccess) ? params : HashWithIndifferentAccess.new(params)
|
158
|
-
end
|
159
|
-
|
160
|
-
def only_filtering(options)
|
161
|
-
options.except(*only_sorting(options).keys).except(:per_page, :page, :action, :controller, :format, :order_by)
|
162
|
-
end
|
163
|
-
|
164
|
-
def only_sorting(options)
|
165
|
-
options.inject({}) do |result, item|
|
166
|
-
key, value = item
|
167
|
-
result[key] = value if sorting_parameter?(key, value)
|
168
|
-
result
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
def sorting_parameter?(key, value)
|
173
|
-
order_by_parameter?(key) or sorting_key_parameter?(key) or sorting_value_parameter?(value)
|
174
|
-
end
|
175
|
-
|
176
|
-
def order_by_parameter?(key)
|
177
|
-
key.to_s == 'order_by'
|
178
|
-
end
|
179
|
-
|
180
|
-
def sorting_key_parameter?(key)
|
181
|
-
key.match(/(.*)\.(#{SORTING_OPERATORS.join('|')})/)
|
182
|
-
end
|
183
|
-
|
184
|
-
def sorting_value_parameter?(value)
|
185
|
-
value.present? && SORTING_OPERATORS.include?(value.to_sym)
|
186
|
-
end
|
187
|
-
|
188
|
-
def parse_order_by(options)
|
189
|
-
sorting_options = []
|
190
|
-
|
191
|
-
if order_by = options.delete('order_by')
|
192
|
-
if match = order_by.match(/(.*)\.(#{SORTING_OPERATORS.join('|')})/)
|
193
|
-
sorting_options << match[1].to_sym.send(match[2])
|
194
|
-
else
|
195
|
-
sorting_options << order_by.to_sym.asc
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
|
-
sorting_options
|
200
|
-
end
|
201
|
-
|
202
|
-
def parse_sorting(options)
|
203
|
-
options.inject([]) do |result, item|
|
204
|
-
key, value = item
|
205
|
-
|
206
|
-
attribute = attribute_from(key)
|
207
|
-
sorting_operator = sorting_operator_from(key)
|
208
|
-
|
209
|
-
result << attribute.send(sorting_operator || value)
|
210
|
-
result
|
211
|
-
end
|
212
|
-
end
|
213
|
-
|
214
|
-
def sorting_operator_from(key)
|
215
|
-
if match = key.match(/.*\.(#{SORTING_OPERATORS.join('|')})/)
|
216
|
-
match[1].to_sym
|
217
|
-
end
|
218
|
-
end
|
219
|
-
end
|
220
|
-
end
|
1
|
+
require File.expand_path(File.join('mongoid', 'query_string_interface'), File.dirname(__FILE__))
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_query_string_interface
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Vicente Mundim
|
@@ -15,43 +15,74 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-02-01 00:00:00 -02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
-
name:
|
22
|
+
name: json
|
23
23
|
prerelease: false
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 11
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 4
|
33
|
+
- 6
|
34
|
+
version: 1.4.6
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: mongoid
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
25
41
|
none: false
|
26
42
|
requirements:
|
27
43
|
- - ~>
|
28
44
|
- !ruby/object:Gem::Version
|
29
|
-
hash:
|
45
|
+
hash: 7712058
|
30
46
|
segments:
|
31
47
|
- 2
|
32
48
|
- 0
|
33
49
|
- 0
|
34
|
-
-
|
35
|
-
version: 2.0.0.
|
50
|
+
- rc
|
51
|
+
version: 2.0.0.rc
|
36
52
|
type: :runtime
|
37
|
-
version_requirements: *
|
53
|
+
version_requirements: *id002
|
38
54
|
- !ruby/object:Gem::Dependency
|
39
55
|
name: rspec
|
40
56
|
prerelease: false
|
41
|
-
requirement: &
|
57
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
42
58
|
none: false
|
43
59
|
requirements:
|
44
|
-
- -
|
60
|
+
- - ">="
|
45
61
|
- !ruby/object:Gem::Version
|
46
|
-
hash:
|
62
|
+
hash: 3
|
47
63
|
segments:
|
48
64
|
- 2
|
65
|
+
- 3
|
66
|
+
- 0
|
67
|
+
version: 2.3.0
|
68
|
+
type: :development
|
69
|
+
version_requirements: *id003
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: database_cleaner
|
72
|
+
prerelease: false
|
73
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
hash: 11
|
79
|
+
segments:
|
49
80
|
- 0
|
81
|
+
- 5
|
50
82
|
- 0
|
51
|
-
|
52
|
-
version: 2.0.0.beta
|
83
|
+
version: 0.5.0
|
53
84
|
type: :development
|
54
|
-
version_requirements: *
|
85
|
+
version_requirements: *id004
|
55
86
|
description: Gives a method that can parse query string parameters into a set of criterias that Mongoid can use to perform actual queries in MongoDB databases for a given model.
|
56
87
|
email:
|
57
88
|
- vicente.mundim@gmail.com
|
@@ -62,10 +93,18 @@ extensions: []
|
|
62
93
|
extra_rdoc_files: []
|
63
94
|
|
64
95
|
files:
|
96
|
+
- lib/mongoid/parsers/array_parser.rb
|
97
|
+
- lib/mongoid/parsers/boolean_and_nil_parser.rb
|
98
|
+
- lib/mongoid/parsers/date_time_parser.rb
|
99
|
+
- lib/mongoid/parsers/number_parser.rb
|
100
|
+
- lib/mongoid/parsers/regex_parser.rb
|
101
|
+
- lib/mongoid/query_string_interface.rb
|
65
102
|
- lib/mongoid_query_string_interface.rb
|
66
103
|
- lib/version.rb
|
67
104
|
- MIT_LICENSE
|
68
105
|
- README.rdoc
|
106
|
+
- Gemfile
|
107
|
+
- Gemfile.lock
|
69
108
|
has_rdoc: true
|
70
109
|
homepage: http://github.com/vicentemundim/mongoid_query_string_interface
|
71
110
|
licenses: []
|