Class: Code0::ZeroTrack::Context
- Inherits:
-
Object
- Object
- Code0::ZeroTrack::Context
- Defined in:
- lib/code0/zero_track/context.rb
Constant Summary collapse
- LOG_KEY =
'meta'- CORRELATION_ID_KEY =
'correlation_id'- RAW_KEYS =
[CORRELATION_ID_KEY].freeze
Class Method Summary collapse
- .correlation_id ⇒ Object
- .current ⇒ Object
- .log_key(key) ⇒ Object
- .pop(context) ⇒ Object
- .push(new_attributes = {}) ⇒ Object
- .with_context(attributes = {}) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #correlation_id ⇒ Object
- #get_attribute(attribute) ⇒ Object
-
#initialize(values = {}) ⇒ Context
constructor
A new instance of Context.
- #merge(new_attributes) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(values = {}) ⇒ Context
Returns a new instance of Context.
56 57 58 59 60 |
# File 'lib/code0/zero_track/context.rb', line 56 def initialize(values = {}) @data = {} assign_attributes(values) end |
Class Method Details
.correlation_id ⇒ Object
33 34 35 |
# File 'lib/code0/zero_track/context.rb', line 33 def correlation_id current&.correlation_id end |
.current ⇒ Object
37 38 39 |
# File 'lib/code0/zero_track/context.rb', line 37 def current contexts.last end |
.log_key(key) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/code0/zero_track/context.rb', line 41 def log_key(key) key = key.to_s return key if RAW_KEYS.include?(key) return key if key.start_with?("#{LOG_KEY}.") "#{LOG_KEY}.#{key}" end |
.pop(context) ⇒ Object
29 30 31 |
# File 'lib/code0/zero_track/context.rb', line 29 def pop(context) contexts.pop while contexts.include?(context) end |
.push(new_attributes = {}) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/code0/zero_track/context.rb', line 21 def push(new_attributes = {}) new_context = current&.merge(new_attributes) || new(new_attributes) contexts.push(new_context) new_context end |
.with_context(attributes = {}) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/code0/zero_track/context.rb', line 11 def with_context(attributes = {}) context = push(attributes) begin yield(context) ensure pop(context) end end |
Instance Method Details
#[](key) ⇒ Object
73 74 75 |
# File 'lib/code0/zero_track/context.rb', line 73 def [](key) to_h[log_key(key)] end |
#correlation_id ⇒ Object
77 78 79 |
# File 'lib/code0/zero_track/context.rb', line 77 def correlation_id data[CORRELATION_ID_KEY] end |
#get_attribute(attribute) ⇒ Object
81 82 83 84 85 |
# File 'lib/code0/zero_track/context.rb', line 81 def get_attribute(attribute) raw = call_or_value(data[log_key(attribute)]) call_or_value(raw) end |
#merge(new_attributes) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/code0/zero_track/context.rb', line 62 def merge(new_attributes) new_context = self.class.new(data.dup) new_context.assign_attributes(new_attributes) new_context end |
#to_h ⇒ Object
69 70 71 |
# File 'lib/code0/zero_track/context.rb', line 69 def to_h end |