repositor 0.7.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 025d643d69da495aac7845e4883956df337637f5
4
- data.tar.gz: 221bc1c11d9aaaadd9fab6cbdac45a466fe9a43e
3
+ metadata.gz: bd4aae918948fed1130511573293f27eb04186f6
4
+ data.tar.gz: fd9fed701002b6723f1f2f92c0d162a4192eeaf8
5
5
  SHA512:
6
- metadata.gz: add4bbf1d341b89d56dbec5e01b31424dce4f8366d83d5e8bf02083b2ee57ff65f386c7366c34406562363ea10c380d8511eeed1c379fc86195082ab849b0f25
7
- data.tar.gz: 0f3672f250c82572ee4f0cfa4cf263fcc27c4cb364936075ac3efc60c5d3327cb42e6e7b225250ea9377fedf9b685856cbbc5c9c84b67579481018919ee3a32e
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.find(params[:id])
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
- # include ORM submodule
100
- # now supported only ActiveRecord
101
- # more will be added soon
102
- include Repositor::ActiveRecord
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
- and that's all...
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
- If check what exactly was done, including `Repository` module in base `ApplicationRepo` will add default CRUD methods to all repos that will be inherited from it.
135
-
136
- `Repositor` did for you a lot of dry work. In other case for each repo you must make identical methods, like this:
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
- def new
144
- Product.new
145
- end
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
- def create(product_params)
152
- Product.create(product_params)
153
- end
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
- def update(params)
156
- product.update(params)
157
- end
145
+ product_repo.new_record?(product)
146
+ # same as:
147
+ product.new_record?
158
148
 
159
- def destroy(product)
160
- product.destroy
161
- end
162
- end
149
+ # And not allowed will raise exception
150
+ product_repo.persited?(product) # => NoMethodError
163
151
  ```
164
- If you need to add new method for model, just define it in repo file.
165
- Keep your model skinny.
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 generators that generate repo folder and some `application_repo.rb`
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
- module ActiveRecord
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
- def friendly_find(id)
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
- unless args.drop(1).empty?
28
- args[0].send(method, args.drop(1))
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].send(method)
53
+ args[0].public_send(method)
31
54
  end
32
55
  end
33
56
  end
@@ -0,0 +1,10 @@
1
+ module Repositor
2
+ module InstanceMethodsFilter
3
+ attr_reader :allowed_methods
4
+
5
+ # Define which methods only will be allowed to resend to model instance
6
+ def allow_instance_methods(*methods)
7
+ @allowed_methods = methods
8
+ end
9
+ end
10
+ 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.7.4
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.4.5.1
98
+ rubygems_version: 2.5.1
98
99
  signing_key:
99
100
  specification_version: 4
100
101
  summary: Implementation of Repository Pattern for Rails