Class: BindableBlock
- Inherits:
-
Proc
- Object
- Proc
- BindableBlock
- Defined in:
- lib/bindable_block.rb,
lib/bindable_block/version.rb,
lib/bindable_block/arg_aligner.rb,
lib/bindable_block/bound_block.rb
Defined Under Namespace
Classes: ArgAligner, BoundBlock
Constant Summary collapse
- VERSION =
"0.0.7"
Instance Method Summary collapse
- #arity ⇒ Object
- #bind(target) ⇒ Object
- #call(*args, &block) ⇒ Object
- #curry(arity = nil) ⇒ Object
-
#initialize(klass = BasicObject, &block) ⇒ BindableBlock
constructor
match args to arity, since instance_method has lambda properties.
Constructor Details
#initialize(klass = BasicObject, &block) ⇒ BindableBlock
match args to arity, since instance_method has lambda properties
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/bindable_block.rb', line 8 def initialize(klass=BasicObject, &block) @klass = klass @original_block = block if curried_values = block.instance_variable_get(:@curried_values) @original_block = curried_values[:original_block] @curried_args = curried_values[:curried_args] @uncurried_size = curried_values[:uncurried_size] end @instance_method = block_to_method klass, @original_block end |
Instance Method Details
#arity ⇒ Object
30 31 32 |
# File 'lib/bindable_block.rb', line 30 def arity @original_block.arity end |
#bind(target) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/bindable_block.rb', line 19 def bind(target) bound = BoundBlock.new @original_block, &@instance_method.bind(target) if @curried_args then bound.curry(@uncurried_size)[*@curried_args] else bound end end |
#call(*args, &block) ⇒ Object
26 27 28 |
# File 'lib/bindable_block.rb', line 26 def call(*args, &block) @original_block.call(*args, &block) end |
#curry(arity = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/bindable_block.rb', line 34 def curry(arity=nil) arity ||= @instance_method.parameters.count { |type, _| type == :req } original_block = @original_block # can't use the imeth, because it's unbound, so no curry bindable_blk_class = self.class klass = @klass proc_maker = lambda do |curried_args, uncurried_size| p = Proc.new do |*args, &block| actual_args = curried_args + args if uncurried_size <= actual_args.size original_block.call(*actual_args, &block) else curried = proc_maker.call actual_args, uncurried_size bindable_blk_class.new klass, &curried end end p.instance_variable_set :@curried_values, { original_block: original_block, curried_args: curried_args, uncurried_size: arity, } p end curried = proc_maker.call [], arity BindableBlock.new klass, &curried end |