abstract_builder 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +41 -0
- data/LICENSE.txt +21 -0
- data/README.md +143 -0
- data/Rakefile +6 -0
- data/abstract_builder.gemspec +21 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/abstract_builder.rb +150 -0
- data/lib/abstract_builder/lazy_cache.rb +42 -0
- data/lib/abstract_builder/null_cache.rb +19 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d1510bbd2e6f2501d0dd235dce25b353075165fab4d27c810297c4cf6218de0e
|
4
|
+
data.tar.gz: 0f644442394e9b52fb6de6930c6d9a4de0dd24adee25eec8f449bdc64546b7ba
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2816f65be6345a916578a231c574ef611b5f2adf9f80e0815660e3a7f74bdd74bc1b1902480c562083e44e4b1ce683aa976858882ef0c2f13146b9ec7c377a0c
|
7
|
+
data.tar.gz: 9bd710bd5eea983fa57a4d279fa427e26e14b76ae2c99cb8ebc3dc6796842262f89c2f1e232689f79cb88c417dcbda0caa748e42105ca9d9e42d31d97c376cae
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at gabriel.sobrinho@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
abstract_builder (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
coderay (1.1.2)
|
10
|
+
diff-lcs (1.3)
|
11
|
+
method_source (0.9.0)
|
12
|
+
pry (0.11.3)
|
13
|
+
coderay (~> 1.1.0)
|
14
|
+
method_source (~> 0.9.0)
|
15
|
+
rake (10.5.0)
|
16
|
+
rspec (3.7.0)
|
17
|
+
rspec-core (~> 3.7.0)
|
18
|
+
rspec-expectations (~> 3.7.0)
|
19
|
+
rspec-mocks (~> 3.7.0)
|
20
|
+
rspec-core (3.7.0)
|
21
|
+
rspec-support (~> 3.7.0)
|
22
|
+
rspec-expectations (3.7.0)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.7.0)
|
25
|
+
rspec-mocks (3.7.0)
|
26
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
27
|
+
rspec-support (~> 3.7.0)
|
28
|
+
rspec-support (3.7.0)
|
29
|
+
|
30
|
+
PLATFORMS
|
31
|
+
ruby
|
32
|
+
|
33
|
+
DEPENDENCIES
|
34
|
+
abstract_builder!
|
35
|
+
bundler (~> 1.16)
|
36
|
+
pry
|
37
|
+
rake (~> 10.0)
|
38
|
+
rspec (~> 3.0)
|
39
|
+
|
40
|
+
BUNDLED WITH
|
41
|
+
1.16.0
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Gabriel Sobrinho
|
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,143 @@
|
|
1
|
+
# AbstractBuilder
|
2
|
+
|
3
|
+
AbstractBuilder gives you a simple DSL for declaring structures that beats manipulating giant hash structures. This is particularly helpful when the generation process is fraught with conditionals and loops.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'abstract_builder'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install abstract_builder
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
AbstractBuilder gives you a simple DSL for declaring structures:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
builder = AbstractBuilder.new
|
27
|
+
builder.(company, :name, :created_at, :updated_at)
|
28
|
+
builder.size company.employees.count
|
29
|
+
builder.merge! company.metadata
|
30
|
+
|
31
|
+
builder.address do |address_builder|
|
32
|
+
address_builder.street company.address.street
|
33
|
+
address_builder.number company.address.number
|
34
|
+
end
|
35
|
+
|
36
|
+
builder.phones company.phones do |phone_builder, phone|
|
37
|
+
phone_builder.number phone.number
|
38
|
+
phone_builder.contact phone.contact
|
39
|
+
end
|
40
|
+
|
41
|
+
builder.data!
|
42
|
+
{
|
43
|
+
:name => "...",
|
44
|
+
:created_at => "...",
|
45
|
+
:updated_at => "...",
|
46
|
+
:size => "...",
|
47
|
+
:metadata => { ... },
|
48
|
+
:address => {
|
49
|
+
:street => "...",
|
50
|
+
:number => "..."
|
51
|
+
},
|
52
|
+
:phones => [
|
53
|
+
{
|
54
|
+
:number => "...",
|
55
|
+
:contact => "..."
|
56
|
+
}
|
57
|
+
]
|
58
|
+
}
|
59
|
+
```
|
60
|
+
|
61
|
+
Alternatively, you can use the low-level API for declaring complex structures:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
builder = AbstractBuilder.new
|
65
|
+
|
66
|
+
company.block! company.uuid do |block_builder|
|
67
|
+
block_builder.name company.name
|
68
|
+
|
69
|
+
company.phones.each do |phone|
|
70
|
+
builder.set! "phone_number_#{phone.index}", phone.number
|
71
|
+
end
|
72
|
+
|
73
|
+
company.messages.each_slice(2).each_with_index do |messages, index|
|
74
|
+
builder.array! "messages_#{index}", messages do |message_builder, message|
|
75
|
+
message_builder.content message.content
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
builder.data!
|
81
|
+
{
|
82
|
+
"461d84cf-039e-4b86-9e92-8f04cce19a1d" => {
|
83
|
+
:name => "...",
|
84
|
+
:phone_number_0 => "...",
|
85
|
+
:phone_number_1 => "..."
|
86
|
+
:messages_0 => [
|
87
|
+
{
|
88
|
+
:content => "..."
|
89
|
+
},
|
90
|
+
{
|
91
|
+
:content => "..."
|
92
|
+
}
|
93
|
+
],
|
94
|
+
:messages_1 => [
|
95
|
+
{
|
96
|
+
:content => "..."
|
97
|
+
}
|
98
|
+
]
|
99
|
+
},
|
100
|
+
}
|
101
|
+
```
|
102
|
+
|
103
|
+
Since the AbstractBuilder is abstract to the data protocol, you may serialize the result into any protocol you want, like JSON:
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
JSON.dump(builder.data!)
|
107
|
+
```
|
108
|
+
|
109
|
+
Be aware that if you are doing this over and over again, you can inherit from AbstractBuilder to DRY:
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
class MyBuilder < AbstractBuilder
|
113
|
+
def to_json
|
114
|
+
JSON.dump(data!)
|
115
|
+
end
|
116
|
+
|
117
|
+
def to_yaml
|
118
|
+
YAML.dump(data!)
|
119
|
+
end
|
120
|
+
|
121
|
+
def to_msgpack
|
122
|
+
MessagePack.pack(data!)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
```
|
126
|
+
|
127
|
+
## Development
|
128
|
+
|
129
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
130
|
+
|
131
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `abstract_builder.gemspec`, 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).
|
132
|
+
|
133
|
+
## Contributing
|
134
|
+
|
135
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sobrinho/abstract_builder. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
136
|
+
|
137
|
+
## License
|
138
|
+
|
139
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
140
|
+
|
141
|
+
## Code of Conduct
|
142
|
+
|
143
|
+
Everyone interacting in the AbstractBuilder project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/sobrinho/abstract_builder/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "abstract_builder"
|
3
|
+
spec.version = "0.1.0"
|
4
|
+
spec.authors = ["Gabriel Sobrinho"]
|
5
|
+
spec.email = ["gabriel.sobrinho@gmail.com"]
|
6
|
+
|
7
|
+
spec.summary = %q{AbstractBuilder gives you a simple DSL for declaring structures that beats manipulating giant hash structures.}
|
8
|
+
spec.homepage = "https://github.com/sobrinho/abstract_builder"
|
9
|
+
spec.license = "MIT"
|
10
|
+
|
11
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
12
|
+
f.match(%r{^(test|spec|features)/})
|
13
|
+
end
|
14
|
+
spec.bindir = "exe"
|
15
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
16
|
+
spec.require_paths = ["lib"]
|
17
|
+
|
18
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
19
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
20
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
21
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "abstract_builder"
|
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(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
require 'abstract_builder/null_cache'
|
2
|
+
require 'abstract_builder/lazy_cache'
|
3
|
+
|
4
|
+
class AbstractBuilder
|
5
|
+
@@format_key = nil
|
6
|
+
@@ignore_value = nil
|
7
|
+
@@cache_store = NullCache.new
|
8
|
+
|
9
|
+
def self.cache_store!(cache_store)
|
10
|
+
@@cache_store = cache_store
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.format_key!(&block)
|
14
|
+
@@format_key = block
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.ignore_value!(&block)
|
18
|
+
@@ignore_value = block
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize
|
22
|
+
@format_key = @@format_key
|
23
|
+
@ignore_value = @@ignore_value
|
24
|
+
@cache_store = @@cache_store
|
25
|
+
@lazy_cache = LazyCache.new(@@cache_store)
|
26
|
+
@stack = []
|
27
|
+
end
|
28
|
+
|
29
|
+
def format_key!(&block)
|
30
|
+
@format_key = block
|
31
|
+
end
|
32
|
+
|
33
|
+
def ignore_value!(&block)
|
34
|
+
@ignore_value = block
|
35
|
+
end
|
36
|
+
|
37
|
+
def cache_store!(cache_store)
|
38
|
+
@cache_store = cache_store
|
39
|
+
@lazy_cache = LazyCache.new(cache_store)
|
40
|
+
end
|
41
|
+
|
42
|
+
def set!(key, value)
|
43
|
+
@stack << [key, value]
|
44
|
+
end
|
45
|
+
|
46
|
+
def merge!(value)
|
47
|
+
value.each_pair do |key, value|
|
48
|
+
set! key, value
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def call(object, *keys)
|
53
|
+
keys.each do |key|
|
54
|
+
set! key, object.public_send(key)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def block!(key, &block)
|
59
|
+
builder = _inherit
|
60
|
+
block.call(builder)
|
61
|
+
value = builder.data!
|
62
|
+
|
63
|
+
set! key, value unless value.empty?
|
64
|
+
end
|
65
|
+
|
66
|
+
def array!(key, collection, &block)
|
67
|
+
values = collection.map do |item|
|
68
|
+
builder = _inherit
|
69
|
+
block.call(builder, item)
|
70
|
+
builder.data!
|
71
|
+
end
|
72
|
+
|
73
|
+
set! key, values
|
74
|
+
end
|
75
|
+
|
76
|
+
def cache!(cache_key, options = {}, &block)
|
77
|
+
cache_key = _compute_cache_key(cache_key)
|
78
|
+
|
79
|
+
@lazy_cache.add(cache_key, options) do
|
80
|
+
builder = _inherit
|
81
|
+
block.call(builder)
|
82
|
+
builder.data!
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def data!
|
87
|
+
data = {}
|
88
|
+
|
89
|
+
@stack.each do |(key, value)|
|
90
|
+
key = _format_key(key)
|
91
|
+
data[key] = value unless _ignore_value?(value)
|
92
|
+
end
|
93
|
+
|
94
|
+
@lazy_cache.resolve.each do |value|
|
95
|
+
data.merge!(value)
|
96
|
+
end
|
97
|
+
|
98
|
+
data
|
99
|
+
end
|
100
|
+
|
101
|
+
def method_missing(*args, &block)
|
102
|
+
if args.length == 2 && block
|
103
|
+
array!(args[0], args[1], &block)
|
104
|
+
elsif args.length == 1 && block
|
105
|
+
block!(args[0], &block)
|
106
|
+
elsif args.length == 2
|
107
|
+
set!(args[0], args[1])
|
108
|
+
else
|
109
|
+
raise ArgumentError, <<~EOF.chomp
|
110
|
+
Expected 1 argument without a block, 0 arguments with a block or 1 argument with a block.
|
111
|
+
|
112
|
+
This is `AbstractBuilder#set!', `AbstractBuilder#block!' or `AbstractBuilder#array!' signatures, example:
|
113
|
+
|
114
|
+
builder.content post.content
|
115
|
+
|
116
|
+
builder.meta do |meta_builder|
|
117
|
+
meta_builder.hashtags post.hashtags
|
118
|
+
end
|
119
|
+
|
120
|
+
builder.comments post.comments do |comment_builder, comment|
|
121
|
+
comment_builder.content comment.content
|
122
|
+
end
|
123
|
+
|
124
|
+
Received `#{args[0]}' with #{args.length - 1} argument#{'s' if args.length > 2} #{block ? "with a block" : "without a block"}.
|
125
|
+
EOF
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
private
|
130
|
+
|
131
|
+
def _format_key(key)
|
132
|
+
@format_key ? @format_key.call(key) : key
|
133
|
+
end
|
134
|
+
|
135
|
+
def _inherit
|
136
|
+
builder = self.class.new
|
137
|
+
builder.format_key!(&@format_key)
|
138
|
+
builder.ignore_value!(&@ignore_value)
|
139
|
+
builder.cache_store!(@cache_store)
|
140
|
+
builder
|
141
|
+
end
|
142
|
+
|
143
|
+
def _compute_cache_key(cache_key)
|
144
|
+
[:abstract_builder, :v1, cache_key]
|
145
|
+
end
|
146
|
+
|
147
|
+
def _ignore_value?(value)
|
148
|
+
@ignore_value && @ignore_value.call(value)
|
149
|
+
end
|
150
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class AbstractBuilder
|
2
|
+
class LazyCache
|
3
|
+
def initialize(driver)
|
4
|
+
@cache = Hash.new { |h, k| h[k] = {} }
|
5
|
+
@driver = driver
|
6
|
+
end
|
7
|
+
|
8
|
+
def add(key, options, &block)
|
9
|
+
cache[options][key] = block
|
10
|
+
end
|
11
|
+
|
12
|
+
def resolve
|
13
|
+
resolved = []
|
14
|
+
|
15
|
+
# Fail-fast if there is no items to be computed.
|
16
|
+
return resolved if cache.empty?
|
17
|
+
|
18
|
+
# We can't add new items during interation, so iterate through a clone
|
19
|
+
# that will allow us to add new items.
|
20
|
+
previous = cache.clone
|
21
|
+
cache.clear
|
22
|
+
|
23
|
+
# Keys are grouped by options and because of that, fetch_multi will use
|
24
|
+
# the same options for the same group of keys.
|
25
|
+
previous.each do |options, group|
|
26
|
+
result = driver.fetch_multi(*group.keys, options) do |group_key|
|
27
|
+
[group[group_key].call, *resolve]
|
28
|
+
end
|
29
|
+
|
30
|
+
# Since the fetch_multi returns { cache_key => value }, we need to
|
31
|
+
# discard the cache key and merge only the values.
|
32
|
+
resolved.concat result.values.flatten(1)
|
33
|
+
end
|
34
|
+
|
35
|
+
resolved
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
attr_reader :cache, :driver
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class AbstractBuilder
|
2
|
+
class NullCache
|
3
|
+
def fetch(key, _options = nil, &block)
|
4
|
+
block.call
|
5
|
+
end
|
6
|
+
|
7
|
+
def fetch_multi(*keys, options, &block)
|
8
|
+
result = {}
|
9
|
+
|
10
|
+
keys.each do |key|
|
11
|
+
result[key] = fetch(key, options) do
|
12
|
+
block.call(key)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
result
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: abstract_builder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gabriel Sobrinho
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-02-01 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
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: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- gabriel.sobrinho@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- Gemfile.lock
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- abstract_builder.gemspec
|
72
|
+
- bin/console
|
73
|
+
- bin/setup
|
74
|
+
- lib/abstract_builder.rb
|
75
|
+
- lib/abstract_builder/lazy_cache.rb
|
76
|
+
- lib/abstract_builder/null_cache.rb
|
77
|
+
homepage: https://github.com/sobrinho/abstract_builder
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.7.2
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: AbstractBuilder gives you a simple DSL for declaring structures that beats
|
101
|
+
manipulating giant hash structures.
|
102
|
+
test_files: []
|