fetch-process-multi 0.1.0

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: 710f64803764a73ec80ca343df9efc62d8c40101
4
+ data.tar.gz: cb3888bb00b60849c837f04eaf807d3fde734c66
5
+ SHA512:
6
+ metadata.gz: c36d0c7007426895e9197c6918920b1341ad1b9bbc5c8c0b15a54b3446358013108a69f53ac6a3324cac54ba65355546a6c67050947bcee24f796dbfdc7ca32d
7
+ data.tar.gz: f2ba66628d831ba07b7dda6175b976e7a917f3e2d175b3b5b3b88a02ca7e2834f4eb40025c64631e6e75c146253ebb279fa8500c47d510addbced54c29106827
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fetch-process-multi.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # Fetch and process multi
2
+
3
+ Fetches data from the cache, using the given `ids` and `ids_to_keys_proc`.
4
+
5
+ If there is data in the cache with the given keys, then that data is returned.
6
+
7
+ Otherwise, the supplied block is called for ids for which there was no
8
+ data, and the result will be written to the cache and returned
9
+ as ids -> values hash.
10
+
11
+ Ids can be any even composite identifiers.
12
+
13
+ Options are passed to the underlying cache implementation.
14
+
15
+ Returns hash of id -> value of passed ids.
16
+
17
+ ```ruby
18
+ def ids_to_keys(ids)
19
+ ids.each_with_object({}) do |id, memo|
20
+ memo[id] = "key_base/id_#{id}"
21
+ end
22
+ end
23
+
24
+ cache.write("key_for_1", "value_for_1")
25
+
26
+ cache.fetch_and_process_multi(ids_to_keys([1, 2]) do |uncached_ids|
27
+ uncached_ids.each_with_object({}) do |id, memo|
28
+ memo[id] = "fallback_value_for_#{id}"
29
+ end
30
+ end
31
+
32
+ # => { 1 => "value_for_1", 2 => "fallback_value_for_2" }
33
+ ```
34
+
35
+ ## Installation
36
+
37
+ Add this line to your application's `Gemfile`:
38
+
39
+ ```ruby
40
+ gem 'fetch-process-multi'
41
+ ```
42
+
43
+ And then execute:
44
+
45
+ ```bash
46
+ bundle
47
+ ```
48
+
49
+ Or install it yourself as:
50
+
51
+ ```bash
52
+ gem install fetch-process-multi
53
+ ```
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
58
+
59
+ To install this gem onto your local machine, run `bundle exec rake install`.
60
+
61
+ To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`,
62
+ which will create a git tag for the version, push git commits and tags,
63
+ and push the `.gem` file to [rubygems.org](https://rubygems.org).
64
+
65
+ ## Contributing
66
+
67
+ Bug reports and pull requests are welcome on GitHub at
68
+ [https://github.com/vinted/fetch-process-multi](https://github.com/vinted/fetch-process-multi).
69
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'fetch-process-multi'
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fetch_process_multi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'fetch-process-multi'
8
+ spec.version = FetchProcessMulti::VERSION
9
+ spec.authors = ['Karolis Astrauka']
10
+ spec.email = ['astrauka@gmail.com']
11
+
12
+ spec.summary = %q{Fetch and process multi allows you to calculate and cache multiple values}
13
+ spec.description = %q{Fetch and process multi allows you to calculate and cache multiple values}
14
+ spec.homepage = 'https://github.com/vinted/fetch-process-multi'
15
+ spec.licenses = ['MIT']
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'activesupport', '~> 4.2'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.10'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rails', '~> 4.2'
27
+ spec.add_development_dependency 'rspec', '~> 3.4'
28
+ end
@@ -0,0 +1,5 @@
1
+ require 'fetch_process_multi/version'
2
+ require 'fetch_process_multi/active_support'
3
+
4
+ module FetchProcessMulti
5
+ end
@@ -0,0 +1,55 @@
1
+ module ActiveSupport
2
+ module Cache
3
+ class Store
4
+ # Fetches data from the cache, using the given ids and ids_to_keys_proc.
5
+ # If there is data in the cache with the given keys,
6
+ # then that data is returned.
7
+ # Otherwise, the supplied block is called for ids for which there was no
8
+ # data, and the result will be written to the cache and returned
9
+ # as ids -> values hash.
10
+ # Ids can be any even composite identifiers.
11
+ #
12
+ # Options are passed to the underlying cache implementation.
13
+ #
14
+ # Returns hash of id -> value of passed ids. For example:
15
+ #
16
+ # def ids_to_keys(ids)
17
+ # ids.each_with_object({}) do |id, memo|
18
+ # memo[id] = "key_base/id_#{id}"
19
+ # end
20
+ # end
21
+ # cache.write("key_for_1", "value_for_1")
22
+ # cache.fetch_and_process_multi(ids_to_keys([1, 2]) do |uncached_ids|
23
+ # uncached_ids.each_with_object({}) do |id, memo|
24
+ # memo[id] = "fallback_value_for_#{id}"
25
+ # end
26
+ # end
27
+ # # => { 1 => "value_for_1", 2 => "fallback_value_for_2" }
28
+ #
29
+ def fetch_and_process_multi(ids_to_keys, options = {})
30
+ options = merged_options(options)
31
+ return {} if ids_to_keys.blank?
32
+
33
+ keys_to_ids = ids_to_keys.invert
34
+
35
+ cached_keys_to_values = read_multi(*keys_to_ids.keys, options)
36
+ cached_ids_to_values =
37
+ cached_keys_to_values.each_with_object({}) do |(key, value), memo|
38
+ memo[keys_to_ids[key]] = value
39
+ end
40
+
41
+ if cached_ids_to_values.size == ids_to_keys.size
42
+ return cached_ids_to_values
43
+ end
44
+
45
+ uncached_ids = ids_to_keys.keys - cached_ids_to_values.keys
46
+ uncached_ids_to_values = yield(uncached_ids)
47
+ uncached_ids_to_values.each do |id, value|
48
+ write(ids_to_keys[id], value, options)
49
+ end
50
+
51
+ cached_ids_to_values.merge!(uncached_ids_to_values)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module FetchProcessMulti
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fetch-process-multi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Karolis Astrauka
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.4'
83
+ description: Fetch and process multi allows you to calculate and cache multiple values
84
+ email:
85
+ - astrauka@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - README.md
93
+ - Rakefile
94
+ - bin/console
95
+ - bin/setup
96
+ - fetch-process-multi.gemspec
97
+ - lib/fetch-process-multi.rb
98
+ - lib/fetch_process_multi/active_support.rb
99
+ - lib/fetch_process_multi/version.rb
100
+ homepage: https://github.com/vinted/fetch-process-multi
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.4.5
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Fetch and process multi allows you to calculate and cache multiple values
124
+ test_files: []