Module: Loba::Internal::Value::ValueHelper Private

Defined in:
lib/loba/internal/value/value_helper.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Internal helper functions for Value.phrases

Class Method Summary collapse

Class Method Details

.class_name(depth: 0) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prepare display of class name from where Loba was invoked

Parameters:

  • depth (Integer) (defaults to: 0)

    depth in call stack to retrieve class name from

Returns:

  • (String)

    name of class where Loba was invoked



90
91
92
93
94
95
96
97
98
99
# File 'lib/loba/internal/value/value_helper.rb', line 90

def class_name(depth: 0)
  m = binding.of_caller(depth + 1).eval('self.class.name')
  if m.nil? || m.empty?
    '<anonymous class>'
  elsif m == 'Class'
    binding.of_caller(depth + 1).eval('self.name')
  else
    m
  end
end

.evaluate(argument:, inspect: true, depth: 0) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Evaluate an arguments value from where it’s bound.

Parameters:

  • argument (Symbol, Object)

    the value or variable for which information is to be retrieved

  • inspect (Boolean) (defaults to: true)

    when true, force #inspect to be called against argument when evaluating

  • depth (Integer) (defaults to: 0)

    depth in call stack to start evaluation from

Returns:

  • (Object)

    value of the argument when Loba was invoked



80
81
82
83
84
85
# File 'lib/loba/internal/value/value_helper.rb', line 80

def evaluate(argument:, inspect: true, depth: 0)
  return inspect ? argument.inspect : argument unless argument.is_a?(Symbol)

  evaluation = binding.of_caller(depth + 1).eval(argument.to_s)
  inspect ? evaluation.inspect : evaluation
end

.label(argument:, explicit_label: nil) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds a label for display based on the argument when instantiated. If the argument (when instantiated) is not a symbol, it may not be possible to infer a label; in that case, “[unknown value]”“ is returned.

Parameters:

  • argument (Symbol, Object)

    the value or variable for which information is to be retrieved

    • If a symbol, it is assumed to be a reference to a variable and a label can be inferred.

    • If any other type, it is assumed to be a literal value to and a label should be supplied when instantiated.

  • explicit_label (String) (defaults to: nil)

    when provided, an explicit label to use; will override any possible inferred label

Returns:

  • (String)

    label



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/loba/internal/value/value_helper.rb', line 61

def label(argument:, explicit_label: nil)
  text = if explicit_label.nil?
           argument.is_a?(Symbol) ? "#{argument}:" : nil
         elsif explicit_label.respond_to?(:strip)
           s = explicit_label.strip
           s += ':' unless s[-1] == ':'
           s
         end

  text.nil? ? '[unknown value]:' : text.to_s
end

.line(depth: 0) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Identify source code line from where Loba was invoked

Parameters:

  • depth (Integer) (defaults to: 0)

    depth in call stack to retrieve source code line from

Returns:

  • (Integer)

    source code line number where Loba was invoked



25
26
27
# File 'lib/loba/internal/value/value_helper.rb', line 25

def line(depth: 0)
  caller[depth]
end

.method_name(depth: 0) ⇒ Symbol, String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prepare display of method name from where Loba was invoked

Parameters:

  • depth (Integer) (defaults to: 0)

    depth in call stack to retrieve method name from

Returns:

  • (Symbol, String)

    name of class where Loba was invoked



117
118
119
120
# File 'lib/loba/internal/value/value_helper.rb', line 117

def method_name(depth: 0)
  m = binding.of_caller(depth + 1).eval('self.send(:__method__)')
  m.nil? || m.empty? ? '<anonymous method>' : m
end

.method_type(depth: 0) ⇒ :class, :instance

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prepare display of whether the method from where Loba was invoked is for a class or an instance

Parameters:

  • depth (Integer) (defaults to: 0)

    depth in call stack

Returns:

  • (:class)

    if method in call stack is a class method

  • (:instance)

    if method in call stack is an instance method



106
107
108
109
110
111
112
# File 'lib/loba/internal/value/value_helper.rb', line 106

def method_type(depth: 0)
  if binding.of_caller(depth + 1).eval('self.class.name') == 'Class'
    :class
  else
    :instance
  end
end

.tag(depth: 0) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Assemble display that shows the method invoking Loba

Parameters:

  • depth (Integer) (defaults to: 0)

    depth in call stack to retrieve tag from

Returns:

  • (String)

    tag for where Loba was invoked, wrapped in “[” and “]”



13
14
15
16
17
18
19
20
# File 'lib/loba/internal/value/value_helper.rb', line 13

def tag(depth: 0)
  delim = { class: '.', instance: '#' }
  '[' \
    "#{class_name(depth: depth + 1)}" \
    "#{delim[method_type(depth: depth + 1)]}" \
    "#{method_name(depth: depth + 1)}" \
    ']'
end

.value(argument:, inspect: true, depth: 0) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prepare display of an argument’s value.

Parameters:

  • argument (Symbol, Object)

    the value or variable for which information is to be retrieved

  • inspect (Boolean) (defaults to: true)

    when true, force #inspect to be called against argument when evaluating

  • depth (Integer) (defaults to: 0)

    depth in call stack to start evaluation from

Returns:

  • (String)

    value representation of argument for display



36
37
38
39
40
41
42
43
44
45
# File 'lib/loba/internal/value/value_helper.rb', line 36

def value(argument:, inspect: true, depth: 0)
  val = evaluate(argument: argument, inspect: inspect, depth: depth + 1)

  # #inspect adds explicit quotes to strings, so strip back off since #inspect
  # always returns a String
  val = Loba::Internal.unquote(val) if inspect

  # make nils obvious
  val.nil? ? '-nil-' : val.to_s
end