mongoid-dynamic_matchers 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: 0cbf5f1d7095166efb2843e7d9db2f379bf13320
4
+ data.tar.gz: b537936c0d3a76bc79c09c159d56af41c051e956
5
+ SHA512:
6
+ metadata.gz: f0c77c05c01cf3515cb28a32cb5691775f57259aa324a5ce237561b0d8837bc32e1572803fb9bb22146cf6697b24a59d34c37b90575511be581e6975862f4b95
7
+ data.tar.gz: ac49511fbbb768333976dec80c338c60547028f5248d73e6e799d8e73b0d8257fdd9a06fe6325167cd1b5bc96ecf1de8bd4d3fa11cc30b73e4dc7ea9bef3a28e
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 mongoid-dynamic_matchers.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 linyows
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,67 @@
1
+ Mongoid::DynamicMatchers
2
+ ========================
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/mongoid-dynamic_matchers.png)][gem]
5
+
6
+ Support `find_by_xxx_and_yyy` like a activerecord to mongoid.
7
+
8
+ Installation
9
+ ------------
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'mongoid-dynamic_matchers'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install mongoid-dynamic_matchers
22
+
23
+ Usage
24
+ -----
25
+
26
+ ### Example:
27
+
28
+ ```ruby
29
+ class User
30
+ include Mongoid::Document
31
+
32
+ field :first_name
33
+ field :last_name
34
+ field :nick
35
+ end
36
+
37
+ User.create(first_name: 'Thomas', last_name: 'Anderson', nick: 'Neo')
38
+ ```
39
+
40
+ ### Query:
41
+
42
+ ```ruby
43
+ User.find_by_first_name_and_last_name('Thomas', 'Anderson')
44
+ #=> #<User _id: 501a6f6071d7f13750000001, _type: nil, first_name: "Thomas", last_name: "Anderson">
45
+ User.find_by_nick('Smith')
46
+ #=> nil
47
+ ```
48
+
49
+ Contributing
50
+ ------------
51
+
52
+ 1. Fork it
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create new Pull Request
57
+
58
+
59
+ Author
60
+ ------
61
+
62
+ - [linyows](https://github.com/linyows)
63
+
64
+ License
65
+ -------
66
+
67
+ MIT
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module DynamicMatchers
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,112 @@
1
+ module Mongoid
2
+ module DynamicMatchers
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def respond_to?(name, include_private = false)
7
+ match = Method.match(self, name)
8
+ match && match.valid? || super
9
+ end
10
+
11
+ private
12
+
13
+ def method_missing(name, *arguments, &block)
14
+ match = Method.match(self, name)
15
+
16
+ if match && match.valid?
17
+ match.define
18
+ send(name, *arguments, &block)
19
+ else
20
+ super
21
+ end
22
+ end
23
+ end
24
+
25
+ class Method
26
+ @matchers = []
27
+
28
+ class << self
29
+ attr_reader :matchers
30
+
31
+ def match(model, name)
32
+ klass = matchers.find { |k| name =~ k.pattern }
33
+ klass.new(model, name) if klass
34
+ end
35
+
36
+ def pattern
37
+ /^#{prefix}_([_a-zA-Z]\w*)#{suffix}$/
38
+ end
39
+
40
+ def prefix
41
+ raise NotImplementedError
42
+ end
43
+
44
+ def suffix
45
+ ''
46
+ end
47
+ end
48
+
49
+ attr_reader :model, :name, :attribute_names
50
+
51
+ def initialize(model, name)
52
+ @model = model
53
+ @name = name.to_s
54
+ @attribute_names = @name.match(self.class.pattern)[1].split('_and_')
55
+ @attribute_names.map! { |n| @model.aliased_fields[n] || n }
56
+ end
57
+
58
+ def valid?
59
+ attribute_names.all? { |name| model.fields[name] }
60
+ end
61
+
62
+ def define
63
+ ap body
64
+ model.class_eval <<-CODE, __FILE__, __LINE__ + 1
65
+ def self.#{name}(#{signature})
66
+ #{body}
67
+ end
68
+ CODE
69
+ end
70
+
71
+ def body
72
+ raise NotImplementedError
73
+ end
74
+ end
75
+
76
+ module Finder
77
+ def body
78
+ result
79
+ end
80
+
81
+ def result
82
+ "#{finder}(#{attributes_hash}).first"
83
+ end
84
+
85
+ def signature
86
+ attribute_names.join(', ')
87
+ end
88
+
89
+ def attributes_hash
90
+ "{" + attribute_names.map { |name| ":#{name} => #{name}" }.join(',') + "}"
91
+ end
92
+
93
+ def finder
94
+ raise NotImplementedError
95
+ end
96
+ end
97
+
98
+ class FindBy < Method
99
+ Method.matchers << self
100
+ include Finder
101
+
102
+ def self.prefix
103
+ "find_by"
104
+ end
105
+
106
+ def finder
107
+ "where"
108
+ end
109
+ end
110
+ end
111
+ end
112
+ ::Mongoid::Document.__send__(:include, Mongoid::DynamicMatchers)
@@ -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 'mongoid/dynamic_matchers/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mongoid-dynamic_matchers"
8
+ spec.version = Mongoid::DynamicMatchers::VERSION
9
+ spec.authors = ["linyows"]
10
+ spec.email = ["linyows@gmail.com"]
11
+ spec.description = %q{Support `find_by_xxx_and_yyy` like activerecord to mongoid.}
12
+ spec.summary = %q{This is a mongoid extension.}
13
+ spec.homepage = "https://github.com/linyows/mongoid-dynamic_matchers"
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,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-dynamic_matchers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - linyows
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-10 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: Support `find_by_xxx_and_yyy` like activerecord to mongoid.
42
+ email:
43
+ - linyows@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/mongoid/dynamic_matchers.rb
54
+ - lib/mongoid/dynamic_matchers/version.rb
55
+ - mongoid-dynamic_matchers.gemspec
56
+ homepage: https://github.com/linyows/mongoid-dynamic_matchers
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.0.0
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: This is a mongoid extension.
80
+ test_files: []