callable_symbol 1.0.0
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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +4 -0
- data/callable_symbol.gemspec +21 -0
- data/lib/callable_symbol.rb +6 -0
- data/lib/callable_symbol/callable_symbol.rb +38 -0
- data/lib/callable_symbol/version.rb +3 -0
- data/spec/callable_symbol_spec.rb +65 -0
- metadata +74 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Ilya Vorontsov
|
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,33 @@
|
|
1
|
+
# CallableSymbol
|
2
|
+
|
3
|
+
Callable symbol - is an useful extension for Ruby 1.9 &:symbol - syntax that makes it possible to pass arguments for method in block created from Symbol.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'callable_symbol'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install callable_symbol
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
* ['abc','','','def','ghi'].tap(&:delete.('')) # ==> ['abc','def','ghi']
|
22
|
+
* [1,2,3].map(&:to_s.(2)) # ==> ['1','10','11']
|
23
|
+
* ['abc','cdef','xy','z','wwww'].select(&:size.() == 4) # ==> ['cdef', 'wwww']
|
24
|
+
* ['abc','aaA','AaA','z'].count(&:upcase.().succ == 'AAB') # ==> 2
|
25
|
+
* [%w{1 2 3 4 5},%w{6 7 8 9}].map(&:join.().length) # ==> [5,4]
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
1. Fork it
|
30
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
31
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
32
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
33
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'callable_symbol/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "callable_symbol"
|
8
|
+
gem.version = CallableSymbol::VERSION
|
9
|
+
gem.authors = ["Ilya Vorontsov"]
|
10
|
+
gem.email = ["prijutme4ty@gmail.com"]
|
11
|
+
gem.description = %q{Callable symbol - is an extension that makes it possible to pass arguments to block created with Symbol#to_proc}
|
12
|
+
gem.summary = %q{Extension that makes it possible to pass arguments to block created with Symbol#to_proc}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency "rspec", ">= 2.0"
|
21
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# Useful extension for &:symbol - syntax to make it possible to pass arguments for method in block
|
2
|
+
# ['abc','','','def','ghi'].tap(&:delete.('')) # ==> ['abc','def','ghi']
|
3
|
+
# [1,2,3].map(&:to_s.(2)) # ==> ['1','10','11']
|
4
|
+
# ['abc','cdef','xy','z','wwww'].select(&:size.() == 4) # ==> ['cdef', 'wwww']
|
5
|
+
# ['abc','aaA','AaA','z'].count(&:upcase.().succ == 'AAB') # ==> 2
|
6
|
+
# [%w{1 2 3 4 5},%w{6 7 8 9}].map(&:join.().length) # ==> [5,4]
|
7
|
+
class Symbol
|
8
|
+
def call(*args, &block)
|
9
|
+
obj = BasicObject.new.instance_exec(self,args,block) do |meth,params,block|
|
10
|
+
@postprocess_meth = [meth]
|
11
|
+
@postprocess_args = [params]
|
12
|
+
@postprocess_block = [block]
|
13
|
+
self
|
14
|
+
end
|
15
|
+
|
16
|
+
def obj.to_proc
|
17
|
+
# proc/lambda are methods that cannot be used in BasicObject. It's possible to use ->(slf){...} syntax since ruby-1.9.3 p194 but it's too modern and not widely spreaded yet
|
18
|
+
::Proc.new do |slf|
|
19
|
+
@postprocess_meth.zip(@postprocess_args,@postprocess_block).inject(slf) do |result,(call_meth,call_args,call_block)|
|
20
|
+
result.send(call_meth, *call_args,&call_block)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def obj.method_missing(meth,*args,&block)
|
26
|
+
@postprocess_meth << meth
|
27
|
+
@postprocess_args << args
|
28
|
+
@postprocess_block << block
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
def obj.==(other)
|
33
|
+
method_missing(:==, other)
|
34
|
+
end
|
35
|
+
|
36
|
+
obj
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require_relative "../lib/callable_symbol/callable_symbol"
|
2
|
+
|
3
|
+
# TODO: organize and write more correct descriptions
|
4
|
+
describe Symbol do
|
5
|
+
describe '#call' do
|
6
|
+
context 'when symbol curries a block' do
|
7
|
+
it 'should pass a block to a corresponding proc' do
|
8
|
+
:map.(&:to_s).to_proc.call([1,2,3]).should == ['1','2','3']
|
9
|
+
[[1,2,3],[4,5,6]].map(&:map.(&:to_s)).should == [['1','2','3'],['4','5','6']]
|
10
|
+
[[1,2,3],[4,5,6]].map(&:map.(&:to_s.(2))).should == [['1','10','11'],['100','101','110']]
|
11
|
+
['abc','cdef','xy','z','wwww'].select(&:size.() == 4).should == ['cdef', 'wwww']
|
12
|
+
|
13
|
+
[%w{1 2 3 4 5},%w{6 7 8 9 10}].map(&:join.().length).should == [5,6] # method chaining
|
14
|
+
['abc','aaA','AaA','z'].count(&:upcase.().succ == 'AAB').should == 2 # method chaining with ==
|
15
|
+
[[1,2,3]].map( &:map.(&:to_s.(2)).map(&:to_i) ).should == [[1,10,11]] # method chaining with block on initial symbol and on later symbols
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'returned object' do
|
20
|
+
|
21
|
+
it 'should have to_proc method' do
|
22
|
+
expect { :to_s.() }.to respond_to :to_proc
|
23
|
+
expect { :to_s.(2) }.to respond_to :to_proc
|
24
|
+
expect { :to_s.(2) == '110' }.to respond_to :to_proc
|
25
|
+
expect { :to_s.(2).postprocess('arg1','arg2') }.to respond_to :to_proc
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'corresponding proc' do
|
29
|
+
it 'should call method, corresponding to symbol, on first argument' do
|
30
|
+
stub_obj = double('obj')
|
31
|
+
stub_obj.should_receive(:to_s).exactly(:twice)
|
32
|
+
prc_1 = :to_s.(2).to_proc
|
33
|
+
prc_2 = :to_s.(2).to_proc # When used with &, to_proc can be omitted: it's implicitly casted on call (see other examples)
|
34
|
+
prc_1.call(stub_obj) # These forms are almost equivalent, but second is much more concise
|
35
|
+
stub_obj.tap(&prc_2)
|
36
|
+
end
|
37
|
+
context 'when multiple arguments of call specified' do
|
38
|
+
it 'should call using given arguments' do
|
39
|
+
stub_obj = double('obj')
|
40
|
+
stub_obj.should_receive(:gsub).with('before','after')
|
41
|
+
prc = :gsub.('before','after')
|
42
|
+
stub_obj.tap(&prc)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
context 'when the only argument of call specified' do
|
46
|
+
it 'should call using given argument' do
|
47
|
+
stub_obj = double('obj')
|
48
|
+
stub_obj.should_receive(:to_s).with(2)
|
49
|
+
prc = :to_s.(2)
|
50
|
+
stub_obj.tap(&prc)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
context 'when no arguments given' do
|
54
|
+
it 'should call without any arguments' do
|
55
|
+
stub_obj = double('obj')
|
56
|
+
stub_obj.should_receive(:to_s).with()
|
57
|
+
prc = :to_s.()
|
58
|
+
stub_obj.tap(&prc)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: callable_symbol
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ilya Vorontsov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.0'
|
30
|
+
description: Callable symbol - is an extension that makes it possible to pass arguments
|
31
|
+
to block created with Symbol#to_proc
|
32
|
+
email:
|
33
|
+
- prijutme4ty@gmail.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .gitignore
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- callable_symbol.gemspec
|
44
|
+
- lib/callable_symbol.rb
|
45
|
+
- lib/callable_symbol/callable_symbol.rb
|
46
|
+
- lib/callable_symbol/version.rb
|
47
|
+
- spec/callable_symbol_spec.rb
|
48
|
+
homepage: ''
|
49
|
+
licenses: []
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.8.24
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Extension that makes it possible to pass arguments to block created with
|
72
|
+
Symbol#to_proc
|
73
|
+
test_files:
|
74
|
+
- spec/callable_symbol_spec.rb
|