once 0.0.1 → 0.0.3
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.
- checksums.yaml +4 -4
- data/.ruby-gemset +1 -0
- data/CHANGELOG.md +6 -0
- data/lib/once.rb +9 -4
- data/lib/once/version.rb +1 -1
- data/spec/once_spec.rb +7 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc1dc66aa1467c67d46b20d62737dce0c95dc307
|
4
|
+
data.tar.gz: 18db88e365f371eab21b2bca55348e2db40d1f68
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c3ef2cc6f39da8ee988767b3af06872086ced4f20639d4c356ca7893169e328d73bad675aa3b227ea3993695d8ec90c6351ecd98c65ad695001aab0c5d8e25f
|
7
|
+
data.tar.gz: c79340580c1018db34d175055d48170a3300388e03b84c5d205456da408512fbdcc5213ec2512aa39cb61999e609075d28f553aed2920e52b845527298206031
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
once
|
data/CHANGELOG.md
ADDED
data/lib/once.rb
CHANGED
@@ -13,7 +13,7 @@ require 'digest'
|
|
13
13
|
# .. stuff that should happen only once ..
|
14
14
|
# end
|
15
15
|
#
|
16
|
-
# The combination of the name and params makes the check unique. So typically it would be the
|
16
|
+
# The combination of the name and params makes the check unique. So typically it would be the
|
17
17
|
# command you're executing, plus the params to that command
|
18
18
|
module Once
|
19
19
|
DEFAULT_TIME = 3600 # seconds
|
@@ -37,12 +37,17 @@ module Once
|
|
37
37
|
hash = Digest::MD5.hexdigest(params.inspect)
|
38
38
|
redis_key = "uniquecheck:#{name}:#{hash}"
|
39
39
|
|
40
|
+
perform(redis_key, &block).tap do
|
41
|
+
redis.setex(redis_key, within, true)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def perform(redis_key, &block)
|
40
48
|
unless redis.exists(redis_key)
|
41
49
|
block.call
|
42
50
|
end
|
43
|
-
|
44
|
-
redis.set(redis_key, true)
|
45
|
-
redis.expire(redis_key, within)
|
46
51
|
end
|
47
52
|
end
|
48
53
|
end
|
data/lib/once/version.rb
CHANGED
data/spec/once_spec.rb
CHANGED
@@ -8,7 +8,7 @@ describe Once do
|
|
8
8
|
Once.redis = Redis.new
|
9
9
|
end
|
10
10
|
|
11
|
-
let(:thing_to_execute) { double(execute:
|
11
|
+
let(:thing_to_execute) { double(execute: "foo") }
|
12
12
|
let(:params) {{foo: "bar"}}
|
13
13
|
|
14
14
|
it "executes the given command" do
|
@@ -19,6 +19,12 @@ describe Once do
|
|
19
19
|
thing_to_execute.should have_received(:execute).once
|
20
20
|
end
|
21
21
|
|
22
|
+
it "returns the result of the command" do
|
23
|
+
described_class.do(name: "mycheck", params: params) do
|
24
|
+
thing_to_execute.execute
|
25
|
+
end.should == "foo"
|
26
|
+
end
|
27
|
+
|
22
28
|
it "executes when called from within different namespaces" do
|
23
29
|
described_class.do(name: "mycheck1", params: params) do
|
24
30
|
thing_to_execute.execute
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: once
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yan Pritzker
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -88,7 +88,9 @@ extensions: []
|
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
90
|
- ".gitignore"
|
91
|
+
- ".ruby-gemset"
|
91
92
|
- ".ruby-version"
|
93
|
+
- CHANGELOG.md
|
92
94
|
- Gemfile
|
93
95
|
- LICENSE
|
94
96
|
- README.md
|