database_repository 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 +14 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/README.md +180 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/database_repository.gemspec +29 -0
- data/lib/database_repository/base.rb +123 -0
- data/lib/database_repository/engine.rb +5 -0
- data/lib/database_repository/version.rb +3 -0
- data/lib/database_repository.rb +6 -0
- data/lib/generators/database_repository/repository_generator.rb +12 -0
- data/lib/generators/database_repository/templates/repository.rb +4 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2759f6ce430a41dbea84649e41ce82436007c320
|
4
|
+
data.tar.gz: afba4cf371e3945ff917fe65a5628d7cdd349edb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0755ed08edbd66294637ed1154f4e99dc75de0b39fb5b4749411f6f7ae530d9add6cb8f502c74d584b72cc7c0f132001f703d619943b2d5bb7faaa0327208ee4
|
7
|
+
data.tar.gz: ef110f7e7992a20d087d51348d56f5ba02d783a55b0e8f436b067a4c6c6230e27d1820b67474056ba5ad8bb2250041ff3fefcc1122c292208d3391fa8da46e00
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
# DatabaseRepository
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/database_repository)
|
4
|
+
[](https://travis-ci.org/piotrjaworski/database_repository)
|
5
|
+
[](https://codeclimate.com/github/piotrjaworski/database_repository/maintainability)
|
6
|
+
[](https://codeclimate.com/github/piotrjaworski/database_repository/test_coverage)
|
7
|
+
|
8
|
+
Welcome to the DatabaseRepository gem!
|
9
|
+
This gem was written to provide an extra layer to your Ruby applications called **repositories**.
|
10
|
+
Repositories are used to interact with our database - execute all queries.
|
11
|
+
Thanks to them, you don't need to write anymore **ActiveRecord code** inside your models, controllers, and services - everything is much simpler to maintain!
|
12
|
+
|
13
|
+
This gem requires just **ActiveRecord**, not **Rails** - feel free to use it without Rails in your Rack applications!
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'database_repository'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install database_repository
|
30
|
+
|
31
|
+
## How to create a repository
|
32
|
+
|
33
|
+
To create a new repository, you just need to run a Rails generator (if you use Rails):
|
34
|
+
|
35
|
+
$ rails generate database_repository:repository your_repository_name
|
36
|
+
|
37
|
+
For example:
|
38
|
+
|
39
|
+
$ rails generate database_repository:repository user
|
40
|
+
|
41
|
+
A new repository will be generated under:
|
42
|
+
- app/repositories/user_repository.rb
|
43
|
+
|
44
|
+
You can also add the new repository to app/repositories, your class should inherit from `DatabaseRepository::Base` class, like:
|
45
|
+
```ruby
|
46
|
+
class UserRepository < DatabaseRepository::Base
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
## Dependencies
|
51
|
+
|
52
|
+
- ActiveRecord >= 3.2
|
53
|
+
|
54
|
+
## Usage
|
55
|
+
|
56
|
+
If you want to use your repository, you should create a new instance of a repository, then call a method which will return some data:
|
57
|
+
```ruby
|
58
|
+
users_repository = UserRepository.new
|
59
|
+
users = users_repository.all
|
60
|
+
user = users_repository.find(2)
|
61
|
+
```
|
62
|
+
|
63
|
+
By default, a repository maps to a model by a repository's name, for example:
|
64
|
+
- UserRepository -> User
|
65
|
+
- LineItemRepository -> LineItem
|
66
|
+
- User::LineItemRepository -> User::LineItem
|
67
|
+
|
68
|
+
If you want to change a model, you can redefine a class name:
|
69
|
+
```ruby
|
70
|
+
class UsersRepository < DatabaseRepository::Base
|
71
|
+
entity 'Model'
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
#### Preimplemented methods:
|
76
|
+
- `all`
|
77
|
+
calls ActiveRecord **Model.all** method.
|
78
|
+
|
79
|
+
- `build(attributes)`
|
80
|
+
calls ActiveRecord **Model.new(attributes)** method.
|
81
|
+
|
82
|
+
- `find(id)`
|
83
|
+
calls ActiveRecord **Model.find(id)** method.
|
84
|
+
Raises `DatabaseRepository::RecordNotFound` if a record is not found.
|
85
|
+
|
86
|
+
- `find_by(id)`
|
87
|
+
calls ActiveRecord **Model.find_by(id)** method.
|
88
|
+
|
89
|
+
- `find_or_initialize_by(id)`
|
90
|
+
calls ActiveRecord **Model.find_or_initialize_by(id)** method.
|
91
|
+
|
92
|
+
- `find_or_create_by(attributes)`
|
93
|
+
calls ActiveRecord **Model.find_or_create_by(attributes)** method.
|
94
|
+
|
95
|
+
- `find_or_create_by!(attributes)`
|
96
|
+
calls ActiveRecord **Model.find_or_create_by!(attributes)** method. Raises `DatabaseRepository::RecordInvalid` if a record is invalid.
|
97
|
+
|
98
|
+
- `first(id)`
|
99
|
+
calls ActiveRecord **Model.first(id)** method.
|
100
|
+
|
101
|
+
- `first!(id)`
|
102
|
+
calls ActiveRecord **Model.first!(id)** method.
|
103
|
+
Raises `DatabaseRepository::RecordNotFound` if a record is not found.
|
104
|
+
|
105
|
+
- `last(id)`
|
106
|
+
calls ActiveRecord **Model.last(id)** method.
|
107
|
+
|
108
|
+
- `last!(id)`
|
109
|
+
calls ActiveRecord **Model.last!(id)** method.
|
110
|
+
Raises `DatabaseRepository::RecordNotFound` if a record is not found.
|
111
|
+
|
112
|
+
- `create(attributes)`
|
113
|
+
calls ActiveRecord **Model.create(attributes)** method.
|
114
|
+
|
115
|
+
- `create!(attributes)`
|
116
|
+
calls ActiveRecord **Model.create!(attributes)** method.
|
117
|
+
Raises `DatabaseRepository::RecordInvalid` if a record is invalid.
|
118
|
+
|
119
|
+
- `update(id, attributes)`
|
120
|
+
calls ActiveRecord **Model.update(id, attributes)** method.
|
121
|
+
|
122
|
+
- `update!(id, attributes)`
|
123
|
+
calls ActiveRecord **Model.update!(id, attributes)** method.
|
124
|
+
Raises `DatabaseRepository::RecordInvalid` if a record is invalid.
|
125
|
+
|
126
|
+
- `update_all(attributes)`
|
127
|
+
calls ActiveRecord **Model.update_all(attributes)** method.
|
128
|
+
|
129
|
+
- `delete(id)`
|
130
|
+
calls ActiveRecord **Model.delete(id)** method.
|
131
|
+
|
132
|
+
- `destroy(id)`
|
133
|
+
calls ActiveRecord **Model.destoy(id)** method.
|
134
|
+
|
135
|
+
- `destroy!(id)`
|
136
|
+
calls ActiveRecord **Model.destroy!(id)** method.
|
137
|
+
Raises `DatabaseRepository::RecordNotDestroyed` if a record cannot be destroyed.
|
138
|
+
|
139
|
+
- `delete_all`
|
140
|
+
calls ActiveRecord **Model.delete_all** method.
|
141
|
+
|
142
|
+
- `destroy_all`
|
143
|
+
calls ActiveRecord **Model.destroy_all** method.
|
144
|
+
|
145
|
+
### Your own methods
|
146
|
+
|
147
|
+
If you want to add your methods, just write a regular ActiveRecord code, for example:
|
148
|
+
```ruby
|
149
|
+
class UserRepository < DatabaseRepository::Base
|
150
|
+
def most_recent_by_name(name:, limit: 5)
|
151
|
+
where(name: name).
|
152
|
+
order(created_at: :desc).
|
153
|
+
limit(limit)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
```
|
157
|
+
|
158
|
+
Then, inside your service, controller or any other class:
|
159
|
+
```ruby
|
160
|
+
UserRepository.new.most_recent_by_name(name: 'Piotr Jaworski', limit: 10)
|
161
|
+
```
|
162
|
+
|
163
|
+
Voila! That's all!
|
164
|
+
|
165
|
+
## TODO
|
166
|
+
|
167
|
+
- Add Sequel support
|
168
|
+
|
169
|
+
## Development
|
170
|
+
|
171
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
172
|
+
|
173
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
174
|
+
|
175
|
+
## Contributing
|
176
|
+
|
177
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/piotrjaworski/database_repository. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
178
|
+
|
179
|
+
## License
|
180
|
+
MIT License. Copyright 2018 Piotr Jaworski - http://piotrjaworski.pl.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "database_repository"
|
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,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "database_repository/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "database_repository"
|
8
|
+
spec.version = DatabaseRepository::VERSION
|
9
|
+
spec.authors = ["Piotr Jaworski"]
|
10
|
+
spec.email = ["piotrek.jaw@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Simple database repository pattern for ActiveRecord.}
|
13
|
+
spec.description = %q{Gem which provides simple database repository pattern for ActiveRecord with predefined basics methods.}
|
14
|
+
spec.homepage = "https://github.com/piotrjaworski/database_repository"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_runtime_dependency "activerecord", ">= 3.2"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
spec.add_development_dependency "simplecov", "~> 0.16"
|
29
|
+
end
|
@@ -0,0 +1,123 @@
|
|
1
|
+
module DatabaseRepository
|
2
|
+
class RecordNotFound < StandardError; end
|
3
|
+
class RecordInvalid < StandardError; end
|
4
|
+
class RecordNotDestroyed < StandardError; end
|
5
|
+
|
6
|
+
class Base
|
7
|
+
class << self
|
8
|
+
attr_accessor :entity_class_name
|
9
|
+
|
10
|
+
def entity(entity_class_name)
|
11
|
+
@entity_class_name = entity_class_name
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def all
|
16
|
+
entity.all
|
17
|
+
end
|
18
|
+
|
19
|
+
def build(**attributes)
|
20
|
+
entity.new(attributes)
|
21
|
+
end
|
22
|
+
|
23
|
+
def find(id)
|
24
|
+
entity.find(id)
|
25
|
+
rescue ActiveRecord::RecordNotFound => e
|
26
|
+
raise DatabaseRepository::RecordNotFound, e.message
|
27
|
+
end
|
28
|
+
|
29
|
+
def find_by(**attributes)
|
30
|
+
entity.find_by(attributes)
|
31
|
+
end
|
32
|
+
|
33
|
+
def find_or_initialize_by(**attributes)
|
34
|
+
entity.find_or_initialize_by(attributes)
|
35
|
+
end
|
36
|
+
|
37
|
+
def find_or_create_by(**attributes)
|
38
|
+
entity.find_or_create_by(attributes)
|
39
|
+
end
|
40
|
+
|
41
|
+
def find_or_create_by!(**attributes)
|
42
|
+
entity.find_or_create_by!(attributes)
|
43
|
+
rescue ActiveRecord::RecordInvalid => e
|
44
|
+
raise DatabaseRepository::RecordInvalid, e.message
|
45
|
+
end
|
46
|
+
|
47
|
+
def first
|
48
|
+
entity.first
|
49
|
+
end
|
50
|
+
|
51
|
+
def first!
|
52
|
+
entity.first!
|
53
|
+
rescue ActiveRecord::RecordNotFound => e
|
54
|
+
raise DatabaseRepository::RecordNotFound, e.message
|
55
|
+
end
|
56
|
+
|
57
|
+
def last
|
58
|
+
entity.last
|
59
|
+
end
|
60
|
+
|
61
|
+
def last!
|
62
|
+
entity.last!
|
63
|
+
rescue ActiveRecord::RecordNotFound => e
|
64
|
+
raise DatabaseRepository::RecordNotFound, e.message
|
65
|
+
end
|
66
|
+
|
67
|
+
def create(**attributes)
|
68
|
+
entity.create(attributes)
|
69
|
+
end
|
70
|
+
|
71
|
+
def create!(**attributes)
|
72
|
+
entity.create!(attributes)
|
73
|
+
rescue ActiveRecord::RecordInvalid => e
|
74
|
+
raise DatabaseRepository::RecordInvalid, e.message
|
75
|
+
end
|
76
|
+
|
77
|
+
def update(id, **attributes)
|
78
|
+
find(id).tap do |record|
|
79
|
+
record.update(attributes)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def update!(id, **attributes)
|
84
|
+
find(id).tap do |record|
|
85
|
+
record.update!(attributes)
|
86
|
+
end
|
87
|
+
rescue ActiveRecord::RecordInvalid => e
|
88
|
+
raise DatabaseRepository::RecordInvalid, e.message
|
89
|
+
end
|
90
|
+
|
91
|
+
def update_all(**attributes)
|
92
|
+
entity.update_all(attributes)
|
93
|
+
end
|
94
|
+
|
95
|
+
def delete(id)
|
96
|
+
find(id).delete
|
97
|
+
end
|
98
|
+
|
99
|
+
def destroy(id)
|
100
|
+
find(id).destroy
|
101
|
+
end
|
102
|
+
|
103
|
+
def destroy!(id)
|
104
|
+
find(id).destroy!
|
105
|
+
rescue ActiveRecord::RecordNotDestroyed => e
|
106
|
+
raise DatabaseRepository::RecordNotDestroyed, e.message
|
107
|
+
end
|
108
|
+
|
109
|
+
def delete_all
|
110
|
+
entity.delete_all
|
111
|
+
end
|
112
|
+
|
113
|
+
def destroy_all
|
114
|
+
entity.destroy_all
|
115
|
+
end
|
116
|
+
|
117
|
+
private
|
118
|
+
|
119
|
+
def entity
|
120
|
+
@_entity ||= Object.const_get(self.class.entity_class_name || self.class.name.match(/(.*)Repository/)[1])
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module DatabaseRepository
|
2
|
+
module Generators
|
3
|
+
class RepositoryGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
argument :repository_name, type: :string
|
6
|
+
|
7
|
+
def create_repository_file
|
8
|
+
template 'repository.rb', File.join('app', 'repositories', "#{repository_name.underscore}_repository.rb")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: database_repository
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Piotr Jaworski
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-05-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.15'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.15'
|
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
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.16'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.16'
|
83
|
+
description: Gem which provides simple database repository pattern for ActiveRecord
|
84
|
+
with predefined basics methods.
|
85
|
+
email:
|
86
|
+
- piotrek.jaw@gmail.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".travis.yml"
|
94
|
+
- Gemfile
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- database_repository.gemspec
|
100
|
+
- lib/database_repository.rb
|
101
|
+
- lib/database_repository/base.rb
|
102
|
+
- lib/database_repository/engine.rb
|
103
|
+
- lib/database_repository/version.rb
|
104
|
+
- lib/generators/database_repository/repository_generator.rb
|
105
|
+
- lib/generators/database_repository/templates/repository.rb
|
106
|
+
homepage: https://github.com/piotrjaworski/database_repository
|
107
|
+
licenses: []
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.6.12
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Simple database repository pattern for ActiveRecord.
|
129
|
+
test_files: []
|