serial_fetcher 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9473c55de75f60712d7fc3414f8ddfc1432c1d5a
4
+ data.tar.gz: 46459962b81d3f6fa43476bf2de61b12d192e1a3
5
+ SHA512:
6
+ metadata.gz: c4c6852aac20e727fc5aa4d639b3cd915b594049edc1b3c909c9941cfbf9dd97fd9796c164b2874d627b29601bf6348b225a05bbf40af54b009acb4424d3333a
7
+ data.tar.gz: 56f8483cdb10b09d7498a40d998120adfaef7089b74df437518aa91ef786e72e24a16106f8d9dac9f7a1cafd5004f3026cd89c170c55170f847ead5a9ca34a67
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ serial_fetcher
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.3.0
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in serial_fetcher.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Olivier Berho
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,128 @@
1
+ # SerialFetcher
2
+
3
+ Serial Fetcher provides a way to automatically fetch a list of resources from
4
+ the params hash.
5
+
6
+ The params must be passed to the `fetch` method with a `schema` describing the
7
+ associations between params and models.
8
+
9
+ A fetcher must be provided to the SerialFetcher through configuration.
10
+ By default there is an ActiveRecord fetcher that call `find` to the constantized
11
+ class name.
12
+
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'serial_fetcher'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install serial_fetcher
29
+
30
+ ## Usage
31
+
32
+ Suppose you have an url like :
33
+
34
+ http://localhost:3000/users/1/blogs/5/posts/35/comments/3
35
+
36
+ The corresponding params are :
37
+
38
+ ```ruby
39
+ params: {
40
+ user_id: "1",
41
+ blog_id: "5",
42
+ post_id: "35",
43
+ id: "3"
44
+ }
45
+ ```
46
+
47
+ Now in your comments_controller.rb, you probably have something like :
48
+
49
+ ```ruby
50
+ @user = User.find params[:user_id]
51
+ @blog = Blog.find params[:blog_id]
52
+ @post = Post.find params[:post_id]
53
+ @comment = User.find params[:id]
54
+ ```
55
+
56
+ With SerialFetcher, you could just write :
57
+
58
+ ```ruby
59
+ @store = SerialFetcher.fetch(params, id: :comment)
60
+ ```
61
+
62
+ And then @store will be created with :
63
+
64
+ ```ruby
65
+ @store = {
66
+ user: User.find params[:user_id],
67
+ blog: Blog.find params[:blog_id],
68
+ post: Post.find params[:post_id],
69
+ comment: Comment.find params[:id]
70
+ }
71
+ ```
72
+ You must provide the resource associated to the :id param.
73
+ The script will parse all the params containing '_id', and try to fetch the
74
+ corresponding resources.
75
+
76
+ Particular case if your param is not named like the model :
77
+
78
+ ```ruby
79
+ schema: {
80
+ id: :comment,
81
+ post_id: :article,
82
+ }
83
+
84
+ @store = SerialFetcher.fetch(params, schema)
85
+ ```
86
+
87
+ And then @store will be created with :
88
+
89
+ ```ruby
90
+ @store = {
91
+ article: Article.find params[:post_id],
92
+ comment: Comment.find params[:id]
93
+ }
94
+ ```
95
+ You can provide your own fetcher in an initializer :
96
+
97
+ ```ruby
98
+ SerialFetcher.configure |config|
99
+
100
+ # If we define repositories for our models, with a
101
+ # fetch_for_id method
102
+ config.fetcher = ->(param_name, param) {
103
+ begin
104
+ klass_name = "#{param_name}_repository".camelize
105
+ klass = klass_name.constantize
106
+ klass.send(:fetch_for_id, param)
107
+ rescue NameError
108
+ # The repository does not exist for the given param
109
+ nil
110
+ end
111
+ }
112
+ ```
113
+
114
+ ## Development
115
+
116
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
117
+
118
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
119
+
120
+ ## Contributing
121
+
122
+ Bug reports and pull requests are welcome on GitHub at https://github.com/o-bo/serial_fetcher.
123
+
124
+
125
+ ## License
126
+
127
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
128
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "serial_fetcher"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+ require "serial_fetcher/version"
2
+ require "serial_fetcher/configuration"
3
+
4
+ module SerialFetcher
5
+
6
+ class << self
7
+ attr_accessor :configuration
8
+ end
9
+
10
+
11
+ def self.configure
12
+ self.configuration ||= Configuration.new
13
+ yield(configuration) if block_given?
14
+ end
15
+
16
+
17
+ def self.fetch(params, schema={})
18
+ config = self.configuration ||= Configuration.new
19
+ fetcher = config.fetcher
20
+
21
+ raise "You must provide a fetcher method." if fetcher.nil?
22
+ raise "You must provide a hash for params." if params.nil?
23
+ raise "You must provide the key id for the basic resource to be fetched." if schema[:id].nil?
24
+
25
+ params.keys.each_with_object({}) do |k, res|
26
+ key_id = schema[k]
27
+ if key_id
28
+ param_name = key_id.to_s
29
+ res[key_id] = fetcher.(param_name, params[k])
30
+ else
31
+ if /.*_id/.match(k.to_s)
32
+ param_name = k.to_s.split('_id').first
33
+ res[param_name.to_sym] = fetcher.(param_name, params[k])
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,13 @@
1
+ require "serial_fetcher/fetchers/active_record_fetcher"
2
+
3
+ module SerialFetcher
4
+ class Configuration
5
+
6
+ attr_accessor :fetcher
7
+
8
+
9
+ def initialize
10
+ @fetcher = SerialFetcher::Fetchers::ActiveRecordFetcher.get
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ module SerialFetcher
2
+ module Fetchers
3
+ class ActiveRecordFetcher
4
+
5
+ def self.get
6
+ ->(param_name, param) {
7
+ begin
8
+ klass_name = param_name.camelize
9
+ klass = klass_name.constantize
10
+ klass.send(:find, param)
11
+ rescue NameError
12
+ nil
13
+ end
14
+ }
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module SerialFetcher
2
+ VERSION = "1.0.3"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'serial_fetcher/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "serial_fetcher"
8
+ spec.version = SerialFetcher::VERSION
9
+ spec.authors = ["o-bo"]
10
+
11
+ spec.summary = %q{A script to ease fetching data from hash params in ruby applications.}
12
+ spec.description = %q{Serial Fetcher provides a way to automatically fetch a list of resources from the params hash. The params must be passed to the `fetch` method with a `schema` describing the associations between params and models. A fetcher must be provided to the SerialFetcher through configuration. By default there is an ActiveRecord fetcher that call `find` to the constantized class name.}
13
+ spec.homepage = "https://github.com/o-bo/serial_fetcher"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "~> 5.0"
24
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: serial_fetcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3
5
+ platform: ruby
6
+ authors:
7
+ - o-bo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-16 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: Serial Fetcher provides a way to automatically fetch a list of resources
56
+ from the params hash. The params must be passed to the `fetch` method with a `schema`
57
+ describing the associations between params and models. A fetcher must be provided
58
+ to the SerialFetcher through configuration. By default there is an ActiveRecord
59
+ fetcher that call `find` to the constantized class name.
60
+ email:
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - ".ruby-gemset"
67
+ - ".ruby-version"
68
+ - ".travis.yml"
69
+ - Gemfile
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - bin/console
74
+ - bin/setup
75
+ - lib/serial_fetcher.rb
76
+ - lib/serial_fetcher/configuration.rb
77
+ - lib/serial_fetcher/fetchers/active_record_fetcher.rb
78
+ - lib/serial_fetcher/version.rb
79
+ - serial_fetcher.gemspec
80
+ homepage: https://github.com/o-bo/serial_fetcher
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.5.1
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: A script to ease fetching data from hash params in ruby applications.
104
+ test_files: []