file_series 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.autotest ADDED
@@ -0,0 +1,5 @@
1
+ # less resource-intense way to watch for changed files, OSX only.
2
+ require 'autotest/fsevent'
3
+
4
+ # show failure messages via growl.
5
+ require 'autotest/growl'
data/Gemfile CHANGED
@@ -6,6 +6,9 @@ source "http://rubygems.org"
6
6
  # Add dependencies to develop your gem here.
7
7
  # Include everything needed to run rake, tests, features, etc.
8
8
  group :development do
9
+ gem "autotest-standalone"
10
+ gem "autotest-fsevent"
11
+ gem "autotest-growl"
9
12
  gem "rspec", "~> 2.8.0"
10
13
  gem "rdoc", "~> 3.12"
11
14
  gem "bundler", "~> 1.0.0"
data/Gemfile.lock CHANGED
@@ -1,7 +1,12 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
+ autotest-fsevent (0.2.8)
5
+ sys-uname
6
+ autotest-growl (0.2.16)
7
+ autotest-standalone (4.5.9)
4
8
  diff-lcs (1.1.3)
9
+ ffi (1.1.0)
5
10
  git (1.2.5)
6
11
  jeweler (1.8.4)
7
12
  bundler (~> 1.0)
@@ -20,12 +25,17 @@ GEM
20
25
  rspec-expectations (2.8.0)
21
26
  diff-lcs (~> 1.1.2)
22
27
  rspec-mocks (2.8.0)
28
+ sys-uname (0.9.0)
29
+ ffi (>= 1.0.0)
23
30
  timecop (0.3.5)
24
31
 
25
32
  PLATFORMS
26
33
  ruby
27
34
 
28
35
  DEPENDENCIES
36
+ autotest-fsevent
37
+ autotest-growl
38
+ autotest-standalone
29
39
  bundler (~> 1.0.0)
30
40
  jeweler (~> 1.8.4)
31
41
  rdoc (~> 3.12)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
data/file_series.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "file_series"
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alex Dean"]
12
- s.date = "2012-07-18"
12
+ s.date = "2012-07-19"
13
13
  s.description = "Automatically start writing to a new file every X seconds without any locking or file moving/renaming."
14
14
  s.email = "alex@crackpot.org"
15
15
  s.extra_rdoc_files = [
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
17
17
  "README.rdoc"
18
18
  ]
19
19
  s.files = [
20
+ ".autotest",
20
21
  ".document",
21
22
  ".rspec",
22
23
  "Gemfile",
@@ -40,12 +41,18 @@ Gem::Specification.new do |s|
40
41
  s.specification_version = 3
41
42
 
42
43
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ s.add_development_dependency(%q<autotest-standalone>, [">= 0"])
45
+ s.add_development_dependency(%q<autotest-fsevent>, [">= 0"])
46
+ s.add_development_dependency(%q<autotest-growl>, [">= 0"])
43
47
  s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
44
48
  s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
45
49
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
46
50
  s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
47
51
  s.add_development_dependency(%q<timecop>, ["= 0.3.5"])
48
52
  else
53
+ s.add_dependency(%q<autotest-standalone>, [">= 0"])
54
+ s.add_dependency(%q<autotest-fsevent>, [">= 0"])
55
+ s.add_dependency(%q<autotest-growl>, [">= 0"])
49
56
  s.add_dependency(%q<rspec>, ["~> 2.8.0"])
50
57
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
51
58
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
@@ -53,6 +60,9 @@ Gem::Specification.new do |s|
53
60
  s.add_dependency(%q<timecop>, ["= 0.3.5"])
54
61
  end
55
62
  else
63
+ s.add_dependency(%q<autotest-standalone>, [">= 0"])
64
+ s.add_dependency(%q<autotest-fsevent>, [">= 0"])
65
+ s.add_dependency(%q<autotest-growl>, [">= 0"])
56
66
  s.add_dependency(%q<rspec>, ["~> 2.8.0"])
57
67
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
58
68
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
data/lib/file_series.rb CHANGED
@@ -33,13 +33,13 @@ class FileSeries
33
33
  @current_ts = nil
34
34
  @filename_prefix = options[:prefix] || DEFAULT_PREFIX
35
35
  @rotate_freq = options[:rotate_every] || DEFAULT_FREQ #seconds
36
- @binary_writes = options[:binary]
36
+ @binary_mode = options[:binary]
37
37
  @separator = options[:separator] || DEFAULT_SEPARATOR
38
38
  end
39
39
 
40
40
  # write something to the current log file.
41
41
  def write(message)
42
- log_file.write(message + @separator)
42
+ log_file.write(message.to_s + @separator)
43
43
  end
44
44
 
45
45
  # return a File object for the current log file.
@@ -65,7 +65,7 @@ class FileSeries
65
65
  def rotate(ts=nil)
66
66
  ts ||= this_period
67
67
  @file.close if @file
68
- @file = File.open(filename(ts), "a#{'b' if @binary_writes}")
68
+ @file = File.open(filename(ts), "a#{'b' if @binary_mode}")
69
69
  @current_ts = ts
70
70
  end
71
71
 
@@ -88,4 +88,13 @@ class FileSeries
88
88
  end
89
89
  end
90
90
 
91
+ # enumerate over all the writes in a series, across all files.
92
+ def each
93
+ complete_files.sort.each do |file|
94
+ File.open(file,"r#{'b' if @binary_mode}").each_line(@separator) do |raw|
95
+ yield raw
96
+ end
97
+ end
98
+ end
99
+
91
100
  end
@@ -38,6 +38,17 @@ describe "FileSeries" do
38
38
 
39
39
  end
40
40
 
41
+ it "should support binary file writing" do
42
+ # TODO: need to come up with some sequence which can't be written in non-binary mode.
43
+ # Trying to write a Marshal'd version of
44
+ # https://github.com/tedconf/videometrics/blob/83ad528b013ce591ac2500d7317e0904270356f9/spec/controllers/imports_controller_spec.rb#L92
45
+ # in non-binary mode fails with
46
+ # Failure/Error: post :record_event, :event => @event
47
+ # Encoding::UndefinedConversionError:
48
+ # "\x87" from ASCII-8BIT to UTF-8
49
+ # Writing in binary mode works fine. How to reproduce that more simply for a test?
50
+ end
51
+
41
52
  describe "#write" do
42
53
  it "should call log_file.write with message and separator" do
43
54
  fs = FileSeries.new(:separator=>'...')
@@ -100,4 +111,71 @@ describe "FileSeries" do
100
111
  end
101
112
  end
102
113
 
114
+ describe "#each" do
115
+ it "should enumerate all lines in all files in a series" do
116
+ fs = FileSeries.new(:dir=>test_dir, :rotate_every=>60, :prefix=>'events')
117
+
118
+ # write 3 files with consecutive integers.
119
+ Timecop.freeze(100) do
120
+ (0..9).each do |i|
121
+ fs.write i
122
+ end
123
+ end
124
+ Timecop.freeze(160) do
125
+ (10..19).each do |i|
126
+ fs.write i
127
+ end
128
+ end
129
+ Timecop.freeze(220) do
130
+ (20..29).each do |i|
131
+ fs.write i
132
+ end
133
+ end
134
+ fs.file.flush
135
+
136
+ out = []
137
+ fs.each do |line|
138
+ out << line
139
+ end
140
+
141
+ out.should == (0..29).to_a.map {|i| i.to_s+"\n" }
142
+ end
143
+
144
+ it "should enumerate all entries in a binary file series" do
145
+ fs = FileSeries.new(
146
+ :binary=>true,
147
+ :separator=>'!~!~!',
148
+ :dir=>test_dir,
149
+ :prefix=>'bin',
150
+ :rotate_every=>60
151
+ )
152
+
153
+ Timecop.freeze(100) do
154
+ (0..9).each do |i|
155
+ fs.write Marshal.dump(i)
156
+ end
157
+ end
158
+ Timecop.freeze(160) do
159
+ (10..19).each do |i|
160
+ fs.write Marshal.dump(i)
161
+ end
162
+ end
163
+ Timecop.freeze(220) do
164
+ (20..29).each do |i|
165
+ fs.write Marshal.dump(i)
166
+ end
167
+ end
168
+ fs.file.flush
169
+
170
+ out = []
171
+ fs.each do |line|
172
+ out << Marshal.load(line)
173
+ end
174
+
175
+ # note that we don't get the separator, and they're Fixnum not String
176
+ out.should == (0..29).to_a.map {|i| i }
177
+
178
+ end
179
+ end
180
+
103
181
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file_series
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,56 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-18 00:00:00.000000000 Z
12
+ date: 2012-07-19 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: autotest-standalone
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: autotest-fsevent
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: autotest-growl
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
14
62
  - !ruby/object:Gem::Dependency
15
63
  name: rspec
16
64
  requirement: !ruby/object:Gem::Requirement
@@ -100,6 +148,7 @@ extra_rdoc_files:
100
148
  - LICENSE.txt
101
149
  - README.rdoc
102
150
  files:
151
+ - .autotest
103
152
  - .document
104
153
  - .rspec
105
154
  - Gemfile
@@ -127,7 +176,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
127
176
  version: '0'
128
177
  segments:
129
178
  - 0
130
- hash: -3623692300417860031
179
+ hash: 2322774718501649979
131
180
  required_rubygems_version: !ruby/object:Gem::Requirement
132
181
  none: false
133
182
  requirements: