hanami-mongoid 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/README.md +71 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/hanami-mongoid.gemspec +30 -0
- data/lib/hanami/mongoid.rb +10 -0
- data/lib/hanami/mongoid/repository.rb +41 -0
- data/lib/hanami/mongoid/version.rb +5 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7162e16b03ff0cd75aeeefabbd277145233ce4490cf69b906aec430ee8752154
|
4
|
+
data.tar.gz: 4f6ec29482d86fcf070476e727a0ebada89708f082cdd5a5a20e4a6bbb9a736c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4dc1b5d3274d3a25f44838f865d1636df5a590f9e7cdaefc1f845d6dbc5c4c3db7e71075618ea1710ac442b5b1e120558e7cb02b52440c8ccae6b6115e2f64d2
|
7
|
+
data.tar.gz: 4ce0c86c9760f7589a16ff9fe5d4ab5b12fa0dae5bdff77ec5b214715e781ae5337eebb8e55924fa5c35c6fb363d2dc4251109b50e01067d565ae7138a5e4208
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# Hanami::Mongoid
|
2
|
+
|
3
|
+
This simple gem aims to replicate Hanami concept of _repositories_ when using MongoDB as database.
|
4
|
+
It adds a class `Hanami::Mongoid::Repository` which is a substitute for the original `Hanami::Repository` class.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Use thi line to your application's Gemfile instead of `hanami-model`:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'hanami-mongoid'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle install
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
When generating a new model, i.e. with
|
21
|
+
|
22
|
+
$ hanami generate model book
|
23
|
+
|
24
|
+
Simpy delete the `db` folder (since MongoDB doesn't care for migrations) and change the following generated files:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
# lib/bookshelf/entities/book.rb
|
28
|
+
class Book < Hanami::Entity
|
29
|
+
end
|
30
|
+
```
|
31
|
+
```ruby
|
32
|
+
# lib/bookshelf/entities/book.rb
|
33
|
+
class Book
|
34
|
+
include Mongoid::Document
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
And:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
# lib/bookshelf/repositories/book_repository.rb
|
42
|
+
class BookRepository < Hanami::Repository
|
43
|
+
end
|
44
|
+
```
|
45
|
+
```ruby
|
46
|
+
# lib/bookshelf/repositories/book_repository.rb
|
47
|
+
class BookRepository < Hanami::Mongoid::Repository
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
This way you can keep separated the repository and entity logic as Hanami suggests.
|
52
|
+
You can call the usual database related methods using the repository:
|
53
|
+
```ruby
|
54
|
+
BookRepository.first # #<Book:0x00007fd8b6bea578>
|
55
|
+
BookRepository.find('5af4c697699bbdaad') # #<Book id: 5af4c697699bbdaad>
|
56
|
+
```
|
57
|
+
Or even collection specific methods, for example `insert_many` or `aggregate`.
|
58
|
+
|
59
|
+
And of course you can add custom queries in the repository:
|
60
|
+
```ruby
|
61
|
+
# lib/bookshelf/repositories/book_repository.rb
|
62
|
+
class BookRepository < Hanami::Mongoid::Repository
|
63
|
+
def most_recent_for_author(author, limit = 8)
|
64
|
+
self.where(author: author).order_by(created_at: :desc).limit(limit)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Pluvie/hanami-mongoid.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hanami/mongoid"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "hanami/mongoid/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hanami-mongoid"
|
8
|
+
spec.version = Hanami::Mongoid::VERSION
|
9
|
+
spec.authors = ["Francesco Ballardin"]
|
10
|
+
spec.email = ["francesco.ballardin@develonproject.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Adds repositories and wraps Mongoid methods for Hanami.}
|
13
|
+
spec.description = %q{Adds repositories and wraps Mongoid methods for Hanami.}
|
14
|
+
spec.homepage = "https://github.com/Pluvie/hanami-mongoid"
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_dependency "mongoid"
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
30
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Hanami
|
2
|
+
module Mongoid
|
3
|
+
class Repository
|
4
|
+
|
5
|
+
##
|
6
|
+
# Model class
|
7
|
+
#
|
8
|
+
# @return [Class] the model class
|
9
|
+
def self.model_klass
|
10
|
+
Object.const_get self.name.gsub('Repository', '')
|
11
|
+
end
|
12
|
+
|
13
|
+
##
|
14
|
+
# Forwards common repository methods to the model's class
|
15
|
+
%i( find where any_of all first last
|
16
|
+
new create update_all destroy_all delete_all ).each do |method|
|
17
|
+
define_singleton_method method do |*args|
|
18
|
+
puts self.inspect
|
19
|
+
self.model_klass.send method, *args
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
##
|
24
|
+
# Forwards common repository methods to the model's instance
|
25
|
+
%i( update_attributes update destroy delete ).each do |method|
|
26
|
+
define_method method do |*args|
|
27
|
+
self.send method, *args
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
##
|
32
|
+
# Forwards common repository methods to the model's collection
|
33
|
+
%i( insert_many aggregate ).each do |method|
|
34
|
+
define_singleton_method method do |*args|
|
35
|
+
self.model_klass.collection.send method, *args
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hanami-mongoid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Francesco Ballardin
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-03-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mongoid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
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: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: Adds repositories and wraps Mongoid methods for Hanami.
|
70
|
+
email:
|
71
|
+
- francesco.ballardin@develonproject.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- hanami-mongoid.gemspec
|
85
|
+
- lib/hanami/mongoid.rb
|
86
|
+
- lib/hanami/mongoid/repository.rb
|
87
|
+
- lib/hanami/mongoid/version.rb
|
88
|
+
homepage: https://github.com/Pluvie/hanami-mongoid
|
89
|
+
licenses: []
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.7.6
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Adds repositories and wraps Mongoid methods for Hanami.
|
111
|
+
test_files: []
|