encapsulate 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 +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +222 -0
- data/Rakefile +11 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/encapsulate.gemspec +28 -0
- data/lib/encapsulate.rb +23 -0
- data/lib/encapsulate/version.rb +3 -0
- metadata +125 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6e780a5ee6b125851b50a31ac06d586b7d53071d
|
4
|
+
data.tar.gz: c68e720426f79ad6ea3158b6c5f48d5dee7450a6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4c028bd04704ce813ccd553246292ad11d82abc14aedd0ec49a2f3ca56061f55b7a9b9408591a92387e99a2947081afb870646dd3bf819f3a8108feb17f21d84
|
7
|
+
data.tar.gz: 5f2f28a451cc43a617ffc5a7c2f5810b7d5f9cecd1513cca60c51b575d5f07ff477a94b21444b45453fc279feae7a4c6c80350ce4301dd593742b7e933b9b732
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,222 @@
|
|
1
|
+
# Encapsulate
|
2
|
+
|
3
|
+
#### A small Ruby framework that enables encapsulating pieces of code within eachother.
|
4
|
+
|
5
|
+
| Branch | Status |
|
6
|
+
| ------ | ------ |
|
7
|
+
| Release | [![Build Status](https://travis-ci.org/thisismydesign/encapsulate.svg?branch=release)](https://travis-ci.org/thisismydesign/encapsulate) [![Coverage Status](https://coveralls.io/repos/github/thisismydesign/encapsulate/badge.svg?branch=release)](https://coveralls.io/github/thisismydesign/encapsulate?branch=release) [![Gem Version](https://badge.fury.io/rb/encapsulate.svg)](https://badge.fury.io/rb/encapsulate) [![Total Downloads](http://ruby-gem-downloads-badge.herokuapp.com/encapsulate?type=total)](https://rubygems.org/gems/encapsulate) |
|
8
|
+
| Development | [![Build Status](https://travis-ci.org/thisismydesign/encapsulate.svg?branch=master)](https://travis-ci.org/thisismydesign/encapsulate) [![Coverage Status](https://coveralls.io/repos/github/thisismydesign/encapsulate/badge.svg?branch=master)](https://coveralls.io/github/thisismydesign/encapsulate?branch=master) |
|
9
|
+
|
10
|
+
## What is this?
|
11
|
+
|
12
|
+
Ever get tired of writing exception handling blocks? Ever think it's not really DRY (Don't Repeat Yourself)?
|
13
|
+
|
14
|
+
Wouldn't it be better instead of...
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
begin
|
18
|
+
read_file
|
19
|
+
rescue IOError => e
|
20
|
+
handle_io_error
|
21
|
+
ensure
|
22
|
+
close_io
|
23
|
+
end
|
24
|
+
```
|
25
|
+
|
26
|
+
... to just write
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
run callback: read_file, with: io_error_handling
|
30
|
+
```
|
31
|
+
|
32
|
+
Not to mention code duplications you just can't get rid of
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
signal_processing_start
|
36
|
+
start_time_measurement
|
37
|
+
|
38
|
+
process
|
39
|
+
|
40
|
+
stop_time_measurement
|
41
|
+
signal_processing_end
|
42
|
+
```
|
43
|
+
|
44
|
+
How about
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
run callback: process, with: [time_measurement, lifecycle_signals]
|
48
|
+
```
|
49
|
+
|
50
|
+
Encapsulate enables you to do just that.
|
51
|
+
|
52
|
+
## How does it work?
|
53
|
+
|
54
|
+
We essentially want to create the following structure:
|
55
|
+
```ruby
|
56
|
+
encapsulator1
|
57
|
+
# ...
|
58
|
+
encapsulator2
|
59
|
+
# ...
|
60
|
+
base_function
|
61
|
+
# ...
|
62
|
+
end
|
63
|
+
# ...
|
64
|
+
end
|
65
|
+
# ...
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
In order to achieve this each encapsulator function must know the next one in the chain (the last one being the base function). Also the parameters of the base function must be carried by the encapsulators. Our pseudocode should look something like this:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
def encapsulator1(callback:, params: nil)
|
73
|
+
# some logic...
|
74
|
+
def encapsulator2(callback:, params: nil)
|
75
|
+
# some logic...
|
76
|
+
base_function(params)
|
77
|
+
# some logic...
|
78
|
+
end
|
79
|
+
# some logic...
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
## How to use it?
|
84
|
+
|
85
|
+
Below you will find some insight on how to create and structure your call chains but you can also head right to the [unit tests](https://github.com/thisismydesign/encapsulate/blob/master/spec/encapsulate_spec.rb) for hands-on examples.
|
86
|
+
|
87
|
+
### Building the chain
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
# Single encapsulator
|
91
|
+
Encapsulate.run callback: base_function, with: [encapsulator]
|
92
|
+
|
93
|
+
# Multiple encapsulators
|
94
|
+
# They will apply in the given order: encapsulator2(encapsulator1(callback))
|
95
|
+
Encapsulate.run callback: base_function, with: [encapsulator1, encapsulator2]
|
96
|
+
|
97
|
+
# Parameters
|
98
|
+
Encapsulate.run callback: base_function, with: [encapsulator1, encapsulator2], params: {arg: 'something'}
|
99
|
+
```
|
100
|
+
|
101
|
+
### Base function
|
102
|
+
|
103
|
+
To sidestep the issue of varying number of parameters we must use [keyword arguments or a single Hash parameter](https://robots.thoughtbot.com/ruby-2-keyword-arguments) in the base function.
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
def self.base_function(my_param:, my_other_param:)
|
107
|
+
# ...
|
108
|
+
end
|
109
|
+
|
110
|
+
base_function = self.method(:base_function)
|
111
|
+
```
|
112
|
+
|
113
|
+
### Encapsulators
|
114
|
+
|
115
|
+
Encapsulators should be implemented (for reasons detailed in the previous chapter) along the lines of:
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
def self.my_encapsulator(callback:, params: nil)
|
119
|
+
# ...
|
120
|
+
params.nil? ? callback.call : callback.call(params)
|
121
|
+
# ...
|
122
|
+
end
|
123
|
+
|
124
|
+
my_encapsulator = self.method(:my_encapsulator)
|
125
|
+
```
|
126
|
+
|
127
|
+
The base function may take no parameters which is why `params` in our interface must default to nil and we also need to take care of calling the `callback` accordingly. This small piece of logic is implemented in the gem [reflection_utils](https://github.com/thisismydesign/reflection_utils) (as seen below) alongside with other useful reflection related functions.
|
128
|
+
|
129
|
+
You may use any objects that respond to `call`. The only difference will be in how you reference these objects.
|
130
|
+
These are also valid skeletons:
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
my_encapsulator = lambda do |callback:, params: nil|
|
134
|
+
# ...
|
135
|
+
ReflectionUtils.call(callback, params)
|
136
|
+
# ...
|
137
|
+
end
|
138
|
+
```
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
my_encapsulator = Proc.new do |callback:, params: nil|
|
142
|
+
# ...
|
143
|
+
ReflectionUtils.call(callback, params)
|
144
|
+
# ...
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
148
|
+
Encapsulators will ideally not use any parameters. They do take the base function's parameter hash as second parameter and you could *technically* hide additional parameters there but it's not a good practice. Instead:
|
149
|
+
- try to keep encapsulators simple
|
150
|
+
- use their own classes to configure them
|
151
|
+
|
152
|
+
This brings us to how to structure encapsulators. You have several options.
|
153
|
+
|
154
|
+
#### 1 class per encapsulator
|
155
|
+
|
156
|
+
```ruby
|
157
|
+
class ExceptionEncapsulator
|
158
|
+
def self.callback(callback:, params: nil)
|
159
|
+
# ...
|
160
|
+
end
|
161
|
+
end
|
162
|
+
```
|
163
|
+
|
164
|
+
#### Collcetor class
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
class Encapsulators
|
168
|
+
def self.exception_handling(callback:, params: nil)
|
169
|
+
# ...
|
170
|
+
end
|
171
|
+
|
172
|
+
def self.time_measurement(callback:, params: nil)
|
173
|
+
# ...
|
174
|
+
end
|
175
|
+
|
176
|
+
# ...
|
177
|
+
end
|
178
|
+
```
|
179
|
+
|
180
|
+
#### In place
|
181
|
+
|
182
|
+
```ruby
|
183
|
+
my_encapsulator = lambda do |callback:, params: nil|
|
184
|
+
# ...
|
185
|
+
end
|
186
|
+
```
|
187
|
+
|
188
|
+
## Installation
|
189
|
+
|
190
|
+
Add this line to your application's Gemfile:
|
191
|
+
|
192
|
+
```ruby
|
193
|
+
gem 'encapsulate'
|
194
|
+
```
|
195
|
+
|
196
|
+
And then execute:
|
197
|
+
|
198
|
+
$ bundle
|
199
|
+
|
200
|
+
Or install it yourself as:
|
201
|
+
|
202
|
+
$ gem install encapsulate
|
203
|
+
|
204
|
+
## Feedback
|
205
|
+
|
206
|
+
Any feedback is much appreciated.
|
207
|
+
|
208
|
+
I can only tailor this project to fit use-cases I know about - which are usually my own ones. If you find that this might be the right direction to solve your problem too but you find that it's suboptimal or lacks features don't hesitate to contact me.
|
209
|
+
|
210
|
+
Please let me know if you make use of this project so that I can prioritize further efforts.
|
211
|
+
|
212
|
+
## Development
|
213
|
+
|
214
|
+
This gem is developed using Bundler conventions. A good overview can be found [here](http://bundler.io/v1.14/guides/creating_gem.html).
|
215
|
+
|
216
|
+
## Contributing
|
217
|
+
|
218
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/thisismydesign/encapsulate.
|
219
|
+
|
220
|
+
## License
|
221
|
+
|
222
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
|
6
|
+
desc "Check if source can be required locally"
|
7
|
+
task :require do
|
8
|
+
sh "ruby -e \"require '#{File.dirname __FILE__}/lib/encapsulate'\""
|
9
|
+
end
|
10
|
+
|
11
|
+
task :default => [:require, :spec]
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "encapsulate"
|
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
data/encapsulate.gemspec
ADDED
@@ -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 'encapsulate/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "encapsulate"
|
8
|
+
spec.version = Encapsulate::VERSION
|
9
|
+
spec.authors = ["thisismydesign"]
|
10
|
+
spec.email = ["thisismydesign@users.noreply.github.com"]
|
11
|
+
|
12
|
+
spec.summary = "A small framework that enables encapsulating pieces of code within eachother."
|
13
|
+
spec.homepage = "https://github.com/thisismydesign/encapsulate"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
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 "reflection_utils"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
spec.add_development_dependency "coveralls"
|
28
|
+
end
|
data/lib/encapsulate.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'reflection_utils'
|
2
|
+
|
3
|
+
module Encapsulate
|
4
|
+
|
5
|
+
@@encapsulator_params = [[:keyreq, :callback], [:key, :params]]
|
6
|
+
|
7
|
+
def self.run(callback:, with:, params:nil)
|
8
|
+
ReflectionUtils.call(create(callback: callback, with: with), params)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.create(callback:, with:)
|
12
|
+
lambdas = []
|
13
|
+
lambdas[0] = callback
|
14
|
+
|
15
|
+
with.each_with_index do |encapsulator, index|
|
16
|
+
ReflectionUtils.assert_parameters(encapsulator, @@encapsulator_params)
|
17
|
+
lambdas[index + 1] = lambda { |params = nil| encapsulator.call(callback: lambdas[index], params: params) }
|
18
|
+
end
|
19
|
+
|
20
|
+
lambdas.last
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: encapsulate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- thisismydesign
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: reflection_utils
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
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.14'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.14'
|
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: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: coveralls
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- thisismydesign@users.noreply.github.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- encapsulate.gemspec
|
100
|
+
- lib/encapsulate.rb
|
101
|
+
- lib/encapsulate/version.rb
|
102
|
+
homepage: https://github.com/thisismydesign/encapsulate
|
103
|
+
licenses: []
|
104
|
+
metadata: {}
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 2.5.2
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: A small framework that enables encapsulating pieces of code within eachother.
|
125
|
+
test_files: []
|