Class: Trifle::Logger::Tracer::Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/trifle/logger/tracer/hash.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, reference: nil, meta: nil, config: nil) ⇒ Hash

Returns a new instance of Hash.



9
10
11
12
13
14
15
16
17
# File 'lib/trifle/logger/tracer/hash.rb', line 9

def initialize(key:, reference: nil, meta: nil, config: nil)
  @key = key
  @meta = meta
  @config = config
  set_defaults!

  trace("Tracer has been initialized for #{key}")
  @reference = reference || liftoff.first
end

Instance Attribute Details

#artifactsObject

Returns the value of attribute artifacts.



7
8
9
# File 'lib/trifle/logger/tracer/hash.rb', line 7

def artifacts
  @artifacts
end

#dataObject

Returns the value of attribute data.



7
8
9
# File 'lib/trifle/logger/tracer/hash.rb', line 7

def data
  @data
end

#ignoreObject

Returns the value of attribute ignore.



7
8
9
# File 'lib/trifle/logger/tracer/hash.rb', line 7

def ignore
  @ignore
end

#keyObject

Returns the value of attribute key.



7
8
9
# File 'lib/trifle/logger/tracer/hash.rb', line 7

def key
  @key
end

#metaObject

Returns the value of attribute meta.



7
8
9
# File 'lib/trifle/logger/tracer/hash.rb', line 7

def meta
  @meta
end

#referenceObject

Returns the value of attribute reference.



7
8
9
# File 'lib/trifle/logger/tracer/hash.rb', line 7

def reference
  @reference
end

#stateObject

Returns the value of attribute state.



7
8
9
# File 'lib/trifle/logger/tracer/hash.rb', line 7

def state
  @state
end

#tagsObject

Returns the value of attribute tags.



7
8
9
# File 'lib/trifle/logger/tracer/hash.rb', line 7

def tags
  @tags
end

Instance Method Details

#artifact(name, path) ⇒ Object



81
82
83
84
85
86
# File 'lib/trifle/logger/tracer/hash.rb', line 81

def artifact(name, path)
  @data << { at: now, message: name, state: :success, type: :media, size: File.size(path) }
  @artifacts << path
  bump
  path
end

#bumpObject



117
118
119
120
121
122
# File 'lib/trifle/logger/tracer/hash.rb', line 117

def bump
  return unless @bumped_at && @bumped_at <= now - config.bump_every

  @bumped_at = now
  config.on_bump(self)
end

#configObject



36
37
38
# File 'lib/trifle/logger/tracer/hash.rb', line 36

def config
  @config || Trifle::Logger.default
end

#dump_message(message, type:, state:) ⇒ Object



60
61
62
# File 'lib/trifle/logger/tracer/hash.rb', line 60

def dump_message(message, type:, state:)
  @data << { at: now, message: message, state: state, type: type }
end

#dump_result(result) ⇒ Object



64
65
66
67
68
69
# File 'lib/trifle/logger/tracer/hash.rb', line 64

def dump_result(result)
  @data << {
    at: now, message: "#{@result_prefix}#{result.inspect}",
    state: :success, type: :raw
  }
end

#fail!Object



88
89
90
# File 'lib/trifle/logger/tracer/hash.rb', line 88

def fail!
  @state = :error
end

#ignore!Object



108
109
110
# File 'lib/trifle/logger/tracer/hash.rb', line 108

def ignore!
  @ignore = true
end

#keysObject



40
41
42
43
# File 'lib/trifle/logger/tracer/hash.rb', line 40

def keys
  parts = key.split('/')
  parts.count.times.map { |i| parts[0..i].join('/') }
end

#liftoffObject



112
113
114
115
# File 'lib/trifle/logger/tracer/hash.rb', line 112

def liftoff
  @bumped_at = now
  config.on_liftoff(self)
end

#nowObject



71
72
73
# File 'lib/trifle/logger/tracer/hash.rb', line 71

def now
  Time.now.to_i
end

#pop_all_artifactsObject



32
33
34
# File 'lib/trifle/logger/tracer/hash.rb', line 32

def pop_all_artifacts
  @artifacts.pop(@artifacts.count)
end

#pop_all_dataObject



28
29
30
# File 'lib/trifle/logger/tracer/hash.rb', line 28

def pop_all_data
  @data.pop(@data.count)
end

#running?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/trifle/logger/tracer/hash.rb', line 104

def running?
  @state == :running
end

#set_defaults!Object



19
20
21
22
23
24
25
26
# File 'lib/trifle/logger/tracer/hash.rb', line 19

def set_defaults!
  @tags = []
  @data = []
  @artifacts = []
  @state = :running
  @ignore = false
  @result_prefix = '=> '
end

#success!Object



96
97
98
# File 'lib/trifle/logger/tracer/hash.rb', line 96

def success!
  @state = :success
end

#success?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/trifle/logger/tracer/hash.rb', line 100

def success?
  @state == :success
end

#tag(tag) ⇒ Object



75
76
77
78
79
# File 'lib/trifle/logger/tracer/hash.rb', line 75

def tag(tag)
  @tags << tag
  bump
  tag
end

#trace(message, state: :success, head: false) ⇒ Object

rubocop:disable Metrics/MethodLength



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/trifle/logger/tracer/hash.rb', line 45

def trace(message, state: :success, head: false) # rubocop:disable Metrics/MethodLength
  result = yield if block_given?
rescue StandardError => e
  raise e
ensure
  dump_message(
    message,
    type: head ? :head : :text,
    state: e ? :error : state
  )
  dump_result(result) if block_given?
  bump
  result
end

#warn!Object



92
93
94
# File 'lib/trifle/logger/tracer/hash.rb', line 92

def warn!
  @state = :warning
end

#wrapupObject



124
125
126
127
# File 'lib/trifle/logger/tracer/hash.rb', line 124

def wrapup
  success! if running?
  config.on_wrapup(self)
end