bindable_block 0.0.4 → 0.0.5

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/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
 
@@ -12,6 +12,7 @@ greeter.bind(User.new "Josh").call # => "Welcome, Josh"
12
12
  <% end %>
13
13
  ```
14
14
 
15
+ [Here](https://github.com/JoshCheek/surrogate/blob/eb1d7f98a148c032f6d3ef1d8df8b703386f286d/lib/surrogate/options.rb#L32-34) is an example.
15
16
 
16
17
  ## Installation
17
18
 
@@ -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 self.align(args, instance_method)
8
- new(args, instance_method).call
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 initialize(args, instance_method)
20
- @result, @args, @parameters = [], args, instance_method.parameters.map(&:first)
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 num_required
31
- parameters.count { |param| param == :req }
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.align args, instance_method
63
+ ArgAligner.new(args, instance_method).call
80
64
  end
81
65
 
82
66
  def method_name
@@ -1,3 +1,3 @@
1
1
  class BindableBlock
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
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
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-05 00:00:00.000000000 Z
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.