Class: Binding

Inherits:
Object show all
Defined in:
lib/nrser/core_ext/binding.rb,
lib/nrser/labs/core_ext/binding.rb

Overview

Extension methods for Binding

Instance Method Summary collapse

Instance Method Details

#call_block(*args, &block) ⇒ Object

Experimental “adaptive” call from local variable names.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nrser/labs/core_ext/binding.rb', line 5

def call_block *args, &block
  arg_count = 0
  arg_rest = false
  call_kwds = {}
  block.parameters.each do |type, name|
    logger.debug "Processing", type: type, name: name
    
    case type
    when :req, :opt
      arg_count += 1
    when :keyreq, :key
      if self.local_variable_defined? name
        call_kwds[name] = self.local_variable_get name
      end
    when :rest
      arg_rest = true
    end
  end
  
  call_args = if arg_rest
    args
  else
    args[0...arg_count]
  end
  
  logger.debug "CALLING WITH",
    args: call_args,
    kwds: call_kwds
  
  block.call *call_args, **call_kwds
end

#erb(source) ⇒ Object Also known as: template

Calls NRSER.template with ‘self` prepended to `*args`



13
14
15
16
17
18
19
20
21
22
# File 'lib/nrser/core_ext/binding.rb', line 13

def erb source
  require 'erb'
  
  NRSER.filter_repeated_blank_lines(
    NRSER.with_indent_tagged( NRSER.dedent( source ) ) { |tagged_str|
      ERB.new( tagged_str ).result( self )
    },
    remove_leading: true
  )
end

#local_valuesArray<Object>

Get a Array of all local variable values.

Returns:



40
41
42
# File 'lib/nrser/core_ext/binding.rb', line 40

def local_values
  self.local_variables.map { |symbol| local_variable_get symbol }
end

#localsHash<Symbol, Object>

Get a Hash of all local variable names (as Symbol) to values.

Returns:



31
32
33
# File 'lib/nrser/core_ext/binding.rb', line 31

def locals
  self.local_variables.assoc_to { |symbol| local_variable_get symbol }
end