Class: Verifly::Applicator::MethodExtractor

Inherits:
Verifly::Applicator show all
Defined in:
lib/verifly/applicator.rb

Overview

MethodExtractor is used when applicable is a symbol. It extracts a method from binding_ and executes it on binding_ (so it works just like send except it sends nothing when method arity is zero).

Examples:

Applicator.call(Applicator.build(:foo), User.new, context)
# => User.new.foo(context)
# or => User.new.foo, if it does not accept context

Instance Attribute Summary

Attributes inherited from Verifly::Applicator

#applicable

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Verifly::Applicator

#==, call, #initialize

Methods included from ClassBuilder::Mixin

#build, #build_class, #buildable_classes=

Constructor Details

This class inherits a constructor from Verifly::Applicator

Class Method Details

.build_class(applicable) ⇒ Object

Parameters:

  • applicable (Symbol)

Returns:

  • MethodExtractor if applicable is a Symbol

  • (nil)

    otherwise



59
60
61
# File 'lib/verifly/applicator.rb', line 59

def self.build_class(applicable)
  self if applicable.is_a?(Symbol)
end

Instance Method Details

#binding_method(binding_) ⇒ Method

Returns method, extracted from binding.

Parameters:

  • binding_ (#instance_exec)

    binding to find relative source

Returns:

  • (Method)

    method, extracted from binding



90
91
92
# File 'lib/verifly/applicator.rb', line 90

def binding_method(binding_)
  binding_.is_a?(Binding) ? binding_.receiver : binding_
end

#call(binding_, *context) ⇒ Object

Returns application result.

Parameters:

  • binding_ (#instance_exec)

    target to apply applicable to

  • context

    additional info to send to applicable

Returns:

  • application result



66
67
68
69
70
71
72
# File 'lib/verifly/applicator.rb', line 66

def call(binding_, *context)
  if binding_.is_a?(Binding)
    call_on_binding(binding_, *context)
  else
    invoke_lambda(binding_.method(applicable), binding_, *context)
  end
end

#source(binding_) ⇒ String

Returns relative method source defenition.

Parameters:

  • binding_ (#instance_exec)

    binding to find relative source

Returns:

  • (String)

    relative method source defenition

Raises:

  • (NameError)

    if method does not exist on binding_



84
85
86
# File 'lib/verifly/applicator.rb', line 84

def source(binding_)
  binding_method(binding_).method(applicable).source
end

#source_location(binding_) ⇒ [String, Integer]

Returns (file, line) location of calblack source (if exists).

Parameters:

  • binding_ (#instance_exec)

    binding to find relative source

Returns:

  • ([String, Integer])

    (file, line) location of calblack source (if exists)

Raises:

  • (NameError)

    if method does not exist on binding_



77
78
79
# File 'lib/verifly/applicator.rb', line 77

def source_location(binding_)
  binding_method(binding_).method(applicable).source_location
end