bindable_block 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -2
- data/README.mountain_berry_fields.md +1 -0
- data/lib/bindable_block.rb +10 -26
- data/lib/bindable_block/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -3,14 +3,14 @@
|
|
3
3
|
`instance_exec` can't take block arguments. Get around that with BindableProc
|
4
4
|
|
5
5
|
```ruby
|
6
|
-
|
7
6
|
require 'bindable_block'
|
7
|
+
|
8
8
|
User = Struct.new :name
|
9
9
|
greeter = BindableBlock.new(User) { "Welcome, #{name}" }
|
10
10
|
greeter.bind(User.new "Josh").call # => "Welcome, Josh"
|
11
|
-
|
12
11
|
```
|
13
12
|
|
13
|
+
[Here](https://github.com/JoshCheek/surrogate/blob/eb1d7f98a148c032f6d3ef1d8df8b703386f286d/lib/surrogate/options.rb#L32-34) is an example.
|
14
14
|
|
15
15
|
## Installation
|
16
16
|
|
data/lib/bindable_block.rb
CHANGED
@@ -4,8 +4,11 @@ class BindableBlock
|
|
4
4
|
|
5
5
|
# match args to arity, since instance_method has lambda properties
|
6
6
|
class ArgAligner
|
7
|
-
def
|
8
|
-
|
7
|
+
def initialize(args, instance_method)
|
8
|
+
@result, @args, @parameters = [], args, instance_method.parameters.map(&:first)
|
9
|
+
take num :req
|
10
|
+
take [num(:opt), args.size].min
|
11
|
+
take args.size if has? :rest
|
9
12
|
end
|
10
13
|
|
11
14
|
def call
|
@@ -16,36 +19,17 @@ class BindableBlock
|
|
16
19
|
|
17
20
|
attr_reader :args, :parameters, :result
|
18
21
|
|
19
|
-
def
|
20
|
-
|
21
|
-
track_if_has_rest
|
22
|
-
parameters.delete :rest
|
23
|
-
remove_block
|
24
|
-
take num_required
|
25
|
-
parameters.delete :req
|
26
|
-
take 1 while parameters.shift && args.any?
|
27
|
-
take args.size if has_rest?
|
22
|
+
def has?(type)
|
23
|
+
parameters.any? { |param| param == type }
|
28
24
|
end
|
29
25
|
|
30
|
-
def
|
31
|
-
parameters.count { |param| param ==
|
26
|
+
def num(type)
|
27
|
+
parameters.count { |param| param == type }
|
32
28
|
end
|
33
29
|
|
34
30
|
def take(n)
|
35
31
|
n.times { result << args.shift }
|
36
32
|
end
|
37
|
-
|
38
|
-
def remove_block
|
39
|
-
parameters.pop if parameters.last == :block
|
40
|
-
end
|
41
|
-
|
42
|
-
def track_if_has_rest
|
43
|
-
@has_splat = parameters.any? { |param| param == :rest }
|
44
|
-
end
|
45
|
-
|
46
|
-
def has_rest?
|
47
|
-
@has_splat
|
48
|
-
end
|
49
33
|
end
|
50
34
|
|
51
35
|
|
@@ -76,7 +60,7 @@ class BindableBlock
|
|
76
60
|
private
|
77
61
|
|
78
62
|
def align(args)
|
79
|
-
ArgAligner.
|
63
|
+
ArgAligner.new(args, instance_method).call
|
80
64
|
end
|
81
65
|
|
82
66
|
def method_name
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bindable_block
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-09 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: instance_exec can't pass block arguments through. Use a bindable block
|
15
15
|
instead.
|