midwire_common 0.1.1 → 0.1.2
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.
- data/CHANGELOG +6 -0
 - data/Gemfile +1 -1
 - data/Guardfile +1 -1
 - data/README.md +1 -1
 - data/lib/midwire_common/all.rb +1 -0
 - data/lib/midwire_common/data_file_cache.rb +43 -0
 - data/lib/midwire_common/version.rb +1 -1
 - data/spec/lib/midwire_common/data_file_cache_spec.rb +40 -0
 - metadata +12 -3
 
    
        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  
     | 
| 
      
 1 
     | 
    
         
            +
            source "https://rubygems.org"
         
     | 
| 
       2 
2 
     | 
    
         
             
            gemspec
         
     | 
    
        data/Guardfile
    CHANGED
    
    | 
         @@ -1,4 +1,4 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            guard 'rspec', : 
     | 
| 
      
 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
    
    
    
        data/lib/midwire_common/all.rb
    CHANGED
    
    
| 
         @@ -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
         
     | 
| 
         @@ -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. 
     | 
| 
      
 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- 
     | 
| 
      
 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. 
     | 
| 
      
 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
         
     |