step-track 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +25 -0
- data/lib/step_track.rb +57 -0
- data/lib/step_track/version.rb +3 -0
- data/test/runner.rb +5 -0
- data/test/step_track_test.rb +99 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 223ca527f32902f873a8c4b34a8e083a6bb91ca4
|
4
|
+
data.tar.gz: 1f3c345c51bfe5aa3959795824561ce433609e7c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 45ad270d69c98b573eea8b6bb53e747083e8a8511284726bac97737ced664aa8891a2bbd1cd1a68c4a3a9be0cc1470238a42c733ecf0a218e2c2f0c6fa00ee3a
|
7
|
+
data.tar.gz: dafca62752e93766a0377533cd3e3fe6ed6a946b41fe473076452d95c124548fa5fbd6c3a56edeb062829c1fbd1cfa19230ae67f06cf07e45b261c112ec37062
|
data/LICENSE
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
BSD 2-Clause License
|
2
|
+
|
3
|
+
Copyright (c) 2017, Matthias Geier
|
4
|
+
All rights reserved.
|
5
|
+
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
8
|
+
|
9
|
+
* Redistributions of source code must retain the above copyright notice, this
|
10
|
+
list of conditions and the following disclaimer.
|
11
|
+
|
12
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
14
|
+
and/or other materials provided with the distribution.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
17
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
18
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
19
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
20
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
21
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
22
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
23
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
24
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
25
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/lib/step_track.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module StepTrack
|
4
|
+
extend self
|
5
|
+
|
6
|
+
REF = "step_track/%{track}"
|
7
|
+
|
8
|
+
def init(track)
|
9
|
+
raise ArgumentError, "callback block required" unless block_given?
|
10
|
+
Thread.current[ref(track)] = {
|
11
|
+
steps: [],
|
12
|
+
callback: Proc.new,
|
13
|
+
time: Time.now
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
def push(track, name, payload={})
|
18
|
+
require_init!(track)
|
19
|
+
track_ref = Thread.current[ref(track)]
|
20
|
+
last_step = track_ref[:steps].last
|
21
|
+
track_ref[:steps] << {
|
22
|
+
split: Time.now - (last_step&.[](:time) || track_ref[:time]),
|
23
|
+
duration: Time.now - track_ref[:time],
|
24
|
+
time: Time.now,
|
25
|
+
caller: caller[0],
|
26
|
+
name: name
|
27
|
+
}.merge(payload)
|
28
|
+
end
|
29
|
+
|
30
|
+
def done(track)
|
31
|
+
require_init!(track)
|
32
|
+
track_ref = Thread.current[ref(track)]
|
33
|
+
Thread.current[ref(track)] = nil
|
34
|
+
steps = track_ref.delete(:steps)
|
35
|
+
steps.each { |step| step.delete(:time) }
|
36
|
+
result = {step_count: steps.count}
|
37
|
+
result.merge!(steps.last || {})
|
38
|
+
steps.each_with_index do |step, i|
|
39
|
+
result.merge!(step.map { |k, v| ["step_#{i}_#{k}".to_sym, v] }.to_h)
|
40
|
+
end
|
41
|
+
return track_ref[:callback].call(result)
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def ref(track)
|
47
|
+
(REF % {track: track}).to_sym
|
48
|
+
end
|
49
|
+
|
50
|
+
def initialized?(track)
|
51
|
+
!Thread.current[ref(track)].nil?
|
52
|
+
end
|
53
|
+
|
54
|
+
def require_init!(track)
|
55
|
+
raise ArgumentError, "track not initialized" unless initialized?(track)
|
56
|
+
end
|
57
|
+
end
|
data/test/runner.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
describe "StepTrack" do
|
2
|
+
after do
|
3
|
+
Thread.current[StepTrack.send(:ref, "test")] = nil
|
4
|
+
end
|
5
|
+
|
6
|
+
describe ".init" do
|
7
|
+
it "raises when no block is given" do
|
8
|
+
begin
|
9
|
+
rescued = false
|
10
|
+
StepTrack.init("test")
|
11
|
+
rescue ArgumentError
|
12
|
+
rescued = true
|
13
|
+
end
|
14
|
+
assert rescued
|
15
|
+
end
|
16
|
+
|
17
|
+
it "stores the initialized data into thread context" do
|
18
|
+
StepTrack.init("test") { }
|
19
|
+
data = Thread.current[StepTrack.send(:ref, "test")]
|
20
|
+
assert_equal [], data[:steps],
|
21
|
+
"steps is no empty array #{data[:steps].inspect}"
|
22
|
+
assert data[:callback].is_a?(Proc),
|
23
|
+
"callback is no proc #{data[:callback].inspect}"
|
24
|
+
assert data[:time] <= Time.now,
|
25
|
+
"time #{data[:time].inspect} > #{Time.now.inspect}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe ".push" do
|
30
|
+
before do
|
31
|
+
StepTrack.init("test") { }
|
32
|
+
end
|
33
|
+
|
34
|
+
it "requires initialization" do
|
35
|
+
begin
|
36
|
+
rescued = false
|
37
|
+
StepTrack.push("no_init", "step")
|
38
|
+
rescue ArgumentError
|
39
|
+
rescued = true
|
40
|
+
end
|
41
|
+
assert rescued
|
42
|
+
end
|
43
|
+
|
44
|
+
it "pushes a step including payload into data" do
|
45
|
+
StepTrack.push("test", "step", moo: "bar")
|
46
|
+
data = Thread.current[StepTrack.send(:ref, "test")]
|
47
|
+
step = data[:steps].first
|
48
|
+
assert_equal 1, data[:steps].size
|
49
|
+
assert_equal "step", step[:name]
|
50
|
+
assert_equal "bar", step[:moo]
|
51
|
+
expected_keys = [:name, :split, :duration, :time, :caller]
|
52
|
+
assert_equal expected_keys, expected_keys & step.keys
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe ".done" do
|
57
|
+
before do
|
58
|
+
StepTrack.init("test") { |result| result }
|
59
|
+
StepTrack.push("test", "step", moo: "bar")
|
60
|
+
StepTrack.push("test", "last", gnu: "blu")
|
61
|
+
end
|
62
|
+
|
63
|
+
it "requires initialization" do
|
64
|
+
begin
|
65
|
+
rescued = false
|
66
|
+
StepTrack.done("no_init")
|
67
|
+
rescue ArgumentError
|
68
|
+
rescued = true
|
69
|
+
end
|
70
|
+
assert rescued
|
71
|
+
end
|
72
|
+
|
73
|
+
it "returns the callback result" do
|
74
|
+
result = StepTrack.done("test")
|
75
|
+
assert result.is_a?(Hash), "result is no hash #{result.inspect}"
|
76
|
+
end
|
77
|
+
|
78
|
+
it "sets a step count" do
|
79
|
+
result = StepTrack.done("test")
|
80
|
+
assert_equal 2, result[:step_count]
|
81
|
+
end
|
82
|
+
|
83
|
+
it "merges the last step into result" do
|
84
|
+
result = StepTrack.done("test")
|
85
|
+
assert_equal "last", result[:name]
|
86
|
+
expected_keys = [:name, :split, :duration, :caller]
|
87
|
+
assert_equal expected_keys, expected_keys & result.keys
|
88
|
+
end
|
89
|
+
|
90
|
+
it "enumerates every step into result" do
|
91
|
+
result = StepTrack.done("test")
|
92
|
+
expected_key_parts = [:name, :split, :duration, :caller]
|
93
|
+
2.times do |i|
|
94
|
+
expected_keys = expected_key_parts.map { |k| "step_#{i}_#{k}".to_sym }
|
95
|
+
assert_equal expected_keys, expected_keys & result.keys
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: step-track
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthias Geier
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.10'
|
27
|
+
description: Stores data fragments as steps for an executing trail
|
28
|
+
email: 'mayutamano@gmail.com '
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- LICENSE
|
34
|
+
- lib/step_track.rb
|
35
|
+
- lib/step_track/version.rb
|
36
|
+
- test/runner.rb
|
37
|
+
- test/step_track_test.rb
|
38
|
+
homepage: https://github.com/matthias-geier/track_step
|
39
|
+
licenses:
|
40
|
+
- BSD-2-Clause
|
41
|
+
metadata: {}
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 2.5.1
|
59
|
+
signing_key:
|
60
|
+
specification_version: 4
|
61
|
+
summary: Tracks data fragments across the application
|
62
|
+
test_files:
|
63
|
+
- test/step_track_test.rb
|
64
|
+
- test/runner.rb
|