bindable_block 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bindable_block.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Josh Cheek
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,27 @@
1
+ # BindableBlock
2
+
3
+ `instance_exec` can't take block arguments. Get around that with BindableProc
4
+
5
+ ```ruby
6
+
7
+ require 'bindable_block'
8
+ User = Struct.new :name
9
+ greeter = BindableBlock.new(User) { "Welcome, #{name}" }
10
+ greeter.bind(User.new "Josh").call # => "Welcome, Josh"
11
+
12
+ ```
13
+
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ gem 'bindable_block'
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install bindable_block
@@ -0,0 +1,28 @@
1
+ # BindableBlock
2
+
3
+ `instance_exec` can't take block arguments. Get around that with BindableProc
4
+
5
+ ```ruby
6
+ <% test 'example', with: :magic_comments do %>
7
+ require 'bindable_block'
8
+
9
+ User = Struct.new :name
10
+ greeter = BindableBlock.new(User) { "Welcome, #{name}" }
11
+ greeter.bind(User.new "Josh").call # => "Welcome, Josh"
12
+ <% end %>
13
+ ```
14
+
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ gem 'bindable_block'
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install bindable_block
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/bindable_block/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Josh Cheek"]
6
+ gem.email = ["josh.cheek@gmail.com"]
7
+ gem.description = %q{instance_exec can't pass block arguments through. Use a bindable block instead.}
8
+ gem.summary = %q{Allows you to bind procs to instances of classes}
9
+
10
+ gem.homepage = ""
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "bindable_block"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = BindableBlock::VERSION
18
+ end
@@ -0,0 +1,3 @@
1
+ class BindableBlock
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,41 @@
1
+ require "bindable_block/version"
2
+
3
+ class BindableBlock
4
+ def initialize(klass, &block)
5
+ @original_block = block
6
+
7
+ klass.__send__ :define_method, method_name, &block
8
+ @instance_method = klass.instance_method method_name
9
+ klass.__send__ :remove_method, method_name
10
+ end
11
+
12
+ def method_name
13
+ @method_name ||= "bindable_block_#{Time.now.to_i}_#{$$}_#{rand 1000000}"
14
+ end
15
+
16
+ def arg_size
17
+ instance_method.arity
18
+ end
19
+
20
+ attr_reader :instance_method, :original_block
21
+
22
+ def bind(target)
23
+ Proc.new do |*args, &block|
24
+ # match args to arity, since instance_method has lambda properties
25
+ if args.size >= arg_size
26
+ args = args.take arg_size
27
+ else
28
+ args[arg_size-1] = nil
29
+ end
30
+ instance_method.bind(target).call(*args, &block)
31
+ end
32
+ end
33
+
34
+ def call(*args, &block)
35
+ original_block.call(*args, &block)
36
+ end
37
+
38
+ def to_proc
39
+ method(:call).to_proc
40
+ end
41
+ end
@@ -0,0 +1,53 @@
1
+ require 'bindable_block'
2
+
3
+ describe BindableBlock do
4
+ let(:default_name) { "Carmen" }
5
+ let(:klass) { Struct.new :name }
6
+ let(:instance) { klass.new default_name }
7
+
8
+ it 'can be bound to instances of the target' do
9
+ block = BindableBlock.new(klass) { self }
10
+ block.bind(instance).call.should equal instance
11
+ end
12
+
13
+ it 'can be rebound' do
14
+ block = BindableBlock.new(klass) { name }
15
+ block.bind(klass.new 'Josh').call.should == 'Josh'
16
+ block.bind(klass.new 'Mei').call.should == 'Mei'
17
+ end
18
+
19
+ it 'can also just be invoked without being bound' do
20
+ def self.a() 1 end
21
+ b = 2
22
+ block = BindableBlock.new(klass) { |c, &d| a + b + c + d.call }
23
+ block.call(3){4}.should == 10
24
+ end
25
+
26
+ it "doesn't care about arity" do
27
+ block = BindableBlock.new(klass) { |a| [a] }.bind(instance)
28
+ block.call.should == [nil]
29
+ block.call(1).should == [1]
30
+ block.call(1, 2).should == [1]
31
+ end
32
+
33
+ it 'can take a block' do
34
+ block = BindableBlock.new(klass) { |a, &b| [a, (b&&b.call)] }.bind(instance)
35
+ block.call(1).should == [1, nil]
36
+ block.call(1){2}.should == [1, 2]
37
+ end
38
+
39
+ it 'can be passed to methods and shit' do
40
+ doubler = lambda { |&block| block.call + block.call }
41
+ doubler.call(&BindableBlock.new(klass) { 12 }).should == 24
42
+
43
+ # sadly this part doesn't work, I think it unwraps the block from the proc, then re-wraps it in a new one, losing the singleton method
44
+ # binder = lambda { |&block| block.bind(instance).call }
45
+ # binder.call(&BindableBlock.new(klass) { name }).should == default_name
46
+ #
47
+ # This was my attempted to_proc implementation
48
+ # bindable_block = self
49
+ # proc = method(:call).to_proc
50
+ # proc.define_singleton_method(:bind) { |target| bindable_block.bind target }
51
+ # proc
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bindable_block
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Josh Cheek
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-03 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: instance_exec can't pass block arguments through. Use a bindable block
15
+ instead.
16
+ email:
17
+ - josh.cheek@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - README.mountain_berry_fields.md
27
+ - Rakefile
28
+ - bindable_block.gemspec
29
+ - lib/bindable_block.rb
30
+ - lib/bindable_block/version.rb
31
+ - spec/bindable_block_spec.rb
32
+ homepage: ''
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.11
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Allows you to bind procs to instances of classes
56
+ test_files:
57
+ - spec/bindable_block_spec.rb
58
+ has_rdoc: