callchain 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +81 -0
- data/Rakefile +1 -0
- data/callchain.gemspec +23 -0
- data/lib/callchain.rb +38 -0
- data/lib/callchain/version.rb +3 -0
- data/test/callchain_test.rb +97 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6f961badd05dcae2858d74eaf1568858199639b2
|
4
|
+
data.tar.gz: e6aa34f668bf30cb4ac5e0556daa503d2c82f408
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a7252499d454464a7c98ff023bc396d3f51852a5d93678ac6db575a82d3b245e2a4de0ce65e41c8e5c5cb6c14c0f729a61dbef351d8b4e20cd0160c96a367204
|
7
|
+
data.tar.gz: 42320b9bc8c268ca4fa3ec32bea52b77f95a6d823a48fb6e09fc9f8c76b35af11de2fcacd766a31db689010b86ac0b5331a40f9457f5c527fe375a416a2cca88
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 csquared
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# Callchain
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'callchain'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install callchain
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Individual objects, lambdas, or modules do work with `::call(object)`
|
22
|
+
|
23
|
+
Compose a CallChain with the `::use` method
|
24
|
+
|
25
|
+
Compose a CallChain of call chains because it exports `::call(object)`
|
26
|
+
|
27
|
+
### Example: Class/Module names define `::call`
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class PlanScoper
|
31
|
+
def self.call(thing)
|
32
|
+
scope_the_thing(thing)
|
33
|
+
thing
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module Quantizer
|
38
|
+
def self.call(thing)
|
39
|
+
quantize_the_thing(thing)
|
40
|
+
thing
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class Pricer
|
45
|
+
extend CallChain
|
46
|
+
use PlanScoper, Quantizer, AppGrouper
|
47
|
+
use UserGrouper
|
48
|
+
end
|
49
|
+
|
50
|
+
statement = Statement.new
|
51
|
+
Pricer.call(statement)
|
52
|
+
```
|
53
|
+
|
54
|
+
### Example: lambda-friendly
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
class Debugger
|
58
|
+
extend CallChain
|
59
|
+
use lambda { |thing| p thing; thing }
|
60
|
+
end
|
61
|
+
|
62
|
+
Debugger.call(Object.new)
|
63
|
+
```
|
64
|
+
|
65
|
+
### Example: composable
|
66
|
+
```ruby
|
67
|
+
class CompositeCallChain
|
68
|
+
extend CallChain
|
69
|
+
use Debugger
|
70
|
+
end
|
71
|
+
|
72
|
+
CompositeCallChain.call(Object.new)
|
73
|
+
```
|
74
|
+
|
75
|
+
## Contributing
|
76
|
+
|
77
|
+
1. Fork it ( http://github.com/<my-github-username>/callchain/fork )
|
78
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
79
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
80
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
81
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/callchain.gemspec
ADDED
@@ -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 'callchain/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "callchain"
|
8
|
+
spec.version = Callchain::VERSION
|
9
|
+
spec.authors = ["csquared"]
|
10
|
+
spec.email = ["christopher.continanza@gmail.com"]
|
11
|
+
spec.summary = %q{Simple, composable call chains}
|
12
|
+
spec.description = %q{Simple, composalbe call chains}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
data/lib/callchain.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require "callchain/version"
|
2
|
+
|
3
|
+
module CallChain
|
4
|
+
# CallChain implementations
|
5
|
+
#
|
6
|
+
# Individual filters do work with ::call(object)
|
7
|
+
#
|
8
|
+
# Compose a CallChain with the ::use method
|
9
|
+
#
|
10
|
+
# Compose a CallChain of call chains because it exports ::call(object)
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
#
|
14
|
+
# Pricer = Class.new(CallChain)
|
15
|
+
#
|
16
|
+
# class AppTierPricer < Pricer
|
17
|
+
# use PlanScoper, Quantizer, AppGrouper
|
18
|
+
# use UserGrouper
|
19
|
+
# end
|
20
|
+
|
21
|
+
def use(*args)
|
22
|
+
@chain ||= []
|
23
|
+
@chain.push(*args)
|
24
|
+
end
|
25
|
+
|
26
|
+
def chain
|
27
|
+
@chain
|
28
|
+
end
|
29
|
+
|
30
|
+
def call(thing)
|
31
|
+
return thing unless chain
|
32
|
+
chain.each do |filter|
|
33
|
+
thing = filter.call(thing)
|
34
|
+
yield thing, filter if block_given?
|
35
|
+
end
|
36
|
+
thing
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'callchain'
|
3
|
+
|
4
|
+
module CallChainTestTemplate
|
5
|
+
def chain
|
6
|
+
@chain ||= new_chain
|
7
|
+
end
|
8
|
+
|
9
|
+
# create a Module, Class, and lambda
|
10
|
+
def setup
|
11
|
+
super
|
12
|
+
@chain = nil
|
13
|
+
@append_a = Module.new
|
14
|
+
@append_a.define_singleton_method(:call) do |thing|
|
15
|
+
thing << 'a'
|
16
|
+
thing
|
17
|
+
end
|
18
|
+
|
19
|
+
@append_b = Class.new
|
20
|
+
@append_b.define_singleton_method(:call) do |thing|
|
21
|
+
thing << 'b'
|
22
|
+
thing
|
23
|
+
end
|
24
|
+
|
25
|
+
@append_c = lambda do |thing|
|
26
|
+
thing << 'c'
|
27
|
+
thing
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# No shirt, no shoes, no problem
|
32
|
+
def test_noops_with_no_composites
|
33
|
+
result = chain.call([])
|
34
|
+
assert_equal([], result)
|
35
|
+
end
|
36
|
+
|
37
|
+
# #chain returns the chain
|
38
|
+
def test_chain_accessor
|
39
|
+
chain.use @append_a, @append_b
|
40
|
+
assert_equal([@append_a, @append_b], chain.chain)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Single call to ::use
|
44
|
+
def test_with_one_composite
|
45
|
+
chain.use @append_a
|
46
|
+
result = chain.call([])
|
47
|
+
assert_equal(['a'], result)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Single call to ::use with multiple classes
|
51
|
+
# are chained in the order they are added
|
52
|
+
def test_with_multiple_composites
|
53
|
+
chain.use @append_a, @append_b
|
54
|
+
result = chain.call([])
|
55
|
+
assert_equal(['a', 'b'], result)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Multiple calls to ::use are chained in
|
59
|
+
# the order they are added
|
60
|
+
def test_with_multiple_compositions
|
61
|
+
chain.use @append_a, @append_b
|
62
|
+
chain.use @append_c
|
63
|
+
result = chain.call([])
|
64
|
+
assert_equal(['a', 'b', 'c'], result)
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_composite_chain
|
68
|
+
chain2 = new_chain
|
69
|
+
chain2.use @append_a, @append_b, @append_c
|
70
|
+
chain.use chain2, @append_c
|
71
|
+
result = chain.call([])
|
72
|
+
assert_equal(['a', 'b', 'c', 'c'], result)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Test to the two different ways of mixin-in
|
77
|
+
# extend for class-level methods
|
78
|
+
# include for instance-level methods
|
79
|
+
# exposes the same interface
|
80
|
+
class CallChainExtendTest < Minitest::Test
|
81
|
+
include CallChainTestTemplate
|
82
|
+
|
83
|
+
def new_chain
|
84
|
+
chain_class = Class.new
|
85
|
+
chain_class.extend(CallChain)
|
86
|
+
chain_class
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
class CallChainIncludeTest < Minitest::Test
|
91
|
+
include CallChainTestTemplate
|
92
|
+
def new_chain
|
93
|
+
chain_class = Class.new
|
94
|
+
chain_class.include(CallChain)
|
95
|
+
chain_class.new
|
96
|
+
end
|
97
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: callchain
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- csquared
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-26 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.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Simple, composalbe call chains
|
42
|
+
email:
|
43
|
+
- christopher.continanza@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- callchain.gemspec
|
54
|
+
- lib/callchain.rb
|
55
|
+
- lib/callchain/version.rb
|
56
|
+
- test/callchain_test.rb
|
57
|
+
homepage: ''
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.2.0
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: Simple, composable call chains
|
81
|
+
test_files:
|
82
|
+
- test/callchain_test.rb
|