repositor 0.7.4 → 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 +4 -4
- data/README.md +41 -52
- data/lib/repositor/active_record.rb +31 -8
- data/lib/repositor/instance_allow.rb +10 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bd4aae918948fed1130511573293f27eb04186f6
|
4
|
+
data.tar.gz: fd9fed701002b6723f1f2f92c0d162a4192eeaf8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9c69491ab1086fb5b8a976ca4b6c94ee1a7d4049407e32c0516c71961e342fa9ab0a42c6d28ec4bc06afd5ee2702653d77b4107e344e245994f1d63113050373
|
7
|
+
data.tar.gz: 5de69c59cbfc9d6b96ee4fdd78119810241ee4e412b483b66ead93f96e065467a3fca6ec06f9aa98edfac867f65f35973dd1dbfa4075a543f0f5273342650aad
|
data/README.md
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# Repositor
|
2
2
|
|
3
|
-
[gem]: https://rubygems.org/gems/repositor
|
4
|
-
|
5
3
|
## Installation & Setup
|
6
4
|
|
7
5
|
Manual:
|
@@ -63,7 +61,7 @@ class ProductsController < ApplicationController
|
|
63
61
|
# You can use it for at view show action just `product` method
|
64
62
|
# or for _form partial also `product`
|
65
63
|
def product
|
66
|
-
@product ||= repo.
|
64
|
+
@product ||= repo.find_or_initialize(params[:id])
|
67
65
|
end
|
68
66
|
|
69
67
|
# Second helper that allow to cache all collection
|
@@ -85,7 +83,6 @@ end
|
|
85
83
|
|
86
84
|
## How to use
|
87
85
|
|
88
|
-
|
89
86
|
**By generator:**
|
90
87
|
|
91
88
|
`rails generate repos`
|
@@ -95,11 +92,19 @@ end
|
|
95
92
|
In `app` directory you need to create new `repos` directory . Recomended to create `application_repo.rb` and inherit from it all repos, so you could keep all your repos under single point of inheritance.
|
96
93
|
|
97
94
|
```ruby
|
98
|
-
class ApplicationRepo
|
99
|
-
#
|
100
|
-
#
|
101
|
-
#
|
102
|
-
|
95
|
+
class ApplicationRepo < Repositor::ActiveRecordAdapter
|
96
|
+
# now supported only ActiveRecord but will be added more soon
|
97
|
+
#
|
98
|
+
# Adapter allow you to use 4 default methods for CRUD:
|
99
|
+
# :new, :create, :update, :destroy
|
100
|
+
#
|
101
|
+
# Only 2 for finding/quering records
|
102
|
+
# :find, :all
|
103
|
+
#
|
104
|
+
# And additional helpers
|
105
|
+
# find_or_initialize(id, friendly: false) => support for friendly_id gem
|
106
|
+
# or
|
107
|
+
# friendly_find(slugged_id)
|
103
108
|
end
|
104
109
|
```
|
105
110
|
|
@@ -109,62 +114,46 @@ class ProductRepo < ApplicationRepo
|
|
109
114
|
# here you have default methods for repository actions
|
110
115
|
# if you want communicate with model class,
|
111
116
|
# just can use model method to send it any method you need
|
112
|
-
def all_with_name_john
|
113
|
-
model.where(name: 'John')
|
114
|
-
end
|
115
|
-
|
116
117
|
def create_if(params, condition)
|
117
118
|
create(params) if condition
|
118
119
|
end
|
119
|
-
end
|
120
|
-
```
|
121
|
-
|
122
|
-
Also `Repositor` will redirect all missing methods to instance if it was passed as first argument:
|
123
|
-
```ruby
|
124
|
-
product_repo.new_record?(product)
|
125
|
-
```
|
126
|
-
same as:
|
127
|
-
```ruby
|
128
|
-
product.new_record?
|
129
|
-
```
|
130
|
-
Only with the reason that you are not linked with data, only with it repo.
|
131
120
|
|
132
|
-
|
121
|
+
# Very good approach is that you got a place where you can
|
122
|
+
# control persistence process, define some logic and reuse it everywhere.
|
123
|
+
def update(record, params)
|
124
|
+
result = record.update(params) if params[:ok] == 'ok'
|
133
125
|
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
```ruby
|
138
|
-
class ProductRepo
|
139
|
-
def all
|
140
|
-
Product.all
|
126
|
+
if result
|
127
|
+
# trigger some event
|
128
|
+
end
|
141
129
|
end
|
142
130
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
def find(product_id)
|
148
|
-
Product.find(product_id)
|
131
|
+
# You also can compose queries
|
132
|
+
# But recommended to extract such methods to QueryObject (aka finder)
|
133
|
+
def all_with_name_john
|
134
|
+
model.where(name: 'John')
|
149
135
|
end
|
136
|
+
end
|
137
|
+
```
|
150
138
|
|
151
|
-
|
152
|
-
|
153
|
-
|
139
|
+
Also `Repositor` allow redirect defined record methods to instance if it was passed as first argument:
|
140
|
+
```ruby
|
141
|
+
class ProductRepo < ApplicationRepo
|
142
|
+
allow_instance_methods :new_record?
|
143
|
+
end
|
154
144
|
|
155
|
-
|
156
|
-
|
157
|
-
|
145
|
+
product_repo.new_record?(product)
|
146
|
+
# same as:
|
147
|
+
product.new_record?
|
158
148
|
|
159
|
-
|
160
|
-
|
161
|
-
end
|
162
|
-
end
|
149
|
+
# And not allowed will raise exception
|
150
|
+
product_repo.persited?(product) # => NoMethodError
|
163
151
|
```
|
164
|
-
|
165
|
-
|
152
|
+
Only with the reason that you are not linked with data, only with it repo.
|
153
|
+
|
154
|
+
**Keep your model skinny and without business logic.**
|
166
155
|
|
167
156
|
## TODO
|
168
157
|
* Add mongoid support
|
169
|
-
* Add
|
158
|
+
* Add sequel support
|
170
159
|
* Some improvements ? =)
|
@@ -1,8 +1,11 @@
|
|
1
1
|
require 'active_support/core_ext/module/delegation'
|
2
2
|
require 'active_support/inflector'
|
3
|
+
require 'repositor/instance_allow'
|
3
4
|
|
4
5
|
module Repositor
|
5
|
-
|
6
|
+
class ActiveRecordAdapter
|
7
|
+
extend InstanceMethodsFilter
|
8
|
+
|
6
9
|
attr_reader :model
|
7
10
|
|
8
11
|
delegate :find, :all, :new, :create, :update, :destroy, to: :model
|
@@ -11,23 +14,43 @@ module Repositor
|
|
11
14
|
@model = model || self.class.to_s.chomp("Repo").singularize.constantize
|
12
15
|
end
|
13
16
|
|
14
|
-
|
15
|
-
model.friendly.find(id)
|
16
|
-
end
|
17
|
-
|
17
|
+
# Common find process with supporting of friendly_id params
|
18
18
|
def find_or_initialize(id, friendly: false)
|
19
19
|
friendly ? friendly_find(id) : model.find(id)
|
20
20
|
rescue ::ActiveRecord::RecordNotFound
|
21
21
|
model.new
|
22
22
|
end
|
23
23
|
|
24
|
+
# Support for `friendly_id` gem out of box
|
25
|
+
def friendly_find(slugged_id)
|
26
|
+
model.friendly.find(slugged_id)
|
27
|
+
end
|
28
|
+
|
29
|
+
# All methods that have instance object as first argument
|
30
|
+
# will be redirected to itself with all other arguments
|
31
|
+
# `some_repo.new_record?(record)`
|
32
|
+
# same as
|
33
|
+
# `record.new_record?`
|
34
|
+
# But gives you more control under persistence process
|
24
35
|
def method_missing(method, *args)
|
36
|
+
# Break if first argument was not a model record
|
25
37
|
return unless args[0].instance_of? model
|
26
38
|
|
27
|
-
|
28
|
-
|
39
|
+
# Super if allowed_methods not defined or in not included in array
|
40
|
+
am = self.class.allowed_methods
|
41
|
+
super if am.nil? || !am.include?(method)
|
42
|
+
|
43
|
+
resend_method(method, args)
|
44
|
+
end
|
45
|
+
|
46
|
+
def resend_method(method, args)
|
47
|
+
# All except first argumet is a record params
|
48
|
+
params = args.drop(1)
|
49
|
+
|
50
|
+
if params.any?
|
51
|
+
args[0].public_send(method, params)
|
29
52
|
else
|
30
|
-
args[0].
|
53
|
+
args[0].public_send(method)
|
31
54
|
end
|
32
55
|
end
|
33
56
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: repositor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Volodya Sveredyuk
|
@@ -74,6 +74,7 @@ files:
|
|
74
74
|
- lib/generators/repos_generator.rb
|
75
75
|
- lib/repositor.rb
|
76
76
|
- lib/repositor/active_record.rb
|
77
|
+
- lib/repositor/instance_allow.rb
|
77
78
|
homepage: https://github.com/sveredyuk/repositor
|
78
79
|
licenses:
|
79
80
|
- MIT
|
@@ -94,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
95
|
version: '0'
|
95
96
|
requirements: []
|
96
97
|
rubyforge_project:
|
97
|
-
rubygems_version: 2.
|
98
|
+
rubygems_version: 2.5.1
|
98
99
|
signing_key:
|
99
100
|
specification_version: 4
|
100
101
|
summary: Implementation of Repository Pattern for Rails
|