Class: Anyway::Tracing::Trace

Inherits:
Object
  • Object
show all
Defined in:
lib/anyway/tracing.rb

Constant Summary collapse

UNDEF =
Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = :trace, value = UNDEF, **source) ⇒ Trace

Returns a new instance of Trace.



19
20
21
22
23
# File 'lib/anyway/tracing.rb', line 19

def initialize(type = :trace, value = UNDEF, **source)
  @type = type
  @source = source
  @value = (value == UNDEF) ? Hash.new { |h, k| h[k] = Trace.new(:trace) } : value
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



17
18
19
# File 'lib/anyway/tracing.rb', line 17

def source
  @source
end

#typeObject (readonly)

Returns the value of attribute type.



17
18
19
# File 'lib/anyway/tracing.rb', line 17

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



17
18
19
# File 'lib/anyway/tracing.rb', line 17

def value
  @value
end

Instance Method Details

#clearObject



81
# File 'lib/anyway/tracing.rb', line 81

def clear() = value.clear

#digObject



25
26
27
# File 'lib/anyway/tracing.rb', line 25

def dig(...)
  value.dig(...)
end

#dupObject



93
# File 'lib/anyway/tracing.rb', line 93

def dup() = self.class.new(type, value.dup, **source)

#keep_ifObject

Raises:

  • (ArgumentError)


76
77
78
79
# File 'lib/anyway/tracing.rb', line 76

def keep_if(...)
  raise ArgumentError, "You can only filter :trace type, and this is :#{type}" unless trace?
  value.keep_if(...)
end

#merge!(another_trace) ⇒ Object

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/anyway/tracing.rb', line 63

def merge!(another_trace)
  raise ArgumentError, "You can only merge into a :trace type, and this is :#{type}" unless trace?
  raise ArgumentError, "You can only merge a :trace type, but trying :#{type}" unless another_trace.trace?

  another_trace.value.each do |key, sub_trace|
    if sub_trace.trace?
      value[key].merge! sub_trace
    else
      value[key] = sub_trace
    end
  end
end

#merge_values(hash, **opts) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/anyway/tracing.rb', line 43

def merge_values(hash, **opts)
  return hash unless hash

  hash.each do |key, val|
    if val.is_a?(Hash)
      value[key.to_s].merge_values(val, **opts)
    else
      value[key.to_s] = Trace.new(:value, val, **opts)
    end
  end

  hash
end

#pretty_print(q) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/anyway/tracing.rb', line 95

def pretty_print(q)
  if trace?
    q.nest(2) do
      q.breakable ""
      q.seplist(value, nil, :each) do |k, v|
        q.group do
          q.text k
          q.text " =>"
          if v.trace?
            q.text " { "
            q.pp v
            q.breakable " "
            q.text "}"
          else
            q.breakable " "
            q.pp v
          end
        end
      end
    end
  else
    q.pp value
    q.group(0, " (", ")") do
      q.seplist(source, lambda { q.breakable " " }, :each) do |k, v|
        q.group do
          q.text k.to_s
          q.text "="
          q.text v.to_s
        end
      end
    end
  end
end

#record_key(key, key_trace) ⇒ Object



57
58
59
60
61
# File 'lib/anyway/tracing.rb', line 57

def record_key(key, key_trace)
  @value = Hash.new { |h, k| h[k] = Trace.new(:trace) } unless value.is_a?(::Hash)

  value[key] = key_trace
end

#record_value(val, *path, **opts) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/anyway/tracing.rb', line 29

def record_value(val, *path, **opts)
  key = path.pop
  trace = if val.is_a?(Hash)
    Trace.new.tap { it.merge_values(val, **opts) }
  else
    Trace.new(:value, val, **opts)
  end

  target_trace = path.empty? ? self : value.dig(*path)
  target_trace.record_key(key.to_s, trace)

  val
end

#to_hObject



85
86
87
88
89
90
91
# File 'lib/anyway/tracing.rb', line 85

def to_h
  if trace?
    value.transform_values(&:to_h).tap { it.default_proc = nil }
  else
    {value:, source:}
  end
end

#trace?Boolean

Returns:

  • (Boolean)


83
# File 'lib/anyway/tracing.rb', line 83

def trace?() = type == :trace