Class: Anyway::Tracing::Trace
- Inherits:
-
Object
- Object
- Anyway::Tracing::Trace
- Defined in:
- lib/anyway/tracing.rb
Constant Summary collapse
- UNDEF =
Object.new
Instance Attribute Summary collapse
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #clear ⇒ Object
- #dig ⇒ Object
- #dup ⇒ Object
-
#initialize(type = :trace, value = UNDEF, **source) ⇒ Trace
constructor
A new instance of Trace.
- #keep_if ⇒ Object
- #merge!(another_trace) ⇒ Object
- #merge_values(hash, **opts) ⇒ Object
- #pretty_print(q) ⇒ Object
- #record_value(val, *path, key, **opts) ⇒ Object
- #to_h ⇒ Object
- #trace? ⇒ Boolean
Constructor Details
#initialize(type = :trace, value = UNDEF, **source) ⇒ Trace
Returns a new instance of Trace.
25 26 27 28 29 |
# File 'lib/anyway/tracing.rb', line 25 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
#source ⇒ Object (readonly)
Returns the value of attribute source.
23 24 25 |
# File 'lib/anyway/tracing.rb', line 23 def source @source end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
23 24 25 |
# File 'lib/anyway/tracing.rb', line 23 def type @type end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
23 24 25 |
# File 'lib/anyway/tracing.rb', line 23 def value @value end |
Instance Method Details
#clear ⇒ Object
80 |
# File 'lib/anyway/tracing.rb', line 80 def clear() = value.clear |
#dig ⇒ Object
31 32 33 |
# File 'lib/anyway/tracing.rb', line 31 def dig(...) value.dig(...) end |
#dup ⇒ Object
92 |
# File 'lib/anyway/tracing.rb', line 92 def dup() = self.class.new(type, value.dup, source) |
#keep_if ⇒ Object
75 76 77 78 |
# File 'lib/anyway/tracing.rb', line 75 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
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/anyway/tracing.rb', line 62 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
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/anyway/tracing.rb', line 48 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
94 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 |
# File 'lib/anyway/tracing.rb', line 94 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
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/anyway/tracing.rb', line 35 def record_value(val, *path, key, **opts) if val.is_a?(Hash) Trace.new.tap { _1.merge_values(val, **opts) } else Trace.new(:value, val, **opts) end => trace target_trace = path.empty? ? self : value.dig(*path) target_trace.value[key.to_s] = trace val end |
#to_h ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/anyway/tracing.rb', line 84 def to_h if trace? value.transform_values(&:to_h).tap { _1.default_proc = nil } else {value: value, source: source} end end |
#trace? ⇒ Boolean
82 |
# File 'lib/anyway/tracing.rb', line 82 def trace?() = type == :trace |