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.



27
28
29
30
31
# File 'lib/anyway/tracing.rb', line 27

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.



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

def source
  @source
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#clearObject



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

def clear
  value.clear
end

#digObject



33
34
35
# File 'lib/anyway/tracing.rb', line 33

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

#dupObject



98
99
100
# File 'lib/anyway/tracing.rb', line 98

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

#keep_ifObject

Raises:

  • (ArgumentError)


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

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)


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

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



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/anyway/tracing.rb', line 50

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



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 102

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 " =>"
          q.breakable " " unless v.trace?
          q.pp v
        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_value(val, *path, key, **opts) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/anyway/tracing.rb', line 37

def record_value(val, *path, key, **opts)
  trace =
    if val.is_a?(Hash)
      Trace.new.tap { _1.merge_values(val, **opts) }
    else
      Trace.new(:value, val, **opts)
    end
  target_trace = path.empty? ? self : value.dig(*path)
  target_trace.value[key.to_s] = trace

  val
end

#to_hObject



90
91
92
93
94
95
96
# File 'lib/anyway/tracing.rb', line 90

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

#trace?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/anyway/tracing.rb', line 86

def trace?
  type == :trace
end