ruby-chains 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/Gemfile +1 -0
- data/LICENSE.md +21 -0
- data/README.md +64 -0
- data/Rakefile +14 -0
- data/lib/chains.rb +22 -0
- data/lib/chains/version.rb +3 -0
- data/lib/chains/with_chain.rb +17 -0
- data/ruby-chains.gemspec +20 -0
- data/test/chains_test.rb +73 -0
- data/test/test_helper.rb +4 -0
- metadata +72 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 68b9db94f052a9e3a34ba97e410b20dcabf997d0
|
4
|
+
data.tar.gz: 67de85a1e6d40ecd2eff766ab741a22b6e7016a0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: de0bfbad4ae6d8976b50d2922e19d455c8b570c9499f6bc54aae0829975ac453be176ab4b47160ec1dcfcfe6f8d705e016b018ed642c1fc13b019aafb4b5ce6f
|
7
|
+
data.tar.gz: 813449f00ce069ac046b36f07366707c253b14e276b7d9714f0535146738892e0a8f7344ab74a7076ce30e470b4f71d4ac1ef90c28ac1e3961ae1128353ce537
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
gemspec
|
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 James Osler
|
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,64 @@
|
|
1
|
+
# Chains
|
2
|
+
|
3
|
+
## Requiring Chains
|
4
|
+
|
5
|
+
Chains can be used by simply requiring it, and calling `chain` and `with` methods:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
require 'chains'
|
9
|
+
```
|
10
|
+
|
11
|
+
`chain` applies the operations in the block to the object, returning the result of the last expression:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
str = 'Hello'
|
15
|
+
Chains::chain(str, -> {
|
16
|
+
concat(' World')
|
17
|
+
length
|
18
|
+
})
|
19
|
+
# => 11
|
20
|
+
```
|
21
|
+
|
22
|
+
`with` applies the operations in the block to the object, and returns the object:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
str = 'Hello'
|
26
|
+
Chains::with(str, -> {
|
27
|
+
concat(' World')
|
28
|
+
length
|
29
|
+
})
|
30
|
+
# => 'Hello World'
|
31
|
+
```
|
32
|
+
|
33
|
+
|
34
|
+
## Including Chains
|
35
|
+
|
36
|
+
Chains can be included into a class to add the `chain` and `with` methods to the object.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
require 'chains'
|
40
|
+
|
41
|
+
class SuperString < String
|
42
|
+
include Chains
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
Then:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
obj = SuperString.new
|
50
|
+
obj.chain(-> {
|
51
|
+
concat(' World')
|
52
|
+
length
|
53
|
+
})
|
54
|
+
# => 11
|
55
|
+
```
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
obj = SuperString.new
|
59
|
+
obj.with(-> {
|
60
|
+
concat(' World')
|
61
|
+
length
|
62
|
+
})
|
63
|
+
# => 'Hello World'
|
64
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require "bundler"
|
4
|
+
|
5
|
+
Bundler.require
|
6
|
+
|
7
|
+
Rake::TestTask.new('test') do |test|
|
8
|
+
test.libs << 'lib'
|
9
|
+
test.libs << 'test'
|
10
|
+
|
11
|
+
test.test_files = FileList['test/**/*_test.rb']
|
12
|
+
test.warning = false
|
13
|
+
test.verbose = true
|
14
|
+
end
|
data/lib/chains.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'chains/version'
|
2
|
+
require 'chains/with_chain'
|
3
|
+
|
4
|
+
module Chains
|
5
|
+
def with(block)
|
6
|
+
WithChain::with_object(self, block)
|
7
|
+
end
|
8
|
+
|
9
|
+
def chain(block)
|
10
|
+
WithChain::chain_object(self, block)
|
11
|
+
end
|
12
|
+
|
13
|
+
class << self
|
14
|
+
def with(object, block)
|
15
|
+
WithChain::with_object(object, block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def chain(object, block)
|
19
|
+
WithChain::chain_object(object, block)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Chains
|
2
|
+
class WithChain
|
3
|
+
class << self
|
4
|
+
def with_object(object, block)
|
5
|
+
chain_object(object, block)
|
6
|
+
object
|
7
|
+
end
|
8
|
+
|
9
|
+
def chain_object(object, block)
|
10
|
+
object.send(:define_singleton_method, :chain_with_block, block)
|
11
|
+
result = object.chain_with_block
|
12
|
+
object.singleton_class.send(:undef_method, :chain_with_block)
|
13
|
+
result
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/ruby-chains.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'chains/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'ruby-chains'
|
8
|
+
gem.version = Chains::VERSION
|
9
|
+
gem.authors = ['James Osler']
|
10
|
+
gem.email = ['j.osler@gmail.com']
|
11
|
+
gem.description = %q{Chaining and With}
|
12
|
+
gem.licenses = ['MIT']
|
13
|
+
gem.summary = gem.description
|
14
|
+
gem.homepage = ''
|
15
|
+
gem.add_development_dependency "minitest"
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ['lib']
|
20
|
+
end
|
data/test/chains_test.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe 'chains' do
|
4
|
+
describe 'with included chains' do
|
5
|
+
it 'returns the object from with' do
|
6
|
+
result = DummyObject.new.with(-> {
|
7
|
+
add(1)
|
8
|
+
})
|
9
|
+
result.number.must_equal(1)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'returns the result from chain' do
|
13
|
+
result = DummyObject.new.chain(-> {
|
14
|
+
add(1)
|
15
|
+
add(2)
|
16
|
+
})
|
17
|
+
result.must_equal(3)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'can use with within method' do
|
21
|
+
obj = DummyObject.new
|
22
|
+
obj.do_with.number.must_equal(4)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'can use chain within method' do
|
26
|
+
obj = DummyObject.new
|
27
|
+
obj.do_chained.must_equal(4)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'with required Chains' do
|
32
|
+
it 'can with any object' do
|
33
|
+
obj = DummyObject.new
|
34
|
+
result = Chains::with(obj, -> {
|
35
|
+
add(1)
|
36
|
+
})
|
37
|
+
result.number.must_equal(1)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'can chain any object' do
|
41
|
+
obj = DummyObject.new
|
42
|
+
result = Chains::chain(obj, -> {
|
43
|
+
add(2)
|
44
|
+
})
|
45
|
+
result.must_equal(2)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class DummyObject
|
51
|
+
include Chains
|
52
|
+
attr_accessor :number
|
53
|
+
|
54
|
+
def initialize
|
55
|
+
@number = 0
|
56
|
+
end
|
57
|
+
|
58
|
+
def add(num)
|
59
|
+
@number += num
|
60
|
+
end
|
61
|
+
|
62
|
+
def do_chained
|
63
|
+
chain(-> {
|
64
|
+
add(4)
|
65
|
+
})
|
66
|
+
end
|
67
|
+
|
68
|
+
def do_with
|
69
|
+
with(-> {
|
70
|
+
add(4)
|
71
|
+
})
|
72
|
+
end
|
73
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-chains
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Osler
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Chaining and With
|
28
|
+
email:
|
29
|
+
- j.osler@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- .gitignore
|
35
|
+
- Gemfile
|
36
|
+
- LICENSE.md
|
37
|
+
- README.md
|
38
|
+
- Rakefile
|
39
|
+
- lib/chains.rb
|
40
|
+
- lib/chains/version.rb
|
41
|
+
- lib/chains/with_chain.rb
|
42
|
+
- ruby-chains.gemspec
|
43
|
+
- test/chains_test.rb
|
44
|
+
- test/test_helper.rb
|
45
|
+
homepage: ''
|
46
|
+
licenses:
|
47
|
+
- MIT
|
48
|
+
metadata: {}
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 2.0.14
|
66
|
+
signing_key:
|
67
|
+
specification_version: 4
|
68
|
+
summary: Chaining and With
|
69
|
+
test_files:
|
70
|
+
- test/chains_test.rb
|
71
|
+
- test/test_helper.rb
|
72
|
+
has_rdoc:
|