status_cacher 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rvmrc +46 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +24 -0
- data/README.md +31 -0
- data/Rakefile +2 -0
- data/lib/status_cacher.rb +25 -0
- data/lib/status_storage_error.rb +2 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/status_cacher_spec.rb +90 -0
- data/status_cacher.gemspec +14 -0
- metadata +73 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
|
7
|
+
# Only full ruby name is supported here, for short names use:
|
8
|
+
# echo "rvm use 1.9.3" > .rvmrc
|
9
|
+
environment_id="ruby-1.9.3-p362@status_cacher"
|
10
|
+
|
11
|
+
# Uncomment the following lines if you want to verify rvm version per project
|
12
|
+
# rvmrc_rvm_version="1.20.13 (stable)" # 1.10.1 seams as a safe start
|
13
|
+
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
|
14
|
+
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
|
15
|
+
# return 1
|
16
|
+
# }
|
17
|
+
|
18
|
+
# First we attempt to load the desired environment directly from the environment
|
19
|
+
# file. This is very fast and efficient compared to running through the entire
|
20
|
+
# CLI and selector. If you want feedback on which environment was used then
|
21
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
22
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
|
23
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
24
|
+
then
|
25
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
26
|
+
for __hook in "${rvm_path:-$HOME/.rvm}/hooks/after_use"*
|
27
|
+
do
|
28
|
+
if [[ -f "${__hook}" && -x "${__hook}" && -s "${__hook}" ]]
|
29
|
+
then \. "${__hook}" || true
|
30
|
+
fi
|
31
|
+
done
|
32
|
+
unset __hook
|
33
|
+
if (( ${rvm_use_flag:=1} >= 2 )) # display only when forced
|
34
|
+
then
|
35
|
+
if [[ $- == *i* ]] # check for interactive shells
|
36
|
+
then printf "%b" "Using: \E[32m$GEM_HOME\E[0m" # show the user the ruby and gemset they are using in green
|
37
|
+
else printf "%b" "Using: $GEM_HOME" # don't use colors in non-interactive shells
|
38
|
+
fi
|
39
|
+
fi
|
40
|
+
else
|
41
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
42
|
+
rvm --create "$environment_id" || {
|
43
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
44
|
+
return 1
|
45
|
+
}
|
46
|
+
fi
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
status_cacher (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.2.4)
|
10
|
+
rspec (2.13.0)
|
11
|
+
rspec-core (~> 2.13.0)
|
12
|
+
rspec-expectations (~> 2.13.0)
|
13
|
+
rspec-mocks (~> 2.13.0)
|
14
|
+
rspec-core (2.13.1)
|
15
|
+
rspec-expectations (2.13.0)
|
16
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
17
|
+
rspec-mocks (2.13.1)
|
18
|
+
|
19
|
+
PLATFORMS
|
20
|
+
ruby
|
21
|
+
|
22
|
+
DEPENDENCIES
|
23
|
+
rspec
|
24
|
+
status_cacher!
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# StatusCacher
|
2
|
+
|
3
|
+
This gem provides a simple script that writes a status to a file.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
<code>gem 'status\_cacher'</code>
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
<code>bundle</code>
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
<code>gem install status\_cacher </code>
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
require "status_cacher"
|
22
|
+
|
23
|
+
### Writing Status
|
24
|
+
<pre><code>status_cacher = StatusCacher.new("/tmp/filename")
|
25
|
+
status_cacher.status = ("ok")
|
26
|
+
</code></pre>
|
27
|
+
|
28
|
+
### Reading Status
|
29
|
+
<pre><code>status_cacher = StatusCacher.new("/tmp/filename")
|
30
|
+
status_value = status_cacher.status
|
31
|
+
</code></pre>
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
class StatusCacher
|
2
|
+
attr_accessor :status_path
|
3
|
+
|
4
|
+
def initialize(status_path)
|
5
|
+
raise ArgumentError("No status path specified") if status_path.nil?
|
6
|
+
@status_path = status_path
|
7
|
+
|
8
|
+
FileUtils.touch(status_path)
|
9
|
+
end
|
10
|
+
|
11
|
+
def status=(value)
|
12
|
+
File.open(@status_path, 'w') { |f|
|
13
|
+
f.write value
|
14
|
+
}
|
15
|
+
rescue StandardError => e
|
16
|
+
raise StatusStorageError.new("Could not write to status cache at #{@status_path}: #{e.inspect}")
|
17
|
+
end
|
18
|
+
|
19
|
+
def status
|
20
|
+
f = File.open(@status_path, "r")
|
21
|
+
f.read
|
22
|
+
rescue StandardError => e
|
23
|
+
raise StatusStorageError.new("Status path does not exist when it should #{@status_path}: #{e.inspect}")
|
24
|
+
end
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib' ))
|
5
|
+
|
6
|
+
RSpec.configure do |config|
|
7
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
8
|
+
config.run_all_when_everything_filtered = true
|
9
|
+
config.filter_run :focus
|
10
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
require 'tempfile'
|
4
|
+
require 'fileutils'
|
5
|
+
|
6
|
+
require "status_cacher"
|
7
|
+
require "status_storage_error"
|
8
|
+
|
9
|
+
describe StatusCacher do
|
10
|
+
before :each do
|
11
|
+
@test_text = "test text"
|
12
|
+
@status_file_path = Tempfile.new("status-cacher-temp-").path
|
13
|
+
end
|
14
|
+
|
15
|
+
context "when initialized" do
|
16
|
+
it "should raise an argument error if no status file location is specified" do
|
17
|
+
expect {
|
18
|
+
@iut = StatusCacher.new
|
19
|
+
}.to raise_error ArgumentError
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should remember the status file location specified" do
|
23
|
+
@iut = StatusCacher.new(@status_file_path)
|
24
|
+
@iut.status_path.should == @status_file_path
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "when asked to store a status" do
|
29
|
+
it "should create the status file if not present" do
|
30
|
+
FileUtils.rm_f(@status_file_path)
|
31
|
+
File.exists?(@status_file_path).should == false
|
32
|
+
@iut = StatusCacher.new(@status_file_path)
|
33
|
+
File.exists?(@status_file_path).should == true
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should not overwrite the status file if present" do
|
37
|
+
populate_test_data
|
38
|
+
@iut = StatusCacher.new(@status_file_path)
|
39
|
+
File.read(@status_file_path).should == @test_text
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should store only the value provided in the status file" do
|
43
|
+
populate_test_data
|
44
|
+
test_text = "new value"
|
45
|
+
@iut = StatusCacher.new(@status_file_path)
|
46
|
+
@iut.status = (test_text)
|
47
|
+
File.read(@status_file_path).should == test_text
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should raise a StorageError if the store operation fails" do
|
51
|
+
mock_file = double(File)
|
52
|
+
File.should_receive(:open).and_yield(mock_file)
|
53
|
+
mock_file.should_receive(:write).and_raise RuntimeError
|
54
|
+
@iut = StatusCacher.new(@status_file_path)
|
55
|
+
expect{
|
56
|
+
@iut.status = "blah"
|
57
|
+
}.to raise_error StatusStorageError
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context "when asked to return the current status" do
|
62
|
+
it "should return "" if the status file was not present" do
|
63
|
+
FileUtils.rm_f(@status_file_path)
|
64
|
+
@iut = StatusCacher.new(@status_file_path)
|
65
|
+
value = @iut.status
|
66
|
+
value.should == ""
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should return the value stored in the status file" do
|
70
|
+
populate_test_data
|
71
|
+
@iut = StatusCacher.new(@status_file_path)
|
72
|
+
value = @iut.status
|
73
|
+
value.should == @test_text
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should raise a StorageError if the file exists but could not be accessed" do
|
77
|
+
@iut = StatusCacher.new(@status_file_path)
|
78
|
+
expect{
|
79
|
+
@iut.status_path = "/djksghsfdksfgdklsjhfjks/"
|
80
|
+
@iut.status
|
81
|
+
}.to raise_error StatusStorageError
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def populate_test_data
|
86
|
+
open(@status_file_path, 'a') { |f|
|
87
|
+
f.write @test_text
|
88
|
+
}
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'status_cacher'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.date = '2013-06-25'
|
5
|
+
s.summary = "Writes a status to a file"
|
6
|
+
s.description = ""
|
7
|
+
s.authors = ["Tiaan van Deventer"]
|
8
|
+
s.email = 'tiaan.van.deventer@gmail.com'
|
9
|
+
s.homepage = 'http://rubygems.org/gems/status_cacher'
|
10
|
+
s.files = `git ls-files`.split($\)
|
11
|
+
s.require_paths = ["lib/"]
|
12
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
13
|
+
s.add_development_dependency 'rspec'
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: status_cacher
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tiaan van Deventer
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-06-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
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
|
+
description: ''
|
31
|
+
email: tiaan.van.deventer@gmail.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- .gitignore
|
37
|
+
- .rvmrc
|
38
|
+
- Gemfile
|
39
|
+
- Gemfile.lock
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/status_cacher.rb
|
43
|
+
- lib/status_storage_error.rb
|
44
|
+
- spec/spec_helper.rb
|
45
|
+
- spec/status_cacher_spec.rb
|
46
|
+
- status_cacher.gemspec
|
47
|
+
homepage: http://rubygems.org/gems/status_cacher
|
48
|
+
licenses: []
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib/
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.8.24
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Writes a status to a file
|
71
|
+
test_files:
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
- spec/status_cacher_spec.rb
|