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.5"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, &block) ⇒ BindableBlock

Returns a new instance of BindableBlock.



36
37
38
39
40
41
42
# File 'lib/bindable_block.rb', line 36

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.



44
45
46
# File 'lib/bindable_block.rb', line 44

def instance_method
  @instance_method
end

#original_blockObject (readonly)

Returns the value of attribute original_block.



44
45
46
# File 'lib/bindable_block.rb', line 44

def original_block
  @original_block
end

Instance Method Details

#bind(target) ⇒ Object



46
47
48
49
50
# File 'lib/bindable_block.rb', line 46

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

#call(*args, &block) ⇒ Object



52
53
54
# File 'lib/bindable_block.rb', line 52

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

#to_procObject



56
57
58
# File 'lib/bindable_block.rb', line 56

def to_proc
  method(:call).to_proc
end