trecs 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +5 -13
  2. data/Gemfile.lock +13 -1
  3. data/bin/trecs_message +4 -0
  4. data/lib/trecs/player.rb +128 -0
  5. data/lib/trecs/recorder.rb +59 -0
  6. data/lib/trecs/recording_strategies/custom_strategy.rb +5 -0
  7. data/lib/trecs/recording_strategies/hash_strategy.rb +17 -0
  8. data/lib/{recording_strategies/incremental_recording_strategy.rb → trecs/recording_strategies/incremental_strategy.rb} +4 -3
  9. data/lib/trecs/recording_strategies/raw_file_strategy.rb +54 -0
  10. data/lib/{recording_strategies → trecs/recording_strategies}/ttyrec_recording_strategy.rb +0 -0
  11. data/lib/{screens → trecs/screens}/terminal_screen.rb +0 -0
  12. data/lib/trecs/version.rb +1 -1
  13. data/lib/trecs/writers/in_memory_writer.rb +17 -0
  14. data/lib/{recorders/zip_file_recorder.rb → trecs/writers/zip_file_writer.rb} +9 -7
  15. data/lib/trecs.rb +3 -0
  16. data/{spec → old_specs}/player_spec.rb +0 -0
  17. data/{spec → old_specs/recorders}/zip_file_recorder_spec.rb +11 -5
  18. data/{spec → old_specs}/timestamps_spec.rb +0 -0
  19. data/{spec → old_specs}/trecs_message_spec.rb +0 -0
  20. data/{spec → old_specs}/trecs_spec.rb +0 -0
  21. data/pkg/trecs-0.0.3.gem +0 -0
  22. data/spec/spec_helper.rb +90 -20
  23. data/spec/trecs/player_spec.rb +76 -0
  24. data/spec/trecs/recorder_spec.rb +227 -0
  25. data/spec/trecs/recording_strategies/incremental_strategy_spec.rb +43 -0
  26. data/spec/trecs/recording_strategies/raw_file_strategy_spec.rb +66 -0
  27. data/spec/trecs/writers/in_memory_writer_spec.rb +45 -0
  28. data/trecs.gemspec +4 -1
  29. metadata +78 -41
  30. data/lib/player.rb +0 -76
  31. data/lib/recorder.rb +0 -74
  32. data/lib/recording_strategies/raw_file_recording_strategy.rb +0 -42
  33. data/spec/recorder_spec.rb +0 -249
data/lib/player.rb DELETED
@@ -1,76 +0,0 @@
1
- require "timestamps"
2
- require "screens/terminal_screen"
3
-
4
- module TRecs
5
- class Player
6
- attr_reader :output
7
-
8
- def initialize(options={})
9
- time = options.fetch(:time) { nil }
10
- @current_time = time
11
- @step = options.fetch(:step) { 100 }
12
- @output = options.fetch(:output) { TerminalScreen.new }
13
- @testing = options.fetch(:testing) { false }
14
- @ticks = time ? Array(time.to_i) : ticks
15
- end
16
-
17
- def play
18
- ticks.each do |time|
19
- play_frame(time)
20
- sleep(sleep_time) unless testing
21
- end
22
- end
23
-
24
- def tick(time=current_time)
25
- self.current_time = time
26
- get_frame(time)
27
- end
28
-
29
- def timestamps
30
- @timestamps ||= get_timestamps.sort
31
- end
32
-
33
- def generate_ticks
34
- ticks = [0]
35
- curr = 0
36
- while(curr < timestamps.last.to_i)
37
- curr += step
38
- ticks << curr
39
- end
40
- ticks
41
- end
42
-
43
- def ticks
44
- @ticks ||= generate_ticks
45
- end
46
-
47
- def play_frame(time)
48
- output.clear
49
- output.puts tick(time_at(time))
50
- end
51
-
52
- def time_at(time)
53
- Timestamps.new(timestamps).time_at(time)
54
- end
55
-
56
- private
57
- attr_accessor :current_time
58
- attr_reader :step
59
- attr_reader :testing
60
-
61
-
62
-
63
- def get_timestamps
64
- []
65
- end
66
-
67
- def get_frame(time)
68
- ""
69
- end
70
-
71
- def sleep_time
72
- # TODO: Fix this to use the current time
73
- @sleep_time ||= (step/1000.0)*0.8
74
- end
75
- end
76
- end
data/lib/recorder.rb DELETED
@@ -1,74 +0,0 @@
1
- module TRecs
2
- class Recorder
3
- attr_writer :current_time
4
- attr_reader :step
5
-
6
- def initialize(options={})
7
- @step = options.fetch(:step) { 100 }
8
- end
9
-
10
- def next_timestamp
11
- @current_time = -step unless @current_time
12
- @current_time += step
13
- end
14
-
15
- def current_time(time=nil)
16
- if time
17
- @current_time = time
18
- end
19
- @current_time
20
- end
21
-
22
- def current_content(content=nil)
23
- if content
24
- @current_content = content
25
- end
26
- @current_content
27
- end
28
-
29
- def current_frame(options={})
30
- time = options.fetch(:time) { next_timestamp }
31
- content = options.fetch(:content)
32
- current_time(time)
33
- current_content(content)
34
-
35
- if previous_content != content
36
- create_frame
37
- self.previous_content = content
38
- end
39
- end
40
-
41
- def record
42
- start
43
- perform_recording
44
- finish
45
- end
46
-
47
- def stop
48
- recording_strategy.stop
49
- end
50
-
51
- private
52
- attr_accessor :recording
53
- attr_accessor :previous_content
54
- attr_reader :recording_strategy
55
-
56
- def start
57
- self.current_time = nil
58
- self.recording = true
59
- setup
60
- end
61
-
62
- def finish
63
- render
64
- self.recording = false
65
- end
66
-
67
- def create_frame
68
- end
69
-
70
- def perform_recording
71
- recording_strategy.perform
72
- end
73
- end
74
- end
@@ -1,42 +0,0 @@
1
- require "recording_strategy"
2
- module TRecs
3
- class RawFileRecordingStrategy < RecordingStrategy
4
- def initialize(options)
5
- file = options.fetch(:file)
6
- raise "File does not exist: #{file}" unless File.exist?(file)
7
- @file = File.new(file)
8
-
9
- @height = options.fetch(:height) { 24 }
10
- @width = options.fetch(:width) { 80 }
11
- @step = options.fetch(:step) { 100 }
12
-
13
- super
14
- end
15
-
16
- def perform
17
- @recording = true
18
- start_time = Time.now
19
-
20
- while @recording
21
- time = timestamp(Time.now - start_time)
22
- content = File.read(@file)
23
- recorder.current_frame(time: time, content: content)
24
- sleep(sleep_time)
25
- end
26
- end
27
-
28
- def stop
29
- @recording = false
30
- end
31
-
32
- private
33
- def sleep_time
34
- # TODO: Fix this to use the current time
35
- @sleep_time ||= (@step/1000.0)
36
- end
37
-
38
- def timestamp(time)
39
- (time * 1000).to_i
40
- end
41
- end
42
- end
@@ -1,249 +0,0 @@
1
- require "spec_helper"
2
-
3
- require "recorders/message_recorder"
4
-
5
- module TRecs
6
- describe Recorder do
7
- class DummyRecorder < Recorder
8
- attr_reader :result
9
-
10
- def initialize(options={})
11
- frames = options.fetch(:frames) { nil }
12
- @frames = frames.is_a?(Hash) ? frames : Array(frames)
13
- super
14
- end
15
-
16
- def perform_recording
17
- if @frames.is_a?(Hash)
18
- @frames.each do |time, content|
19
- current_frame(time: time, content: content)
20
- end
21
- else
22
- @frames.each do |content|
23
- current_frame(content: content)
24
- end
25
- end
26
- end
27
-
28
- def create_frame
29
- @result[current_time] = current_content
30
- end
31
-
32
- def setup
33
- @result = {}
34
- end
35
-
36
- def render
37
- # no-op
38
- end
39
- end
40
-
41
- context "recording" do
42
- context "with custom timestamps" do
43
- it "records a one frame trecs" do
44
- rec = DummyRecorder.new frames: {0 => "a"}
45
- rec.record
46
-
47
- rec.result.should == {0 => "a"}
48
- end
49
-
50
- it "records a two frames trecs" do
51
- rec = DummyRecorder.new frames: {
52
- 0 => "a",
53
- 100 => "b"
54
- }
55
- rec.record
56
-
57
- rec.result.should == {
58
- 0 => "a",
59
- 100 => "b"
60
- }
61
- end
62
-
63
- it "records a three frames trecs" do
64
- rec = DummyRecorder.new frames: {
65
- 0 => "a",
66
- 100 => "b",
67
- 150 => "c"
68
- }
69
- rec.record
70
-
71
- rec.result.should == {
72
- 0 => "a",
73
- 100 => "b",
74
- 150 => "c"
75
- }
76
- end
77
-
78
- it "calls start_recording" do
79
- rec = DummyRecorder.new
80
- rec.should_receive(:setup)
81
-
82
- rec.record
83
- end
84
-
85
- it "calls finish_recording" do
86
- rec = DummyRecorder.new
87
- rec.should_receive(:render)
88
-
89
- rec.record
90
- end
91
-
92
- end
93
-
94
- context "with custom timestamps" do
95
- it "records a one frame trecs" do
96
- rec = DummyRecorder.new frames: ["a"]
97
- rec.record
98
-
99
- rec.result.should == {0 => "a"}
100
- end
101
-
102
- it "records a two frames trecs" do
103
- rec = DummyRecorder.new frames: ["a", "b"]
104
- rec.record
105
-
106
- rec.result.should == {
107
- 0 => "a",
108
- 100 => "b"
109
- }
110
- end
111
-
112
- it "records a three frames trecs" do
113
- rec = DummyRecorder.new frames: ["a", "b", "c"]
114
- rec.record
115
-
116
- rec.result.should == {
117
- 0 => "a",
118
- 100 => "b",
119
- 200 => "c"
120
- }
121
- end
122
-
123
- it "records a three frames trecs" do
124
- rec = DummyRecorder.new frames: ["a", "b", "c"], step: 75
125
- rec.record
126
-
127
- rec.result.should == {
128
- 0 => "a",
129
- 75 => "b",
130
- 150 => "c"
131
- }
132
- end
133
- end
134
-
135
- context "sanitizing the recording" do
136
- it "records a three frames trecs" do
137
- rec = DummyRecorder.new frames: {
138
- 0 => "a",
139
- 50 => "b",
140
- 100 => "b",
141
- 150 => "c",
142
- }
143
- rec.record
144
-
145
- rec.result.should == {
146
- 0 => "a",
147
- 50 => "b",
148
- 150 => "c",
149
- }
150
- end
151
-
152
- it "records a three frames trecs" do
153
- rec = DummyRecorder.new frames: {
154
- 0 => "a",
155
- 50 => "a",
156
- 100 => "b",
157
- 150 => "c",
158
- }
159
- rec.record
160
-
161
- rec.result.should == {
162
- 0 => "a",
163
- 100 => "b",
164
- 150 => "c",
165
- }
166
- end
167
-
168
- it "records a three frames trecs" do
169
- rec = DummyRecorder.new frames: {
170
- 0 => "a",
171
- 50 => "b",
172
- 100 => "c",
173
- 150 => "c",
174
- }
175
- rec.record
176
-
177
- rec.result.should == {
178
- 0 => "a",
179
- 50 => "b",
180
- 100 => "c",
181
- }
182
- end
183
-
184
- end
185
- end
186
-
187
-
188
- context "timestamps" do
189
- it "starts with 0 as the current time" do
190
- rec = Recorder.new
191
-
192
- rec.current_time.should == nil
193
- end
194
-
195
- it "calculates the next timestamp" do
196
- rec = Recorder.new(step: 100)
197
-
198
- rec.next_timestamp.should == 0
199
- rec.current_time.should == 0
200
- end
201
-
202
- it "calculates the second next timestamp" do
203
- rec = Recorder.new(step: 100)
204
-
205
- rec.next_timestamp
206
- rec.next_timestamp.should == 100
207
- rec.current_time.should == 100
208
- end
209
- end
210
-
211
- describe "#current_time" do
212
- it "sets the current time" do
213
- rec = Recorder.new
214
-
215
- rec.current_time(40)
216
-
217
- rec.current_time.should == 40
218
- end
219
- end
220
-
221
- describe "#current_content" do
222
- it "sets the current time" do
223
- rec = Recorder.new
224
-
225
- rec.current_content("HELLO")
226
-
227
- rec.current_content.should == "HELLO"
228
- end
229
- end
230
-
231
- describe "#current_frame" do
232
- it "sets the current time and content" do
233
- rec = Recorder.new
234
-
235
- rec.current_frame(time: 40, content: "HELLO")
236
-
237
- rec.current_time.should == 40
238
- rec.current_content.should == "HELLO"
239
- end
240
-
241
- it "sets the current time and content" do
242
- rec = Recorder.new()
243
- rec.should_receive(:create_frame)
244
-
245
- rec.current_frame(time: 40, content: "HELLO")
246
- end
247
- end
248
- end
249
- end