From baffa09c364ccbde4856808bbeb22fc092db8522 Mon Sep 17 00:00:00 2001 From: Denis Korobicyn Date: Thu, 5 May 2016 16:18:23 +0600 Subject: [PATCH] feature: removed `data`, and pure virtual method - `find` (#3) --- README.md | 23 ++++++++++++++--------- lib/findit/collections.rb | 20 ++++++++------------ spec/internal/app/finders/post_finder.rb | 11 ++++------- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 6931553..abf2e24 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # Findit -Tired of writing fat controllers? But you must do all these queries.. There is a solution, move it to a Finder class. +Tired of writing fat controllers? But you must do all these queries.. There is a solution, move it to a special Finder class! -Don't write this: +Stop writing this: ```ruby class SomeController @@ -47,7 +47,9 @@ class SomeFinder # some initialize, maybe params parse end - def call + private + + def find # put here you find logic end end @@ -91,13 +93,15 @@ Or install it yourself as: ### Collections -It makes Finder work as Enumerator. -Result can be accessed with `each`, `[]` and `size` methods, but to make things work you *must* implement `call` method. +It makes Finder work as Enumerator with lazy load. +Result can be accessed with `each`, `[]` and `size` methods, but to make things work you *must* implement `find` method. ``` class PostFinder incliude Findit::Collections - def call + private # make it private, so no one call it without lazy load + + def find Post.where(user_id: 1) end end @@ -130,13 +134,14 @@ class PostFinder end # custom initializer, do whatever you want here - def initialize(user, options = {}) - @user = user + def initialize(options = {}) + @user = options.fetch(:user) @query = options[:query] end + private # Here we fetch results. You MUST implement it - def call + def find scope = scope.where(user_id: @user.id) scope = scope.where('description like :query', query: @query) if @query.present? scope diff --git a/lib/findit/collections.rb b/lib/findit/collections.rb index 1f6b0db..067b023 100644 --- a/lib/findit/collections.rb +++ b/lib/findit/collections.rb @@ -9,18 +9,14 @@ # [@user.id, @query] # end # -# cache_tags do -# {user_id: @user.id} -# end -# -# expire_in 30.minutes -# # def initialize(user, options = {}) # @user = user # @query = options[:query] # end # -# def call +# private +# +# def find # scope = scope.where(user_id: @user.id) # scope = scope.where('description like :query', query: @query) if @query.present? # scope @@ -44,7 +40,7 @@ module Collections extend ActiveSupport::Concern included do - delegate :each, :[], :size, :empty?, to: :data + delegate :each, :[], :size, :empty?, to: :call end module ClassMethods @@ -55,13 +51,13 @@ def cache_key(&block) end end - def call + def find end - undef :call + undef :find - def data + def call return @data if defined?(@data) - @data = call + @data = find end end end diff --git a/spec/internal/app/finders/post_finder.rb b/spec/internal/app/finders/post_finder.rb index 7be5bfb..f1c607b 100644 --- a/spec/internal/app/finders/post_finder.rb +++ b/spec/internal/app/finders/post_finder.rb @@ -10,15 +10,12 @@ def initialize(user, options = {}) @query = options[:query] end - def call + private + + def find scope = Post.where(user_id: @user.id) scope = scope.where('text like :query', query: "%#{@query}%") if @query.present? scope end - - def first_post - cache('first_post') do - data.first - end - end end +