ruby_search_object 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MDkyYWQ4ODYwNTRhYWU5YzMwMDZlYTJkYjM5ODk5MmJjNDhmZGI4Yw==
5
+ data.tar.gz: !binary |-
6
+ YzI1ZDU5ZjJhMGRkMTcxMTIwMmE2NzdiOTkxNjY5YjE2NzFiZTU0Yw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZmFjY2FhYjc5ZmM5ZTE0OGMxZDZjMmE3MzI5NTJmYzU0NTJhZmU4YjA0MmEz
10
+ ZjFlZDMzZjkwOTBkMDczYzYwNTZmMGVjZWU4YzUyODdiZmNiOTM3ODE0MjY3
11
+ ZjFlODA2Nzk2ZGE4NDE3MTE3Y2JlZDM2MjQ1YmY1OWRjMWM1NWY=
12
+ data.tar.gz: !binary |-
13
+ NjFhMWI4NTFjMTMxODc0MzdiMTAyYjcxOWMxY2VjOGMxZDBkYTdmYjk0OWYw
14
+ ODRiNjYzNDUwMjI1Zjk1M2FjZWMxMmFmNGJiYzBkNzE0NTgwZGNlMDQyNDM4
15
+ ZTIwY2QzNzc0NTVmOWQ5N2Q0Mzc3ZjQyYjRmOGM1MzUxZWY3NGU=
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby_search_object.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Vijayanand Nandam
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,82 @@
1
+ # RubySearchObject
2
+
3
+ This gem provides a layout for implementation of search on activerecord / sequel or any other ORM based models. What this gem does is it creates and instace of the search handler class for the model and invokes the search method on that class.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ruby_search_object'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ruby_search_object
18
+
19
+ Then include in you class like:
20
+
21
+ class Article < ActiveRecord::Base
22
+ include RubySearchObject
23
+ end
24
+
25
+ If not using in rails you might have to load the gem using
26
+
27
+ require 'ruby_search_object'
28
+
29
+
30
+ ## Usage
31
+ Lets take the classic example of a **Article** search in an rails application.
32
+ When you include the `RubySearchObject` module adds a method called `search` to the class. Then you can pass the parameters to the search method which will then be passed to you search handler object and return the results. Every time you call the method search on the Article class a new search object is created. The search method creates a new instance of class `ArticleSearch` when included in class `Article`. So it appends `Search` to the class name so your search logic should go into the class named `ArticleSearch` or what ever the class name you are using + Search.
33
+
34
+
35
+ Example Implementaion of search.
36
+ Lets say each `Article` has a title, description and published date.
37
+
38
+ The Article Class will be like above
39
+
40
+ class Article < ActiveRecord::Base
41
+ include RubySearchObject
42
+ end
43
+
44
+ An example article search class would be like (prefarably placed in app/services or app/search_handlers folder)
45
+
46
+ class ArticleSearch
47
+ def initialize params = {}
48
+ @title = params[:title]
49
+ @start_date = params[:start_date]
50
+ @end_date = params[:end_date]
51
+ @articles = Article.order('id asc')
52
+ end
53
+
54
+ def search
55
+ build_title_query
56
+ build_date_range_query
57
+ @articles
58
+ end
59
+
60
+ def build_title_query
61
+ @articles = @articles.where(title: @title) if @title.present?
62
+ end
63
+
64
+ def build_date_range_query
65
+ @articles = @articles.where('published_date >= ?', @start_date) if @start_date.present?
66
+ @articles = @articles.where('published_date <= ?', @end_date) if @end_date.present?
67
+ end
68
+ end
69
+
70
+ Then you can call the search like
71
+
72
+ Article.search(title: 'A Title', start_date: '2014-10-1', end_date: '2014-10-31')
73
+
74
+ which will instantiate the ArticleSearch class with above params and returns the search results after forumulating the sql query with conditions.
75
+
76
+ ## Contributing
77
+
78
+ 1. Fork it
79
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
80
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
81
+ 4. Push to the branch (`git push origin my-new-feature`)
82
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,21 @@
1
+ require "ruby_search_object/version"
2
+
3
+ module RubySearchObject
4
+ module ClassMethods
5
+ def search(params = {})
6
+ search_handler(params).search
7
+ end
8
+
9
+ def search_handler params = {}
10
+ search_handler = search_handler_class.new(params)
11
+ end
12
+
13
+ def search_handler_class
14
+ Object.const_get("#{name}Search")
15
+ end
16
+ end
17
+
18
+ def self.included(host_class)
19
+ host_class.extend ClassMethods
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module RubySearchObject
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ruby_search_object/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby_search_object"
8
+ spec.version = RubySearchObject::VERSION
9
+ spec.authors = ["Vijayanand Nandam"]
10
+ spec.email = ["vijayanand.nandam@gmail.com"]
11
+ spec.description = %q{A ruby service object pattern}
12
+ spec.summary = %q{Implements the ruby search service object pattern to encapsulate search functionality on records of an object}
13
+ spec.homepage = "http://cybrilla.com"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency 'rake', '~> 10.3', '>= 10.3.2'
23
+ end
@@ -0,0 +1,44 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+
4
+ $:.unshift File.dirname(__FILE__)
5
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
6
+
7
+ require 'ruby_search_object'
8
+
9
+ class Article
10
+ include RubySearchObject
11
+ end
12
+
13
+ class ArticleSearch
14
+ attr_accessor :params
15
+ def initialize params = {}
16
+ @params = params
17
+ end
18
+
19
+ def search
20
+ params
21
+ end
22
+ end
23
+
24
+ describe RubySearchObject do
25
+
26
+ describe 'when asked for search handler' do
27
+ it 'returns the correct class' do
28
+ Article.search_handler_class.must_equal ArticleSearch
29
+ end
30
+ end
31
+
32
+ describe 'when asked for search handler' do
33
+ it 'returns search handler instance of search handler' do
34
+ Article.search_handler.class.must_equal ArticleSearch
35
+ end
36
+ end
37
+
38
+ describe 'when asked to search' do
39
+ it 'triggers the search on the search handler instance' do
40
+ params = { param: 1 }
41
+ Article.search(params).must_equal params
42
+ end
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_search_object
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Vijayanand Nandam
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.3'
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: 10.3.2
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '10.3'
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: 10.3.2
47
+ description: A ruby service object pattern
48
+ email:
49
+ - vijayanand.nandam@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/ruby_search_object.rb
60
+ - lib/ruby_search_object/version.rb
61
+ - ruby_search_object.gemspec
62
+ - tests/ruby_search_object_test.rb
63
+ homepage: http://cybrilla.com
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.3.0
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Implements the ruby search service object pattern to encapsulate search functionality
87
+ on records of an object
88
+ test_files: []