Class: BindableBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/bindable_block.rb,
lib/bindable_block/version.rb

Defined Under Namespace

Classes: ArgAligner

Constant Summary collapse

VERSION =
"0.0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, &block) ⇒ BindableBlock

Returns a new instance of BindableBlock.



52
53
54
55
56
57
58
# File 'lib/bindable_block.rb', line 52

def initialize(klass, &block)
  @original_block  = block

  klass.__send__ :define_method, method_name, &block
  @instance_method = klass.instance_method method_name
  klass.__send__ :remove_method, method_name
end

Instance Attribute Details

#instance_methodObject (readonly)

Returns the value of attribute instance_method.



60
61
62
# File 'lib/bindable_block.rb', line 60

def instance_method
  @instance_method
end

#original_blockObject (readonly)

Returns the value of attribute original_block.



60
61
62
# File 'lib/bindable_block.rb', line 60

def original_block
  @original_block
end

Instance Method Details

#bind(target) ⇒ Object



62
63
64
65
66
# File 'lib/bindable_block.rb', line 62

def bind(target)
  Proc.new do |*args, &block|
    instance_method.bind(target).call(*align(args), &block)
  end
end

#call(*args, &block) ⇒ Object



68
69
70
# File 'lib/bindable_block.rb', line 68

def call(*args, &block)
  original_block.call(*args, &block)
end

#to_procObject



72
73
74
# File 'lib/bindable_block.rb', line 72

def to_proc
  method(:call).to_proc
end