lib/bcdd/result/transitions/tracking/enabled.rb in bcdd-result-0.11.0 vs lib/bcdd/result/transitions/tracking/enabled.rb in bcdd-result-0.12.0

- old
+ new

@@ -4,30 +4,22 @@ class Tracking::Enabled attr_accessor :tree, :records, :root_started_at private :tree, :tree=, :records, :records=, :root_started_at, :root_started_at= - def start(name:, desc:) - name_and_desc = [name, desc] + def exec(name, desc) + start(name, desc) - tree.frozen? ? root_start(name_and_desc) : tree.insert!(name_and_desc) - end + transition_node = tree.current - def finish(result:) - node = tree.current + result = EnsureResult[yield] - tree.move_up! + tree.move_to_root! if transition_node.root? - return unless node.root? + finish(result) - duration = (now_in_milliseconds - root_started_at) - - metadata = { duration: duration, tree_map: tree.nested_ids } - - result.send(:transitions=, version: Tracking::VERSION, records: records, metadata: metadata) - - reset! + result end def reset! self.tree = Tracking::EMPTY_TREE end @@ -36,25 +28,53 @@ return if tree.frozen? track(result, time: ::Time.now.getutc) end - def record_and_then(type_arg, arg, subject) + def record_and_then(type_arg, arg, source) type = type_arg.instance_of?(::Method) ? :method : type_arg unless tree.frozen? - current_and_then = { type: type, arg: arg, subject: subject } + current_and_then = { type: type, arg: arg, source: source } current_and_then[:method_name] = type_arg.name if type == :method tree.current.value[1] = current_and_then end yield end + def reset_and_then! + return if tree.frozen? + + tree.current.value[1] = Tracking::EMPTY_HASH + end + private + def start(name, desc) + name_and_desc = [name, desc] + + tree.frozen? ? root_start(name_and_desc) : tree.insert!(name_and_desc) + end + + def finish(result) + node = tree.current + + tree.move_up! + + return unless node.root? + + duration = (now_in_milliseconds - root_started_at) + + metadata = { duration: duration, tree_map: tree.nested_ids } + + result.send(:transitions=, version: Tracking::VERSION, records: records, metadata: metadata) + + reset! + end + TreeNodeValueNormalizer = ->(id, (nam, des)) { [{ id: id, name: nam, desc: des }, Tracking::EMPTY_HASH] } def root_start(name_and_desc) self.root_started_at = now_in_milliseconds @@ -72,9 +92,9 @@ records << { root: root, parent: parent, current: current, result: result, and_then: and_then, time: time } end def now_in_milliseconds - Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) + ::Process.clock_gettime(::Process::CLOCK_MONOTONIC, :millisecond) end end end