value_protocol 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 +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +22 -0
- data/README.md +67 -0
- data/Rakefile +1 -0
- data/lib/value_protocol.rb +4 -0
- data/lib/value_protocol/protocol.rb +15 -0
- data/lib/value_protocol/version.rb +3 -0
- data/spec/fruit_filter.rb +11 -0
- data/spec/method_backwards_compatibility_spec.rb +30 -0
- data/spec/proc_backwards_compatibility_spec.rb +53 -0
- data/spec/spec_helper.rb +21 -0
- data/spec/symbol_backwards_compatibility_spec.rb +13 -0
- data/spec/value_protocol_spec.rb +44 -0
- data/value_protocol.gemspec +24 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 118ca477a99d2a6c8b2c39461ee9c14aa584f504
|
4
|
+
data.tar.gz: b6d79496c2979eb4b527001644270fc771e8b911
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 18a9e55649e92741c7b49ff9e3a3d7a82351dd0e9013ee6857a3c0461f1c4e709a2f429be20be65469051cb4fa13c39637380fe0dfded4bdf0e72f72d72663b6
|
7
|
+
data.tar.gz: 54452b1627e8d3ed04fa35ef065145a770a811f16e7f335726073e16a51a0358785f4db3d0cf2e504a6b370ebba26f480a00d08f879abf9036fe0e10f23bc892
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 10Pines
|
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/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Nicolas Papagna Maldonado
|
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,67 @@
|
|
1
|
+
# ValueProtocol
|
2
|
+
|
3
|
+
Allows passing any object where a block is expected.
|
4
|
+
It is inspired by the way Smalltalk deals with the same scenario, making use of the ```#value``` message.
|
5
|
+
|
6
|
+
## Motivation
|
7
|
+
|
8
|
+
I wrote this gem to avoid having to wrap objects inside lambdas (and thus, avoid repeating code) when working with messages like ```:detect```.
|
9
|
+
|
10
|
+
I wanted to turn this:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
numbers.detect(lambda{ 2 }) { |number| number.even? }
|
14
|
+
```
|
15
|
+
|
16
|
+
Into something like this, reducing the noise introduced by the ```lambda``` part:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
numbers.detect(2) { |number| number.even? }
|
20
|
+
```
|
21
|
+
|
22
|
+
Also, I wanted to be able to pass any object when an implicit block is expected:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
# create an instance of a complex condition
|
26
|
+
even_numbers = EvenNumberConition.new
|
27
|
+
numbers.select &even_numbers
|
28
|
+
```
|
29
|
+
|
30
|
+
## How does it works?
|
31
|
+
|
32
|
+
Basically, it works by implementing ```:call``` (returning ```self```), and implementing ```:to_proc``` (returning a proc that evaluates ```self.call```) in ```Object```.
|
33
|
+
|
34
|
+
It sound more complicated than it really is. Take a look at the [implementation](lib/value_protocol/protocol.rb) and [specs](spec/value_protocol_spec.rb)!
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
Just pass any object where a block is expected (respecting the & operator when needed). That's all.
|
39
|
+
If you want to implement custom behavior, override the ```:call``` method in your class (remember to respect the signature!).
|
40
|
+
|
41
|
+
For most scenarios, you won't need to override ```:to_proc```.
|
42
|
+
|
43
|
+
## Feel like reading a blog post?
|
44
|
+
|
45
|
+
Check this out! (TODO: add url)
|
46
|
+
|
47
|
+
## Installation
|
48
|
+
|
49
|
+
Add this line to your application's Gemfile:
|
50
|
+
|
51
|
+
gem 'value_protocol'
|
52
|
+
|
53
|
+
And then execute:
|
54
|
+
|
55
|
+
$ bundle
|
56
|
+
|
57
|
+
Or install it yourself as:
|
58
|
+
|
59
|
+
$ gem install value_protocol
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
1. Fork it
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Method backwards compatibility' do
|
4
|
+
|
5
|
+
it 'should not override :call behavior in Method when arguments are not expected' do
|
6
|
+
upcase_method = 'ruby'.method :upcase
|
7
|
+
|
8
|
+
result = upcase_method.call
|
9
|
+
|
10
|
+
result.should == 'RUBY'
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should not override :call behavior in Method when arguments are expected' do
|
14
|
+
add_method = 1.method :+
|
15
|
+
|
16
|
+
result = add_method.call 1
|
17
|
+
|
18
|
+
result.should == 2
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should not override :to_proc behavior in Method' do
|
22
|
+
upcase_method = 'ruby'.method :upcase
|
23
|
+
to_upcase = upcase_method.to_proc
|
24
|
+
|
25
|
+
result = to_upcase.call
|
26
|
+
|
27
|
+
result.should == 'RUBY'
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Proc backwards compatibility' do
|
4
|
+
|
5
|
+
it 'should not override :call behavior in procs when arguments are not expected' do
|
6
|
+
proc = Proc.new { 7 }
|
7
|
+
|
8
|
+
result = proc.call
|
9
|
+
|
10
|
+
result.should == 7
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should not override :call behavior in procs when arguments are expected' do
|
14
|
+
proc = Proc.new { |number| 1 + number }
|
15
|
+
|
16
|
+
result = proc.call 1
|
17
|
+
|
18
|
+
result.should == 2
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should not override :call behavior in lambdas when arguments are not expected' do
|
22
|
+
lambda = lambda { 7 }
|
23
|
+
|
24
|
+
result = lambda.call
|
25
|
+
|
26
|
+
result.should == 7
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should not override :call behavior in lambdas when arguments are expected' do
|
30
|
+
lambda = lambda { |number| 1 + number }
|
31
|
+
|
32
|
+
result = lambda.call 1
|
33
|
+
|
34
|
+
result.should == 2
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should not override :to_proc behavior in procs' do
|
38
|
+
proc = Proc.new {}
|
39
|
+
|
40
|
+
result = proc.to_proc
|
41
|
+
|
42
|
+
result.should == proc
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should not override :to_proc behavior in lambdas' do
|
46
|
+
lambda = lambda {}
|
47
|
+
|
48
|
+
result = lambda.to_proc
|
49
|
+
|
50
|
+
result.should == lambda
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
require 'fruit_filter'
|
9
|
+
require 'value_protocol'
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
13
|
+
config.run_all_when_everything_filtered = true
|
14
|
+
config.filter_run :focus
|
15
|
+
|
16
|
+
# Run specs in random order to surface order dependencies. If you find an
|
17
|
+
# order dependency and want to debug it, you can fix the order by providing
|
18
|
+
# the seed, which is printed after each run.
|
19
|
+
# --seed 1234
|
20
|
+
config.order = 'random'
|
21
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ValueProtocol::Protocol do
|
4
|
+
|
5
|
+
let(:apple) { Object.new }
|
6
|
+
let(:orange) { Object.new }
|
7
|
+
let(:fruits) { [apple, orange] }
|
8
|
+
|
9
|
+
it 'should be possible to pass any object when an explicit block without arguments is expected' do
|
10
|
+
fruit = fruits.detect(apple) { |_| false }
|
11
|
+
|
12
|
+
fruit.should == apple
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should be possible to pass any object when an implicit block with arguments is expected' do
|
16
|
+
apples_only = FruitFilter.new apple
|
17
|
+
|
18
|
+
selected_fruits = fruits.select &apples_only
|
19
|
+
|
20
|
+
selected_fruits.should have(1).item
|
21
|
+
selected_fruits.should include apple
|
22
|
+
end
|
23
|
+
|
24
|
+
def evaluate_implicit_block_without_arguments &a_block
|
25
|
+
a_block.call
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should be possible to pass any object when an implicit block without arguments is expected' do
|
29
|
+
result = self.evaluate_implicit_block_without_arguments &apple
|
30
|
+
|
31
|
+
result.should == apple
|
32
|
+
end
|
33
|
+
|
34
|
+
def evaluate_explicit_block_with_arguments a_block
|
35
|
+
a_block.call :foo
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should be possible to pass any object when an explicit block with arguments is expected' do
|
39
|
+
result = self.evaluate_explicit_block_with_arguments apple
|
40
|
+
|
41
|
+
result.should == apple
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'value_protocol/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'value_protocol'
|
8
|
+
spec.version = ValueProtocol::VERSION
|
9
|
+
spec.authors = ['Nicolas Papagna Maldonado']
|
10
|
+
spec.email = ['nicolas.papagna@10pines.com']
|
11
|
+
spec.summary = %q{Makes all objects polymorphic with respect to the :call message, allowing to use any objects where a block is expected.}
|
12
|
+
spec.description = %q{Makes all objects polymorphic with respect to the :call message, allowing to use any objects where a block is expected.}
|
13
|
+
spec.homepage = 'https://github.com/10Pines/value_protocol.git'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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.3'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: value_protocol
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nicolas Papagna Maldonado
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-06 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Makes all objects polymorphic with respect to the :call message, allowing
|
56
|
+
to use any objects where a block is expected.
|
57
|
+
email:
|
58
|
+
- nicolas.papagna@10pines.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- .gitignore
|
64
|
+
- .rspec
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- lib/value_protocol.rb
|
71
|
+
- lib/value_protocol/protocol.rb
|
72
|
+
- lib/value_protocol/version.rb
|
73
|
+
- spec/fruit_filter.rb
|
74
|
+
- spec/method_backwards_compatibility_spec.rb
|
75
|
+
- spec/proc_backwards_compatibility_spec.rb
|
76
|
+
- spec/spec_helper.rb
|
77
|
+
- spec/symbol_backwards_compatibility_spec.rb
|
78
|
+
- spec/value_protocol_spec.rb
|
79
|
+
- value_protocol.gemspec
|
80
|
+
homepage: https://github.com/10Pines/value_protocol.git
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.0.3
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: Makes all objects polymorphic with respect to the :call message, allowing
|
104
|
+
to use any objects where a block is expected.
|
105
|
+
test_files:
|
106
|
+
- spec/fruit_filter.rb
|
107
|
+
- spec/method_backwards_compatibility_spec.rb
|
108
|
+
- spec/proc_backwards_compatibility_spec.rb
|
109
|
+
- spec/spec_helper.rb
|
110
|
+
- spec/symbol_backwards_compatibility_spec.rb
|
111
|
+
- spec/value_protocol_spec.rb
|