cx-mongo_query 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/cx-mongo_query.gemspec +20 -0
- data/lib/cx/mongo_query.rb +1 -0
- data/lib/cx/mongo_query/operators.rb +172 -0
- data/lib/cx/mongo_query/version.rb +5 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d561f30f7d359c55f678226e08b2a10878cb435a
|
4
|
+
data.tar.gz: c3d23c9b981b740729b09ad457e4f19336dfcf0f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a403e6a1f7978f8a4204761422f6734980495b0a2305b288cf33c5b2c9a9a7823e3b448f1fd417560885beaf187e14dc723cf9b420b42656161640c1655d2372
|
7
|
+
data.tar.gz: b9658f254526232bb2ddf2870636fe4833b44de88b99062a0c593ca082f4843d1d34ed48d7b578ca88bf32f27eae53fd5a387f7bdd8c17031b33e667d925bd1b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cx/mongo_query/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'cx-mongo_query'
|
8
|
+
spec.version = CX::Mongo::Query::VERSION
|
9
|
+
spec.date = '2015-10-02'
|
10
|
+
spec.summary = 'Shorthand helpers for constructing readable Mongo query args in Ruby'
|
11
|
+
spec.authors = ['Colin Gunn']
|
12
|
+
spec.email = 'colgunn@icloud.com'
|
13
|
+
spec.homepage = 'http://rubygemspec.org/gems/cx-mongo_query' # TODO: push to rubygems ??
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'cx/mongo_query/operators.rb'
|
@@ -0,0 +1,172 @@
|
|
1
|
+
# http://docs.mongodb.org/manual/reference/operator/query/
|
2
|
+
#
|
3
|
+
# COMPARISON
|
4
|
+
# ---- -------------------------
|
5
|
+
# Name Description
|
6
|
+
# ---- -------------------------
|
7
|
+
# $eq Matches values that are equal to a specified value.
|
8
|
+
# $gt Matches values that are greater than a specified value.
|
9
|
+
# $gte Matches values that are greater than or equal to a specified value.
|
10
|
+
# $lt Matches values that are less than a specified value.
|
11
|
+
# $lte Matches values that are less than or equal to a specified value.
|
12
|
+
# $ne Matches all values that are not equal to a specified value.
|
13
|
+
# $in Matches any of the values specified in an array.
|
14
|
+
# $nin Matches none of the values specified in an array.
|
15
|
+
#
|
16
|
+
# LOGICAL
|
17
|
+
# ---- -------------------------
|
18
|
+
# Name Description
|
19
|
+
# ---- -------------------------
|
20
|
+
# $or Joins query clauses with a logical OR : returns all documents that match the conditions of either clause.
|
21
|
+
# $and Joins query clauses with a logical AND : returns all documents that match the conditions of both clauses.
|
22
|
+
# $not Inverts the effect of a query expression : returns documents that do not match the query expression.
|
23
|
+
# $nor Joins query clauses with a logical NOR : returns all documents that fail to match both clauses.
|
24
|
+
#
|
25
|
+
# EG
|
26
|
+
#
|
27
|
+
# db.inventory.find( { qty: { $gte: 20 } } )
|
28
|
+
#
|
29
|
+
# db.inventory.update( { "carrier.fee": { $gte: 2 } }, { $set: { price: 9.99 } } )
|
30
|
+
#
|
31
|
+
# db.inventory.find( { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } )
|
32
|
+
#
|
33
|
+
# db.inventory.find( { qty: { $nin: [ 5, 15 ] } } )
|
34
|
+
#
|
35
|
+
# db.inventory.find( { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] } )
|
36
|
+
#
|
37
|
+
# DB.bars.find(symbol: 'AAPL', $and => [{date: {$gte => '20150615'}}, {date: {$lte => '20150618'}} ])
|
38
|
+
#
|
39
|
+
# db.inventory.find( {
|
40
|
+
# $and : [
|
41
|
+
# { $or : [ { price : 0.99 }, { price : 1.99 } ] },
|
42
|
+
# { $or : [ { sale : true }, { qty : { $lt : 20 } } ] }
|
43
|
+
# ]
|
44
|
+
# } )
|
45
|
+
#
|
46
|
+
# REGEX SYNTAX
|
47
|
+
# { <field>: { $regex: /pattern/, $options: '<options>' } }
|
48
|
+
# { <field>: { $regex: 'pattern', $options: '<options>' } }
|
49
|
+
# { <field>: { $regex: /pattern/<options> } }
|
50
|
+
#
|
51
|
+
# REGEX EG
|
52
|
+
# { name: { $in: [ /^acme/i, /^ack/ ] } }
|
53
|
+
# { name: { $regex: /acme.*corp/i, $nin: [ 'acmeblahcorp' ] } }
|
54
|
+
# { name: { $regex: /acme.*corp/, $options: 'i', $nin: [ 'acmeblahcorp' ] } }
|
55
|
+
# { name: { $regex: 'acme.*corp', $options: 'i', $nin: [ 'acmeblahcorp' ] } }
|
56
|
+
#
|
57
|
+
# REGEX REF
|
58
|
+
# http://docs.mongodb.org/manual/reference/operator/query/regex/#op._S_regex
|
59
|
+
#
|
60
|
+
#
|
61
|
+
# TODO: add all mongo query and projection stuff
|
62
|
+
|
63
|
+
# COMPARISON
|
64
|
+
$eq = '$eq'
|
65
|
+
$ne = '$ne'
|
66
|
+
$gt = '$gt'
|
67
|
+
$gte = '$gte'
|
68
|
+
$lt = '$lt'
|
69
|
+
$lte = '$lte'
|
70
|
+
$in = '$in'
|
71
|
+
$nin = '$nin'
|
72
|
+
# LOGICAL
|
73
|
+
$and = '$and'
|
74
|
+
$or = '$or'
|
75
|
+
$not = '$not'
|
76
|
+
$nor = '$nor'
|
77
|
+
# REGEX
|
78
|
+
$regex = '$regex'
|
79
|
+
$options = '$options'
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
# Include this module or use as module_function.
|
84
|
+
# EG
|
85
|
+
# and( eq(:name, 'Smith'), eq(:dob, '19991231'))
|
86
|
+
# =>
|
87
|
+
# {"$and"=>[{"name"=>{"$eq"=>"Fred"}}, {"dob"=>{"$eq"=>"19991231"}}]}
|
88
|
+
module CX; module Mongo
|
89
|
+
module Query
|
90
|
+
|
91
|
+
module_function
|
92
|
+
|
93
|
+
# COMPARISON OPS
|
94
|
+
def eq( attr, val ); { attr.to_s => { $eq => val } } end
|
95
|
+
def ne( attr, val ); { attr.to_s => { $ne => val } } end
|
96
|
+
def gt( attr, val ); { attr.to_s => { $gt => val } } end
|
97
|
+
def ge( attr, val ); { attr.to_s => { $gte => val } } end
|
98
|
+
def lt( attr, val ); { attr.to_s => { $lt => val } } end
|
99
|
+
def le( attr, val ); { attr.to_s => { $lte => val } } end
|
100
|
+
def in( attr, *vals ); { attr.to_s => { $in => vals } } end
|
101
|
+
def nin( attr, *vals ); { attr.to_s => { $nin => vals } } end
|
102
|
+
def in_s( attr, str ); { attr.to_s => { $in => str } } end
|
103
|
+
def nin_s( attr, str ); { attr.to_s => { $nin => str } } end
|
104
|
+
|
105
|
+
# LOGICAL OPS
|
106
|
+
def and( *args ); { $and => args } end # one or more query args
|
107
|
+
def or( *args ); { $or => args } end # one or more query args
|
108
|
+
def nor( *args ); { $nor => args } end # one or more query args
|
109
|
+
def not( arg ); { $not => arg } end # single query argument only
|
110
|
+
|
111
|
+
# REGEX EXPRESSION
|
112
|
+
def rx(pattern, *qualifiers)
|
113
|
+
r = { $regex => pattern }
|
114
|
+
qualifiers.each {|q| r.merge!(q)}
|
115
|
+
r
|
116
|
+
end
|
117
|
+
|
118
|
+
# REGEX OPTIONS
|
119
|
+
def rxo(string)
|
120
|
+
{ $options => string.to_s }
|
121
|
+
end
|
122
|
+
|
123
|
+
# Intended for use in monkey patching String and Symbol
|
124
|
+
module Postfix
|
125
|
+
def eq(val); { self.to_s => { $eq => val } } end
|
126
|
+
def ne(val); { self.to_s => { $ne => val } } end
|
127
|
+
def gt(val); { self.to_s => { $gt => val } } end
|
128
|
+
def ge(val); { self.to_s => { $gte => val } } end
|
129
|
+
def lt(val); { self.to_s => { $lt => val } } end
|
130
|
+
def le(val); { self.to_s => { $lte => val } } end
|
131
|
+
def in(*vals); { self.to_s => { $in => vals } } end
|
132
|
+
def nin(*vals); { self.to_s => { $nin => vals } } end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end end
|
136
|
+
|
137
|
+
# EG
|
138
|
+
# Using string as an attribute name:
|
139
|
+
# 'name'.ne('Colin') => {"name"=>{"$gte"=>"Colin"}}
|
140
|
+
# 'date'.le('20151231') => {"date"=>{"$lte"=>"20151231"}}
|
141
|
+
#
|
142
|
+
# Using string as target of $in and $nin operators
|
143
|
+
# 'abcdefg'.nin => {"$nin"=>"abcdefg"}
|
144
|
+
class String
|
145
|
+
include CX::Mongo::Query::Postfix
|
146
|
+
def rxo; CX::Mongo::Query.rxo(self) end
|
147
|
+
def in; CX::Mongo::Query.in_s(self) end
|
148
|
+
def nin; CX::Mongo::Query.nin_s(self) end
|
149
|
+
end
|
150
|
+
|
151
|
+
# EG
|
152
|
+
#
|
153
|
+
# Using symbol as an attribute name:
|
154
|
+
# :name.ne('Colin')
|
155
|
+
# :date.le('20151231')
|
156
|
+
#
|
157
|
+
class Symbol
|
158
|
+
include CX::Mongo::Query::Postfix
|
159
|
+
end
|
160
|
+
|
161
|
+
# EG
|
162
|
+
# either
|
163
|
+
# /.*/.qrx('i'.qrxo, 'abcdefg'))
|
164
|
+
# or
|
165
|
+
# $q.regex(/.*/, $q.options('i'), $q.in_string('abcdefg'))
|
166
|
+
class Regexp
|
167
|
+
def rx(*qualifiers)
|
168
|
+
CX::Mongo::Query.rx(self, *qualifiers)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cx-mongo_query
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Colin Gunn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-02 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email: colgunn@icloud.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- ".gitignore"
|
20
|
+
- Gemfile
|
21
|
+
- cx-mongo_query.gemspec
|
22
|
+
- lib/cx/mongo_query.rb
|
23
|
+
- lib/cx/mongo_query/operators.rb
|
24
|
+
- lib/cx/mongo_query/version.rb
|
25
|
+
homepage: http://rubygemspec.org/gems/cx-mongo_query
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 2.4.6
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: Shorthand helpers for constructing readable Mongo query args in Ruby
|
49
|
+
test_files: []
|
50
|
+
has_rdoc:
|