meta_search_mongoid 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8d61e5565d1051efd558464aaaadfd53c46b6502
4
+ data.tar.gz: fbd642cc33a53e4d83628c73a54fd6a23925a62f
5
+ SHA512:
6
+ metadata.gz: 2e5479b61d1f564f18b55c735c08144c5885a1db2aa2c90c47db84b50d41e6a8f0c51f0b8b792e44fa9fa7d71cc14cd21e0161ce2d1db6bd17197e72a8ae8023
7
+ data.tar.gz: 338e5306235ca3e9982f6f6a71f59fbb383fbae32ce228214f916d492945ff45c26cc7460d4dd1dfcfc24e3605fbeff3cad4e80b12acb3dcd2972e42ef612cda
@@ -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 meta_search_mongoid.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Manu S Ajith <neo@codingarena.in>
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.
@@ -0,0 +1,37 @@
1
+ # MetaSearchMongoid
2
+
3
+ MetaSearch extended for mongoid
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'meta_search_mongoid'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install meta_search_mongoid
18
+
19
+ ## Usage
20
+
21
+
22
+ `include MetaSearchMongoid`
23
+
24
+ in the model you wish to add metasearch support.
25
+
26
+ ##ToDo##
27
+
28
+
29
+ 1. Add Specs
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ require "meta_search_mongoid/conditions"
2
+ require "meta_search_mongoid/search"
3
+ require "meta_search_mongoid/version"
@@ -0,0 +1,33 @@
1
+ module MetaSearchMongoid
2
+ module Conditions
3
+ NUMBERS = [:integer, :float, :decimal]
4
+ STRINGS = [:string, :text, :binary]
5
+ DATES = [:date]
6
+ TIMES = [:datetime, :timestamp, :time]
7
+ BOOLEANS = [:boolean]
8
+ ALL_TYPES = NUMBERS + STRINGS + DATES + TIMES + BOOLEANS
9
+
10
+ CONDITIONS = [
11
+ ['equals', 'eq', {:validator => Proc.new {|param| !param.blank? || (param == false)}}],
12
+ ['does_not_equal', 'ne', 'not_eq', {:types => ALL_TYPES, :predicate => :not_eq}],
13
+ ['contains', 'like', 'matches', {:types => STRINGS, :predicate => :matches, :formatter => '"%#{param}%"'}],
14
+ ['does_not_contain', 'nlike', 'not_matches', {:types => STRINGS, :predicate => :does_not_match, :formatter => '"%#{param}%"'}],
15
+ ['starts_with', 'sw', {:types => STRINGS, :predicate => :matches, :formatter => '"#{param}%"'}],
16
+ ['does_not_start_with', 'dnsw', {:types => STRINGS, :predicate => :does_not_match, :formatter => '"#{param}%"'}],
17
+ ['ends_with', 'ew', {:types => STRINGS, :predicate => :matches, :formatter => '"%#{param}"'}],
18
+ ['does_not_end_with', 'dnew', {:types => STRINGS, :predicate => :does_not_match, :formatter => '"%#{param}"'}],
19
+ ['greater_than', 'gt', {:types => (NUMBERS + DATES + TIMES), :predicate => :gt}],
20
+ ['less_than', 'lt', {:types => (NUMBERS + DATES + TIMES), :predicate => :lt}],
21
+ ['greater_than_or_equal_to', 'gte', 'gteq', {:types => (NUMBERS + DATES + TIMES), :predicate => :gteq}],
22
+ ['less_than_or_equal_to', 'lte', 'lteq', {:types => (NUMBERS + DATES + TIMES), :predicate => :lteq}],
23
+ ['in', {:types => ALL_TYPES, :predicate => :in}],
24
+ ['not_in', 'ni', 'not_in', {:types => ALL_TYPES, :predicate => :not_in}],
25
+ ['is_true', {:types => BOOLEANS, :skip_compounds => true}],
26
+ ['is_false', {:types => BOOLEANS, :skip_compounds => true, :formatter => Proc.new {|param| !param}}],
27
+ ['is_present', {:types => (ALL_TYPES - BOOLEANS), :predicate => :not_eq_all, :skip_compounds => true, :cast => :boolean, :formatter => Proc.new {|param| [nil, '']}}],
28
+ ['is_blank', {:types => (ALL_TYPES - BOOLEANS), :predicate => :eq_any, :skip_compounds => true, :cast => :boolean, :formatter => Proc.new {|param| [nil, '']}}],
29
+ ['is_null', {:types => ALL_TYPES, :skip_compounds => true, :cast => :boolean, :formatter => Proc.new {|param| nil}}],
30
+ ['is_not_null', {:types => ALL_TYPES, :predicate => :not_eq, :skip_compounds => true, :cast => :boolean, :formatter => Proc.new {|param| nil}}]
31
+ ]
32
+ end
33
+ end
@@ -0,0 +1,105 @@
1
+ module MetaSearchMongoid
2
+ require 'delegate'
3
+ class SearchBuilder < SimpleDelegator
4
+
5
+ def initialize relation, params, options
6
+ super(relation)
7
+ @relation = relation
8
+ @params, @options = params, options
9
+ end
10
+
11
+ def build
12
+ params.each_pair do |field_query, value|
13
+ field, query = field_query.to_s.scan(metasearch_regexp).first
14
+ case query.to_sym
15
+ when :equals, :eq
16
+ @relation = relation.where(field => value)
17
+ when :does_not_equal, :ne, :not_eq
18
+ @relation = relation.where(field.to_sym.ne => value)
19
+ when :contains, :like, :matches
20
+ @relation = relation.where(field => /#{value}/)
21
+ when :does_not_contain, :nlike, :not_matches
22
+ @relation = relation.where(field.to_sym.not => /#{value}/)
23
+ when :starts_with, :sw
24
+ @relation = relation.where(field.to_sym => /\A#{Regexp.quote(value)}/)
25
+ when :does_not_start_with, :dnsw
26
+ @relation = relation.where(field.to_sym.not => /\A#{Regexp.quote(value)}/)
27
+ when :ends_with, :ew
28
+ @relation = relation.where(field.to_sym => /#{Regexp.quote(value)}\z/)
29
+ when :does_not_end_with, :dnew
30
+ @relation = relation.where(field.to_sym.not => /#{Regexp.quote(value)}\z/)
31
+ when :greater_than, :gt
32
+ @relation = relation.where(field.to_sym.gt => value)
33
+ when :less_than, :lt
34
+ @relation = relation.where(field.to_sym.lt => value)
35
+ when :greater_than_or_equal_to, :gte, :gteq
36
+ @relation = relation.where(field.to_sym.gte => value)
37
+ when :less_than_or_equal_to, :lte, :lteq
38
+ @relation = relation.where(field.to_sym.lte => value)
39
+ when :in
40
+ @relation = relation.where(field.to_sym.in => Array.wrap(value))
41
+ when :not_in, :ni
42
+ @relation = relation.where(field.to_sym.nin => Array.wrap(value))
43
+ when :is_true
44
+ @relation = relation.where(field => true)
45
+ when :is_false
46
+ @relation = relation.where(field => false)
47
+ when :is_present
48
+ @relation = relation.where(field.to_sym.exists => true)
49
+ when :is_blank
50
+ @relation = relation.where(field.to_sym.exists => false)
51
+ when :is_null
52
+ @relation = relation.where(field => nil)
53
+ when :is_not_null
54
+ @relation = relation.where(field.to_sym.ne => nil)
55
+ else
56
+ raise [field_query, value].inspect
57
+ end
58
+ end
59
+ @relation
60
+ end
61
+
62
+ attr_reader :relation, :params, :options
63
+
64
+ def base
65
+ self
66
+ end
67
+
68
+ def klass
69
+ relation
70
+ end
71
+
72
+ def method_missing name, *attrs, &block
73
+ relation.send(name, *attrs, &block)
74
+ end
75
+
76
+ def respond_to? name, include_private = false
77
+ name.to_s =~ metasearch_regexp or super
78
+ end
79
+
80
+ def method_missing name, *args, &block
81
+ if name =~ metasearch_regexp
82
+ params[name]
83
+ else
84
+ super
85
+ end
86
+ end
87
+
88
+ def metasearch_regexp
89
+ field_names = klass.fields.map(&:second).map(&:name)
90
+ conditions = MetaSearchMongoid::Conditions::CONDITIONS.map {|condition| condition[0...-1]}
91
+ /\A(#{field_names.join('|')})_(#{conditions.join('|')})\z/
92
+ end
93
+ end
94
+
95
+ extend ActiveSupport::Concern
96
+ module ClassMethods
97
+ def metasearch(params = nil, options = nil)
98
+ options ||= {}
99
+ params ||= {}
100
+ MetaSearchMongoid::SearchBuilder.new(self, params, options).build
101
+ end
102
+ alias_method :search, :metasearch unless respond_to?(:search)
103
+ end
104
+
105
+ end
@@ -0,0 +1,3 @@
1
+ module MetaSearchMongoid
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'meta_search_mongoid/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "meta_search_mongoid"
8
+ spec.version = MetaSearchMongoid::VERSION
9
+ spec.authors = ["Manu S Ajith"]
10
+ spec.email = ["neo@codingarena.in"]
11
+ spec.description = %q{Metasearch extended to mongoid.}
12
+ spec.summary = %q{Metasearch for mongoid documents}
13
+ spec.homepage = "http://github.com/manusajith/meta_search_mongoid"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: meta_search_mongoid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Manu S Ajith
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-16 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: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
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
+ description: Metasearch extended to mongoid.
42
+ email:
43
+ - neo@codingarena.in
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/meta_search_mongoid.rb
54
+ - lib/meta_search_mongoid/conditions.rb
55
+ - lib/meta_search_mongoid/search.rb
56
+ - lib/meta_search_mongoid/version.rb
57
+ - meta_search_mongoid.gemspec
58
+ homepage: http://github.com/manusajith/meta_search_mongoid
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.0.5
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Metasearch for mongoid documents
82
+ test_files: []