findit 1.2.0 → 1.3.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: aa72800635cdabfff2a7363ba8525315b782e37e
4
- data.tar.gz: 05667fd93e82d9352acc0f9f1e97704fd48b3fb4
3
+ metadata.gz: 4f2a7acc1f32c6584aecb5515f041e81bd2410ee
4
+ data.tar.gz: 6549ea60916596d9fde883fe75c0ebefca715438
5
5
  SHA512:
6
- metadata.gz: 069e2eaea3e6c393ab2d29fdba362bd4b7a34231f141f93cebceb4be85bdc1ecdda5877a48ea5a99c236ed932be92d845919785c9f6d68d7556ba219f18a86ea
7
- data.tar.gz: fc4ba671c1af2282bac7ec289d7da2e868ca8f721b4cec0c937c5c1a61665322efd4ff02fe20a1ec716a1e4eb8d16cf05d2d7e583c27dc4600736fb6c450bef0
6
+ metadata.gz: e3b50f8a45783e0c2d873700c22367d0c38f28d1cb14c05615c94c1d637c287d0b4bfdbdbedb1e03d1032089742cca46bcafb6d9a327f142d46b09772328f98b
7
+ data.tar.gz: 4c1a5cead6328b4494646e6c91d81ce1c61c89a20ea55e3b9dbfc84fc89cfa192202336117b53e15c179bb3421c3e5e6a2471ccbe69d0e8ac174609967833753
data/.drone.yml ADDED
@@ -0,0 +1,19 @@
1
+ build:
2
+ test:
3
+ image: abakpress/dind-testing
4
+ pull: true
5
+ privileged: true
6
+ volumes:
7
+ - /home/data/drone/images:/images
8
+ - /home/data/drone/gems:/bundle
9
+ environment:
10
+ - COMPOSE_FILE_EXT=drone
11
+ - RUBY_IMAGE_TAG=2.2-latest
12
+ commands:
13
+ - wrapdocker docker -v
14
+
15
+ - fetch-images
16
+ --image abakpress/ruby-app:$RUBY_IMAGE_TAG
17
+
18
+ - dip provision
19
+ - dip rspec
data/README.md CHANGED
@@ -160,7 +160,7 @@ end
160
160
  <%=render 'post', collection: @posts, as: :post%> # it will automaticly iterate over finder results by each method
161
161
  ```
162
162
 
163
- ## WillPaginate
163
+ ### WillPaginate
164
164
 
165
165
  It adds delegation of [will_paginate](https://github.com/mislav/will_paginate) methods to finder.
166
166
 
@@ -202,6 +202,85 @@ end
202
202
  <%= will_paginate @posts %>
203
203
  ```
204
204
 
205
+ ### Single
206
+
207
+ Adds DSL for cache_key on Finder with single element to find.
208
+
209
+ Example usage:
210
+
211
+ ```ruby
212
+ # app/finders/post_finder.rb
213
+ class PostsFinder
214
+ include Findit::Single
215
+
216
+ cache_key do
217
+ @user
218
+ end
219
+
220
+ def initialize(user)
221
+ @user = user
222
+ end
223
+
224
+ private
225
+
226
+ def find
227
+ Post.where(user: user).last
228
+ end
229
+ end
230
+ ```
231
+
232
+ ### Cache
233
+
234
+ Extends finder with cache possibility. Every call of `call` method will be cached in `Rails.cache`.
235
+ Method `cache options` allows you to add custom options like `expire_in` or `tags` to `Rails.cache.fetch`.
236
+ If you want to disable cache dependent of initialization arguments, you can use `cache?` DSL method.
237
+
238
+ All in one Example:
239
+ ```ruby
240
+ # app/finders/post_finder.rb
241
+ class CachedPostsFinder
242
+ include Findit::Single
243
+ include Findit::Cache
244
+
245
+ cache_key do
246
+ @user
247
+ end
248
+
249
+ cache_options do
250
+ {expire_in: 15.minutes} # This will be directly passed to Rails.cache.fetch
251
+ end
252
+
253
+ def initialize(user)
254
+ @user = user
255
+ end
256
+
257
+ private
258
+
259
+ def find
260
+ Post.where(user: user)
261
+ end
262
+ end
263
+ ```
264
+
265
+ To disable cache for some reasone you can call special method without_cache:
266
+
267
+ ```ruby
268
+ CachedFinder.new(user).without_cache.load # no cache
269
+
270
+ CachedFinder.new(user).load - # will perform cache operations
271
+ ```
272
+
273
+
274
+ If you want this functionality on Collections finder you can add extension `Findit::Collections` alongside with Cache:
275
+ ```ruby
276
+ class SomeFinder
277
+ include Findit::Collections
278
+ include Findit::Cache
279
+
280
+ ...
281
+ end
282
+ ```
283
+
205
284
  ## Contributing
206
285
 
207
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/findit.
286
+ Bug reports and pull requests are welcome on GitHub at https://github.com/abak-press/findit.
data/dip.yml ADDED
@@ -0,0 +1,46 @@
1
+ version: '1'
2
+
3
+ environment:
4
+ DOCKER_RUBY_VERSION: 2.2
5
+ RUBY_IMAGE_TAG: 2.2-latest
6
+ COMPOSE_FILE_EXT: development
7
+ RAILS_ENV: test
8
+
9
+ compose:
10
+ files:
11
+ - docker-compose.yml
12
+ - docker-compose.${COMPOSE_FILE_EXT}.yml
13
+
14
+ interaction:
15
+ sh:
16
+ service: app
17
+
18
+ irb:
19
+ service: app
20
+ command: irb
21
+
22
+ bundle:
23
+ service: app
24
+ command: bundle
25
+
26
+ rake:
27
+ service: app
28
+ command: bundle exec rake
29
+
30
+ appraisal:
31
+ service: app
32
+ command: bundle exec appraisal
33
+
34
+ rspec:
35
+ service: app
36
+ command: bundle exec appraisal bundle exec rspec
37
+
38
+ clean:
39
+ service: app
40
+ command: rm -f Gemfile.lock gemfiles/*.gemfile.*
41
+
42
+ provision:
43
+ - docker volume create --name bundler_data
44
+ - dip clean
45
+ - dip bundle install
46
+ - dip appraisal install
@@ -0,0 +1,12 @@
1
+ version: '2'
2
+
3
+ services:
4
+ app:
5
+ volumes:
6
+ - .:/app
7
+ - bundler-data:/bundle
8
+
9
+ volumes:
10
+ bundler-data:
11
+ external:
12
+ name: bundler_data
@@ -0,0 +1,7 @@
1
+ version: '2'
2
+
3
+ services:
4
+ app:
5
+ volumes:
6
+ - .:/app
7
+ - /bundle:/bundle
@@ -0,0 +1,9 @@
1
+ version: '2'
2
+
3
+ services:
4
+ app:
5
+ image: abakpress/ruby-app:$RUBY_IMAGE_TAG
6
+ environment:
7
+ - BUNDLE_PATH=/bundle/$DOCKER_RUBY_VERSION
8
+ - BUNDLE_CONFIG=/app/.bundle/config
9
+ command: bash
data/findit.gemspec CHANGED
@@ -25,8 +25,9 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "rspec-rails", ">= 3.2"
26
26
  spec.add_development_dependency 'combustion', '>= 0.5'
27
27
  spec.add_development_dependency "appraisal", ">= 2.1.0"
28
- spec.add_development_dependency 'pry-debugger'
28
+ spec.add_development_dependency 'pry'
29
29
  spec.add_development_dependency 'shoulda-matchers', '< 3.0.0'
30
30
  spec.add_development_dependency 'simplecov'
31
31
  spec.add_development_dependency 'sqlite3'
32
+ spec.add_development_dependency 'test-unit'
32
33
  end
@@ -0,0 +1,87 @@
1
+ #
2
+ # Example usage:
3
+ #
4
+ # #/app/finders/posts_finders.rb
5
+ # class CachedPostFinder
6
+ # include Findit::Cache
7
+ #
8
+ # cache_key do
9
+ # [@user.id, @query]
10
+ # end
11
+ #
12
+ # cache? do
13
+ # !@no_cache
14
+ # end
15
+ #
16
+ # cache_options do
17
+ # {expire_in: 15.minutes}
18
+ # end
19
+ #
20
+ # private
21
+ #
22
+ # def find
23
+ # scope = scope.where(user_id: @user.id)
24
+ # scope = scope.where('description like :query', query: @query) if @query.present?
25
+ # scope
26
+ # end
27
+ # end
28
+ #
29
+ # #/app/controllers/posts_controller.rb
30
+ # class PostsController < ApplicationController
31
+ # def show
32
+ # @post = CachedPostFinder.new(user: current_user).call
33
+ # end
34
+ # end
35
+ #
36
+ # #/app/views/posts/index.html.erb
37
+ # # Already cached in finder itself
38
+ # <%= render 'post', post: @post%>
39
+ #
40
+ # # OR if you call it in another finder
41
+ # class CommentOnMyLastPostFinder
42
+ # include Findit::Collections
43
+ #
44
+ # def initialize(user)
45
+ # @user = user
46
+ # end
47
+ #
48
+ # private
49
+ #
50
+ # def find
51
+ # last_post.comments
52
+ # end
53
+ #
54
+ # def last_post
55
+ # @post = CachedPostFinder.new(user).call
56
+ # end
57
+ # end
58
+ #
59
+ module Findit
60
+ module Cache
61
+ extend ActiveSupport::Concern
62
+
63
+ module ClassMethods
64
+ def cache_options(&block)
65
+ define_method(:cache_options) do
66
+ instance_exec(&block)
67
+ end
68
+ end
69
+ end
70
+
71
+ included do
72
+ set_callback :find, :around do |object, block|
73
+ if !defined?(@cache) || @cache
74
+ options = respond_to?(:cache_options) && cache_options
75
+ @data = Rails.cache.fetch(object.cache_key, options) { block.call }
76
+ else
77
+ block.call
78
+ end
79
+ end
80
+ end
81
+
82
+ def without_cache
83
+ @cache = false
84
+ self
85
+ end
86
+ end
87
+ end
@@ -40,24 +40,8 @@ module Findit
40
40
  extend ActiveSupport::Concern
41
41
 
42
42
  included do
43
+ include ::Findit::Single
43
44
  delegate :each, :[], :size, :empty?, :to_ary, :to_a, to: :call
44
45
  end
45
-
46
- module ClassMethods
47
- def cache_key(&block)
48
- define_method :cache_key do
49
- @cache_key ||= ActiveSupport::Cache.expand_cache_key(instance_exec(&block), self.class.name.underscore)
50
- end
51
- end
52
- end
53
-
54
- def find
55
- end
56
- undef :find
57
-
58
- def call
59
- return @data if defined?(@data)
60
- @data = find
61
- end
62
46
  end
63
47
  end
@@ -0,0 +1,63 @@
1
+ #
2
+ # Example usage:
3
+ #
4
+ # #/app/finders/post_finders.rb
5
+ # class PostFinder
6
+ # include Findit::Single
7
+ #
8
+ # cache_key do
9
+ # [@user.id, @query]
10
+ # end
11
+ #
12
+ # def initialize(user, options = {})
13
+ # @user = user
14
+ # @query = options.fetch(:query)
15
+ # end
16
+ #
17
+ # private
18
+ #
19
+ # def find
20
+ # post = scope.find_by('description like :query', query: @query)
21
+ # end
22
+ # end
23
+ #
24
+ # #/app/controllers/posts_controller.rb
25
+ # class PostsController < ApplicationController
26
+ # def show
27
+ # @post = PostFinder.new(current_user, query: 'some desc')
28
+ # end
29
+ # end
30
+ #
31
+ # #/app/views/posts/show.html.erb
32
+ # <% cache(@post, expire_in: 15.minutes) do %>
33
+ # <%= render 'post', post: @post.load%>
34
+ #
35
+ module Findit
36
+ module Single
37
+ extend ActiveSupport::Concern
38
+
39
+ included do
40
+ include ActiveSupport::Callbacks
41
+ define_callbacks :find
42
+ end
43
+
44
+ module ClassMethods
45
+ def cache_key(&block)
46
+ define_method :cache_key do
47
+ @cache_key ||= ActiveSupport::Cache.expand_cache_key(instance_exec(&block), self.class.name.underscore)
48
+ end
49
+ end
50
+ end
51
+
52
+ def find
53
+ end
54
+ undef :find
55
+
56
+ def call
57
+ return @data if defined?(@data)
58
+ res = run_callbacks(:find) { find }
59
+ @data ||= res
60
+ end
61
+ alias_method :load, :call
62
+ end
63
+ end
@@ -1,3 +1,3 @@
1
1
  module Findit
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
data/lib/findit.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'active_support'
2
2
  require "findit/version"
3
+ require 'findit/single'
4
+ require 'findit/cache'
3
5
  require 'findit/collections'
4
6
  require 'findit/will_paginate'
5
7
 
metadata CHANGED
@@ -1,169 +1,183 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: findit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Korobicyn
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-10 00:00:00.000000000 Z
11
+ date: 2017-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- version_requirements: !ruby/object:Gem::Requirement
15
- requirements:
16
- - - '>='
17
- - !ruby/object:Gem::Version
18
- version: '3.1'
19
14
  name: activesupport
20
15
  requirement: !ruby/object:Gem::Requirement
21
16
  requirements:
22
- - - '>='
17
+ - - ">="
23
18
  - !ruby/object:Gem::Version
24
19
  version: '3.1'
25
20
  type: :runtime
26
21
  prerelease: false
27
- - !ruby/object:Gem::Dependency
28
22
  version_requirements: !ruby/object:Gem::Requirement
29
23
  requirements:
30
- - - ~>
24
+ - - ">="
31
25
  - !ruby/object:Gem::Version
32
- version: '10.0'
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
33
28
  name: rake
34
29
  requirement: !ruby/object:Gem::Requirement
35
30
  requirements:
36
- - - ~>
31
+ - - "~>"
37
32
  - !ruby/object:Gem::Version
38
33
  version: '10.0'
39
34
  type: :development
40
35
  prerelease: false
41
- - !ruby/object:Gem::Dependency
42
36
  version_requirements: !ruby/object:Gem::Requirement
43
37
  requirements:
44
- - - '>='
38
+ - - "~>"
45
39
  - !ruby/object:Gem::Version
46
- version: '0'
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
47
42
  name: will_paginate
48
43
  requirement: !ruby/object:Gem::Requirement
49
44
  requirements:
50
- - - '>='
45
+ - - ">="
51
46
  - !ruby/object:Gem::Version
52
47
  version: '0'
53
48
  type: :development
54
49
  prerelease: false
55
- - !ruby/object:Gem::Dependency
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
51
  requirements:
58
- - - '>='
52
+ - - ">="
59
53
  - !ruby/object:Gem::Version
60
- version: '3.2'
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
61
56
  name: rspec
62
57
  requirement: !ruby/object:Gem::Requirement
63
58
  requirements:
64
- - - '>='
59
+ - - ">="
65
60
  - !ruby/object:Gem::Version
66
61
  version: '3.2'
67
62
  type: :development
68
63
  prerelease: false
69
- - !ruby/object:Gem::Dependency
70
64
  version_requirements: !ruby/object:Gem::Requirement
71
65
  requirements:
72
- - - '>='
66
+ - - ">="
73
67
  - !ruby/object:Gem::Version
74
68
  version: '3.2'
69
+ - !ruby/object:Gem::Dependency
75
70
  name: rspec-rails
76
71
  requirement: !ruby/object:Gem::Requirement
77
72
  requirements:
78
- - - '>='
73
+ - - ">="
79
74
  - !ruby/object:Gem::Version
80
75
  version: '3.2'
81
76
  type: :development
82
77
  prerelease: false
83
- - !ruby/object:Gem::Dependency
84
78
  version_requirements: !ruby/object:Gem::Requirement
85
79
  requirements:
86
- - - '>='
80
+ - - ">="
87
81
  - !ruby/object:Gem::Version
88
- version: '0.5'
82
+ version: '3.2'
83
+ - !ruby/object:Gem::Dependency
89
84
  name: combustion
90
85
  requirement: !ruby/object:Gem::Requirement
91
86
  requirements:
92
- - - '>='
87
+ - - ">="
93
88
  - !ruby/object:Gem::Version
94
89
  version: '0.5'
95
90
  type: :development
96
91
  prerelease: false
97
- - !ruby/object:Gem::Dependency
98
92
  version_requirements: !ruby/object:Gem::Requirement
99
93
  requirements:
100
- - - '>='
94
+ - - ">="
101
95
  - !ruby/object:Gem::Version
102
- version: 2.1.0
96
+ version: '0.5'
97
+ - !ruby/object:Gem::Dependency
103
98
  name: appraisal
104
99
  requirement: !ruby/object:Gem::Requirement
105
100
  requirements:
106
- - - '>='
101
+ - - ">="
107
102
  - !ruby/object:Gem::Version
108
103
  version: 2.1.0
109
104
  type: :development
110
105
  prerelease: false
111
- - !ruby/object:Gem::Dependency
112
106
  version_requirements: !ruby/object:Gem::Requirement
113
107
  requirements:
114
- - - '>='
108
+ - - ">="
115
109
  - !ruby/object:Gem::Version
116
- version: '0'
117
- name: pry-debugger
110
+ version: 2.1.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
118
113
  requirement: !ruby/object:Gem::Requirement
119
114
  requirements:
120
- - - '>='
115
+ - - ">="
121
116
  - !ruby/object:Gem::Version
122
117
  version: '0'
123
118
  type: :development
124
119
  prerelease: false
125
- - !ruby/object:Gem::Dependency
126
120
  version_requirements: !ruby/object:Gem::Requirement
127
121
  requirements:
128
- - - <
122
+ - - ">="
129
123
  - !ruby/object:Gem::Version
130
- version: 3.0.0
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
131
126
  name: shoulda-matchers
132
127
  requirement: !ruby/object:Gem::Requirement
133
128
  requirements:
134
- - - <
129
+ - - "<"
135
130
  - !ruby/object:Gem::Version
136
131
  version: 3.0.0
137
132
  type: :development
138
133
  prerelease: false
139
- - !ruby/object:Gem::Dependency
140
134
  version_requirements: !ruby/object:Gem::Requirement
141
135
  requirements:
142
- - - '>='
136
+ - - "<"
143
137
  - !ruby/object:Gem::Version
144
- version: '0'
138
+ version: 3.0.0
139
+ - !ruby/object:Gem::Dependency
145
140
  name: simplecov
146
141
  requirement: !ruby/object:Gem::Requirement
147
142
  requirements:
148
- - - '>='
143
+ - - ">="
149
144
  - !ruby/object:Gem::Version
150
145
  version: '0'
151
146
  type: :development
152
147
  prerelease: false
153
- - !ruby/object:Gem::Dependency
154
148
  version_requirements: !ruby/object:Gem::Requirement
155
149
  requirements:
156
- - - '>='
150
+ - - ">="
157
151
  - !ruby/object:Gem::Version
158
152
  version: '0'
153
+ - !ruby/object:Gem::Dependency
159
154
  name: sqlite3
160
155
  requirement: !ruby/object:Gem::Requirement
161
156
  requirements:
162
- - - '>='
157
+ - - ">="
163
158
  - !ruby/object:Gem::Version
164
159
  version: '0'
165
160
  type: :development
166
161
  prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: test-unit
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
167
181
  description:
168
182
  email:
169
183
  - deniskorobitcin@gmail.com
@@ -171,18 +185,24 @@ executables: []
171
185
  extensions: []
172
186
  extra_rdoc_files: []
173
187
  files:
174
- - .gitignore
188
+ - ".drone.yml"
189
+ - ".gitignore"
175
190
  - Appraisals
176
191
  - Gemfile
177
- - Makefile
178
192
  - README.md
179
193
  - Rakefile
180
194
  - bin/console
181
195
  - bin/setup
182
196
  - config.ru
197
+ - dip.yml
198
+ - docker-compose.development.yml
199
+ - docker-compose.drone.yml
200
+ - docker-compose.yml
183
201
  - findit.gemspec
184
202
  - lib/findit.rb
203
+ - lib/findit/cache.rb
185
204
  - lib/findit/collections.rb
205
+ - lib/findit/single.rb
186
206
  - lib/findit/version.rb
187
207
  - lib/findit/will_paginate.rb
188
208
  homepage: https://github.com/abak-press/findit
@@ -194,17 +214,17 @@ require_paths:
194
214
  - lib
195
215
  required_ruby_version: !ruby/object:Gem::Requirement
196
216
  requirements:
197
- - - '>='
217
+ - - ">="
198
218
  - !ruby/object:Gem::Version
199
219
  version: '0'
200
220
  required_rubygems_version: !ruby/object:Gem::Requirement
201
221
  requirements:
202
- - - '>='
222
+ - - ">="
203
223
  - !ruby/object:Gem::Version
204
224
  version: '0'
205
225
  requirements: []
206
226
  rubyforge_project:
207
- rubygems_version: 2.4.8
227
+ rubygems_version: 2.5.1
208
228
  signing_key:
209
229
  specification_version: 4
210
230
  summary: Extensions for Finder classes
data/Makefile DELETED
@@ -1,23 +0,0 @@
1
- RAILS_ENV = test
2
- BUNDLE_VERSION = 1.11.2
3
- BUNDLE = RAILS_ENV=${RAILS_ENV} bundle _${BUNDLE_VERSION}_
4
- BUNDLE_OPTIONS = -j 2
5
- RSPEC = rspec
6
- APPRAISAL = appraisal
7
-
8
- all: test
9
-
10
- test: bundler/install appraisal/install
11
- ${BUNDLE} exec ${APPRAISAL} ${RSPEC} spec 2>&1
12
-
13
- bundler/install:
14
- gem install bundler --version=${BUNDLE_VERSION};
15
- ${BUNDLE} install ${BUNDLE_OPTIONS}
16
-
17
- appraisal/install:
18
- ${BUNDLE} exec ${APPRAISAL} install
19
-
20
- clean:
21
- rm -f Gemfile.lock
22
- rm -rf gemfiles
23
-