midwire_common 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ *0.1.2* (July 29, 2013)
2
+
3
+ * Removed deprecated :rubygems source from Gemfile
4
+ * Removed deprecated :version specification from Guardfile
5
+ * Added a DataFileCache class
6
+
1
7
  *0.1.1* (January 28, 2013)
2
8
 
3
9
  * Updated README.md file with more accurate usage instructions
data/Gemfile CHANGED
@@ -1,2 +1,2 @@
1
- source :rubygems
1
+ source "https://rubygems.org"
2
2
  gemspec
data/Guardfile CHANGED
@@ -1,4 +1,4 @@
1
- guard 'rspec', :version => 2, :cli => '--color --format doc', all_on_start: false, all_after_pass: false do
1
+ guard 'rspec', :cli => '--color --format doc', all_on_start: false, all_after_pass: false do
2
2
  watch(%r{^spec/.+_spec\.rb$})
3
3
  watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
4
  watch('spec/spec_helper.rb') { "spec" }
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Midwire Common Gem
2
2
 
3
- **Version: 0.1.1**
3
+ **Version: 0.1.2**
4
4
 
5
5
  A handy Ruby library for Midwire development
6
6
 
@@ -1,5 +1,6 @@
1
1
  require 'midwire_common/array'
2
2
  require 'midwire_common/enumerable'
3
+ require 'midwire_common/data_file_cache'
3
4
  require 'midwire_common/file'
4
5
  require 'midwire_common/fixnum'
5
6
  require 'midwire_common/float'
@@ -0,0 +1,43 @@
1
+ module MidwireCommon
2
+
3
+ # A simple class to cache data in a file
4
+ class DataFileCache
5
+
6
+ def initialize(filename)
7
+ @cache_dir = File.dirname(filename)
8
+ @cache_file = normalize_filename(filename)
9
+ ensure_cache_dir
10
+ end
11
+
12
+ def put(data)
13
+ x = data.dup
14
+ x = x.join("\n") if x.is_a? Array
15
+ File.open(@cache_file, 'w') {|file| file.write(x)}
16
+ end
17
+
18
+ def get
19
+ File.read(@cache_file)
20
+ end
21
+
22
+ def age
23
+ return nil unless File.exists?(@cache_file)
24
+ (Time.now - File.ctime(@cache_file)).to_f
25
+ end
26
+
27
+ ########################################
28
+ private
29
+
30
+ def ensure_cache_dir
31
+ FileUtils::mkdir_p(@cache_dir) unless File.exists?(@cache_dir)
32
+ end
33
+
34
+ def normalize_filename(filename)
35
+ unless filename.match(Regexp.new(@cache_dir))
36
+ filename = "#{@cache_dir}/#{filename}"
37
+ end
38
+ return filename
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -1,6 +1,6 @@
1
1
  original_verbosity = $VERBOSE
2
2
  $VERBOSE = nil
3
3
  module MidwireCommon
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
6
6
  $VERBOSE = original_verbosity
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe MidwireCommon::DataFileCache do
4
+ let(:cache_dir) {'/tmp/.cache_dir'}
5
+ let(:filename) {"data_file_cache"}
6
+ let(:filepath) {"#{cache_dir}/#{filename}"}
7
+ let(:data) {'this is my data'}
8
+
9
+ before(:each) do
10
+ @cache = MidwireCommon::DataFileCache.new("#{cache_dir}/#{filename}")
11
+ end
12
+
13
+ it "creates a directory for the cache file" do
14
+ File.exists?(cache_dir).should be_true
15
+ end
16
+
17
+ it "caches data" do
18
+ @cache.put(data)
19
+ File.exists?(filepath).should be_true
20
+ x = File.read(filepath)
21
+ x.should == data
22
+ end
23
+
24
+ it "determines the age of the cached data" do
25
+ tm = @cache.age
26
+ (tm > 0).should be_true
27
+ end
28
+
29
+ it "retrieves cached data" do
30
+ dta = @cache.get
31
+ dta.should == data
32
+ end
33
+
34
+ it "prepends the filename with cache dir" do
35
+ testfile = 'testfile'
36
+ nf = @cache.send(:normalize_filename, testfile)
37
+ nf.should == "#{cache_dir}/#{testfile}"
38
+ end
39
+
40
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: midwire_common
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-28 00:00:00.000000000 Z
12
+ date: 2013-07-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -173,6 +173,7 @@ files:
173
173
  - lib/midwire_common.rb
174
174
  - lib/midwire_common/all.rb
175
175
  - lib/midwire_common/array.rb
176
+ - lib/midwire_common/data_file_cache.rb
176
177
  - lib/midwire_common/enumerable.rb
177
178
  - lib/midwire_common/file.rb
178
179
  - lib/midwire_common/file/stat.rb
@@ -187,6 +188,7 @@ files:
187
188
  - lib/tasks/version.rake
188
189
  - midwire_common.gemspec
189
190
  - spec/lib/midwire_common/array_spec.rb
191
+ - spec/lib/midwire_common/data_file_cache_spec.rb
190
192
  - spec/lib/midwire_common/enumerable_spec.rb
191
193
  - spec/lib/midwire_common/file/stat_spec.rb
192
194
  - spec/lib/midwire_common/fixnum_spec.rb
@@ -207,20 +209,27 @@ required_ruby_version: !ruby/object:Gem::Requirement
207
209
  - - ! '>='
208
210
  - !ruby/object:Gem::Version
209
211
  version: '0'
212
+ segments:
213
+ - 0
214
+ hash: -3792838809084270394
210
215
  required_rubygems_version: !ruby/object:Gem::Requirement
211
216
  none: false
212
217
  requirements:
213
218
  - - ! '>='
214
219
  - !ruby/object:Gem::Version
215
220
  version: '0'
221
+ segments:
222
+ - 0
223
+ hash: -3792838809084270394
216
224
  requirements: []
217
225
  rubyforge_project:
218
- rubygems_version: 1.8.24
226
+ rubygems_version: 1.8.23
219
227
  signing_key:
220
228
  specification_version: 3
221
229
  summary: Midiwre Ruby Library
222
230
  test_files:
223
231
  - spec/lib/midwire_common/array_spec.rb
232
+ - spec/lib/midwire_common/data_file_cache_spec.rb
224
233
  - spec/lib/midwire_common/enumerable_spec.rb
225
234
  - spec/lib/midwire_common/file/stat_spec.rb
226
235
  - spec/lib/midwire_common/fixnum_spec.rb