ransack_mongo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aa14d28f6e8b1cd4f880003a75f9fbe386065fde
4
+ data.tar.gz: db5b0fc818f5624ba66e18d310cecf139a3c7a12
5
+ SHA512:
6
+ metadata.gz: d1990c1854b2d5b6c9ac567b412d0f7ee967496acad4908cf60c372d72f956d729f160a2efd4a8881857be18aa907dab013e9231095abf95957d705236078545
7
+ data.tar.gz: b0b08055f284c65882ea7cd9d67d8b0527af1ce091e5ee53120a7504148654b9ccc53c03c4f1d63e0d08528b861fe069127388ddf32757c33224c272dc5d774f
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ransack_mongo.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Pablo Cantero
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # RansackMongo
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ransack_mongo'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ransack_mongo
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/ransack_mongo/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ require 'ransack_mongo/version'
2
+ require 'ransack_mongo/query'
3
+ require 'ransack_mongo/predicate'
4
+ require 'ransack_mongo/mongo_adapter'
5
+
6
+ module RansackMongo
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,72 @@
1
+ module RansackMongo
2
+ class MongoAdapter
3
+ PREDICATES = %w[eq not_eq cont in gt lt gteq lteq]
4
+
5
+ def initialize
6
+ @query = {}
7
+ end
8
+
9
+ def to_query
10
+ @query
11
+ end
12
+
13
+ def eq_matcher(attr, value)
14
+ @query[attr] = value
15
+ end
16
+
17
+ def not_eq_matcher(attr, value)
18
+ @query[attr] = { '$ne' => value }
19
+ end
20
+
21
+ def cont_matcher(attr, value)
22
+ @query[attr] = /#{value}/i
23
+ end
24
+
25
+ def in_matcher(attr, value)
26
+ @query[attr] = { '$in' => value }
27
+ end
28
+
29
+ def gt_matcher(attr, value)
30
+ @query[attr] ||= {}
31
+ @query[attr]['$gt'] = value.to_f
32
+ end
33
+
34
+ def lt_matcher(attr, value)
35
+ @query[attr] ||= {}
36
+ @query[attr]['$lt'] = value.to_f
37
+ end
38
+
39
+ def gteq_matcher(attr, value)
40
+ @query[attr] ||= {}
41
+ @query[attr]['$gte'] = value.to_f
42
+ end
43
+
44
+ def lteq_matcher(attr, value)
45
+ @query[attr] ||= {}
46
+ @query[attr]['$lte'] = value.to_f
47
+ end
48
+
49
+ def or_op # or operation
50
+ return unless block_given?
51
+
52
+ original_query = @query
53
+ @query = {}
54
+
55
+ yield
56
+
57
+ original_query['$or'] ||= []
58
+ original_query['$or'].concat @query.map { |attr, value| { attr => value } }
59
+
60
+ @query = original_query
61
+ end
62
+
63
+ def sanitize(unsanitized)
64
+ # http://docs.mongodb.org/manual/faq/developers/#how-does-mongodb-address-sql-or-query-injection
65
+ unsanitized
66
+ end
67
+
68
+ def self.predicates
69
+ PREDICATES
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,35 @@
1
+ module RansackMongo
2
+ class Predicate
3
+ def initialize(predicates)
4
+ @predicates = predicates
5
+ end
6
+
7
+ def parse(params)
8
+ params.keys.inject({}) do |query, query_param|
9
+ attr = query_param.to_s
10
+ p, attr = detect_and_strip_from_string(attr)
11
+
12
+ if p && attr
13
+ query[p] ||= []
14
+ query[p] << { 'attr' => attr, 'value' => params[query_param] }
15
+ end
16
+
17
+ query
18
+ end
19
+ end
20
+
21
+ def names_by_decreasing_length
22
+ @predicates.sort { |a, b| b.length <=> a.length }
23
+ end
24
+
25
+ def detect_from_string(str)
26
+ names_by_decreasing_length.detect { |p| str.end_with?("_#{p}") }
27
+ end
28
+
29
+ def detect_and_strip_from_string(str)
30
+ if p = detect_from_string(str)
31
+ [p, str.sub(/_#{p}$/, '')]
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,45 @@
1
+ module RansackMongo
2
+ class MatcherNotFound < StandardError; end
3
+
4
+ # https://github.com/activerecord-hackery/ransack/wiki/Basic-Searching
5
+ class Query
6
+ def initialize(db_adapter_class = MongoAdapter)
7
+ @db_adapter_class = db_adapter_class
8
+ end
9
+
10
+ def to_query(params)
11
+ parsed_predicates = Predicate.new(@db_adapter_class.predicates).parse(params)
12
+
13
+ db_adapter = @db_adapter_class.new
14
+
15
+ parsed_predicates.keys.each do |p|
16
+ parsed_predicates[p].each do |parsed_predicate|
17
+ attr = db_adapter.sanitize(parsed_predicate['attr'])
18
+ value = db_adapter.sanitize(parsed_predicate['value'])
19
+
20
+ begin
21
+ if attr.include? '_or_'
22
+ # attr => name_or_lastname
23
+ or_query(db_adapter, attr, value, p)
24
+ else
25
+ # attr => name
26
+ db_adapter.send("#{p}_matcher", attr, value)
27
+ end
28
+ rescue NoMethodError => e
29
+ raise MatcherNotFound, "The matcher #{p} `#{p}_matcher` was not found in the #{@db_adapter_class.name}. Check `#{@db_adapter_class}.predicates`"
30
+ end
31
+ end
32
+ end
33
+
34
+ db_adapter.to_query
35
+ end
36
+
37
+ def or_query(db_adapter, attr, value, p)
38
+ db_adapter.or_op do
39
+ attr.split('_or_').each do |or_attr|
40
+ db_adapter.send("#{p}_matcher", or_attr, value)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module RansackMongo
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ransack_mongo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'ransack_mongo'
8
+ spec.version = RansackMongo::VERSION
9
+ spec.authors = ['Pablo Cantero']
10
+ spec.email = ['pablo@pablocantero.com']
11
+ spec.homepage = 'https://github.com/phstc/ransack_mongo'
12
+ spec.summary = %q{Object-based searching for MongoDB (currently).}
13
+ spec.description = %q{Ransack Mongo is inspired/based on Ransack but for MongoDB}
14
+ spec.homepage = ''
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'rspec'
25
+ end
@@ -0,0 +1,105 @@
1
+ require 'spec_helper'
2
+
3
+ module RansackMongo
4
+ describe MongoAdapter do
5
+ describe '#eq_matcher' do
6
+ it 'returns the matcher' do
7
+ subject.eq_matcher('name', 'Pablo')
8
+
9
+ expect(subject.to_query).to eq('name' => 'Pablo')
10
+ end
11
+ end
12
+
13
+ describe '#not_eq_matcher' do
14
+ it 'returns the matcher' do
15
+ subject.not_eq_matcher('name', 'Pablo')
16
+
17
+ expect(subject.to_query).to eq('name' => { '$ne' => 'Pablo' })
18
+ end
19
+ end
20
+
21
+ describe '#cont_matcher' do
22
+ it 'returns the matcher' do
23
+ subject.cont_matcher('name', 'Pablo')
24
+
25
+ expect(subject.to_query).to eq('name' => /Pablo/i)
26
+ end
27
+ end
28
+
29
+ describe '#in_matcher' do
30
+ it 'returns the matcher' do
31
+ subject.in_matcher('name', %w[Pablo Cantero])
32
+
33
+ expect(subject.to_query).to eq('name' => { '$in' => %w[Pablo Cantero] })
34
+ end
35
+ end
36
+
37
+ context 'when combine gt lt gteq and lteq' do
38
+ it 'returns all matchers' do
39
+ subject.gt_matcher('count', '1')
40
+ subject.lt_matcher('count', '5')
41
+
42
+ subject.gteq_matcher('quantity', '10.5')
43
+ subject.lteq_matcher('quantity', '15')
44
+
45
+ # all string values must be convert to float `to_f` otherwise mongo will not filter them properly
46
+ expect(subject.to_query).to eq('count' => { '$gt' => 1.0, '$lt' => 5.0 },
47
+ 'quantity' => { '$gte' => 10.5, '$lte' => 15.0 })
48
+ end
49
+ end
50
+
51
+ describe '#gt_matcher' do
52
+ it 'returns the matcher' do
53
+ subject.gt_matcher('quantity', 1)
54
+
55
+ expect(subject.to_query).to eq('quantity' => { '$gt' => 1 })
56
+ end
57
+ end
58
+
59
+ describe '#lt_matcher' do
60
+ it 'returns the matcher' do
61
+ subject.lt_matcher('quantity', 1)
62
+
63
+ expect(subject.to_query).to eq('quantity' => { '$lt' => 1 })
64
+ end
65
+ end
66
+
67
+ describe '#gteq_matcher' do
68
+ it 'returns the matcher' do
69
+ subject.gteq_matcher('quantity', 1)
70
+
71
+ expect(subject.to_query).to eq('quantity' => { '$gte' => 1 })
72
+ end
73
+ end
74
+
75
+ describe '#lteq_matcher' do
76
+ it 'returns the matcher' do
77
+ subject.lteq_matcher('quantity', 1)
78
+
79
+ expect(subject.to_query).to eq('quantity' => { '$lte' => 1 })
80
+ end
81
+ end
82
+
83
+ describe '#or_op' do
84
+ it 'returns the or operation' do
85
+ subject.or_op do
86
+ subject.eq_matcher('name', 'Pablo')
87
+ subject.eq_matcher('lastname', 'Pablo')
88
+ end
89
+
90
+ expect(subject.to_query).to eq('$or' => [{ 'name' => 'Pablo' }, { 'lastname' => 'Pablo' }])
91
+ end
92
+
93
+ it 'preserves other criterias' do
94
+ subject.or_op do
95
+ subject.eq_matcher('name', 'Pablo')
96
+ subject.eq_matcher('lastname', 'Pablo')
97
+ end
98
+
99
+ subject.eq_matcher('country', 'Brazil')
100
+
101
+ expect(subject.to_query).to eq('$or' => [{ 'name' => 'Pablo' }, { 'lastname' => 'Pablo' }], 'country' => 'Brazil')
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ module RansackMongo
4
+ describe Predicate do
5
+ subject { described_class.new(%w[eq not_eq cont]) }
6
+
7
+ describe '#parse' do
8
+ it 'parses predicates' do
9
+ params = { 'name_eq' => 'Pablo', 'name_not_eq' => 'Paul', 'fullname_cont' => 'Cantero' }
10
+
11
+ expect(subject.parse(params)).to eq('eq' => [{ 'attr' => 'name', 'value' => 'Pablo', }],
12
+ 'not_eq' => [{ 'attr' => 'name', 'value' => 'Paul', }],
13
+ 'cont' => [{ 'attr' => 'fullname', 'value' => 'Cantero' }])
14
+ end
15
+
16
+ it 'ignores unknown predicates' do
17
+ params = { 'name_eq' => 'Pablo', 'name_bbb' => 'Paul' }
18
+
19
+ expect(subject.parse(params)).to eq('eq' => [{ 'attr' => 'name', 'value' => 'Pablo', }])
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ module RansackMongo
4
+ describe Query do
5
+ context 'when FooAdapter' do
6
+ class FooAdapter
7
+ PREDICATES = %w[foo]
8
+
9
+ def initialize
10
+ @query = {}
11
+ end
12
+
13
+ def to_query
14
+ @query
15
+ end
16
+
17
+ def sanitize(unsanitized)
18
+ unsanitized
19
+ end
20
+
21
+ def self.predicates
22
+ PREDICATES
23
+ end
24
+ end
25
+
26
+ describe '#to_query' do
27
+ context 'when not implement matcher' do
28
+ it 'raises proper exception' do
29
+ params = { 'name_foo' => 'Pablo' }
30
+ expect {
31
+ described_class.new(FooAdapter).to_query(params)
32
+ }.to raise_error(MatcherNotFound, 'The matcher foo `foo_matcher` was not found in the RansackMongo::FooAdapter. Check `RansackMongo::FooAdapter.predicates`')
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ context 'when MongoAdapter' do
39
+ describe '#to_query' do
40
+ it 'returns the query' do
41
+ params = { 'name_eq' => 'Pablo', 'fullname_cont' => 'Cantero' }
42
+
43
+ expect(described_class.new.to_query(params)).to eq('name' => 'Pablo', 'fullname' => /Cantero/i)
44
+ end
45
+
46
+ context 'when or' do
47
+ it 'returns the query' do
48
+ params = { 'name_or_fullname_eq' => 'Pablo' }
49
+
50
+ expect(described_class.new.to_query(params)).to eq('$or' => [{ 'name' => 'Pablo' }, { 'fullname' => 'Pablo' }])
51
+ end
52
+
53
+ it 'preserves other criterias' do
54
+ params = { 'name_or_fullname_eq' => 'Pablo', 'country_eq' => 'Brazil' }
55
+
56
+ expect(described_class.new.to_query(params)).to eq('$or' => [{ 'name' => 'Pablo' }, { 'fullname' => 'Pablo' }], 'country' => 'Brazil')
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,13 @@
1
+ require 'rspec'
2
+ require 'fileutils'
3
+ require 'tempfile'
4
+
5
+ Dir['../lib/**/*.rb'].each &method(:require)
6
+
7
+ require './lib/ransack_mongo'
8
+
9
+ RSpec.configure do |config|
10
+ config.color_enabled = true
11
+ config.formatter = 'progress'
12
+ end
13
+
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ransack_mongo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pablo Cantero
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Ransack Mongo is inspired/based on Ransack but for MongoDB
56
+ email:
57
+ - pablo@pablocantero.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/ransack_mongo.rb
68
+ - lib/ransack_mongo/mongo_adapter.rb
69
+ - lib/ransack_mongo/predicate.rb
70
+ - lib/ransack_mongo/query.rb
71
+ - lib/ransack_mongo/version.rb
72
+ - ransack_mongo.gemspec
73
+ - spec/ransack_mongo/mongo_adapter_spec.rb
74
+ - spec/ransack_mongo/predicate_spec.rb
75
+ - spec/ransack_mongo/query_spec.rb
76
+ - spec/spec_helper.rb
77
+ homepage: ''
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.0.14
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Object-based searching for MongoDB (currently).
101
+ test_files:
102
+ - spec/ransack_mongo/mongo_adapter_spec.rb
103
+ - spec/ransack_mongo/predicate_spec.rb
104
+ - spec/ransack_mongo/query_spec.rb
105
+ - spec/spec_helper.rb