norton 0.0.12 → 0.0.13

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e366ba864730fb31251486d7479e432cda85e9ba
4
- data.tar.gz: cc2c33ec37f5a2ce287546fa341a6cfba5bdd4e8
3
+ metadata.gz: b8032502bd52f644505505f0a4ded443860c9ac1
4
+ data.tar.gz: 6185c37037b78f70f44ea72c8173ac5a70b6c961
5
5
  SHA512:
6
- metadata.gz: 2e3fed01e4b0c1135df0e69d214dbf52fe56b1cd4ac6c88690604502be9db2ce78240170714cda8ffc02047d23ffdcc38451760a692434fc5af952a8d152573c
7
- data.tar.gz: 1111b8b4ffb59c9b2108fe6ea76502241144e624455398d120434656abc38d2da6bb623070a792aa058d1579f2e1be1c617107cb4f588f23183f887d68bdb0ab
6
+ metadata.gz: dae103b58cff40df3761a51af66e74f867c540bbe944ef63a26af39daadbf39c3d4661ecee20393a8448a808ff23428474ef1711fc577c217088844ea744d977
7
+ data.tar.gz: 8d9b896f3d50974bccecc1379c1d7f1e5cf623537ba6772c0533d251dbff96cb80e5b9a95c9fd3974f50b15e979c7055dc864b850144022259288e7082d30284
@@ -0,0 +1,40 @@
1
+ module Norton
2
+ module TimedValue
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+
7
+ def timed_value(name, options={}, &blk)
8
+ if options[:ttl].nil?
9
+ raise 'Time to live not specified.'
10
+ end
11
+
12
+ if blk.nil?
13
+ raise 'Value generation not provided'
14
+ end
15
+
16
+ # Define getter
17
+ define_method(name) do
18
+ Norton.redis.with do |conn|
19
+ v = conn.get("#{self.class.to_s.tableize}:#{self.id}:#{name}")
20
+
21
+ if v.nil?
22
+ v = instance_eval(&blk)
23
+ conn.setex("#{self.class.to_s.tableize}:#{self.id}:#{name}", options[:ttl], v)
24
+ end
25
+
26
+ v
27
+ end
28
+ end
29
+
30
+ define_method("reset_#{name}") do
31
+ Norton.redis.with do |conn|
32
+ v = instance_eval(&blk)
33
+ conn.setex("#{self.class.to_s.tableize}:#{self.id}:#{name}", options[:ttl], v)
34
+ v
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,3 +1,3 @@
1
1
  module Norton
2
- VERSION = "0.0.12"
2
+ VERSION = "0.0.13"
3
3
  end
data/lib/norton.rb CHANGED
@@ -5,6 +5,7 @@ require "active_support/inflector"
5
5
  require "norton/version"
6
6
  require "norton/timestamp"
7
7
  require "norton/counter"
8
+ require "norton/timed_value"
8
9
 
9
10
  module Norton
10
11
  class << self
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: norton
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Larry Zhao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-15 00:00:00.000000000 Z
11
+ date: 2015-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -137,6 +137,7 @@ files:
137
137
  - Rakefile
138
138
  - lib/norton.rb
139
139
  - lib/norton/counter.rb
140
+ - lib/norton/timed_value.rb
140
141
  - lib/norton/timestamp.rb
141
142
  - lib/norton/version.rb
142
143
  - norton.gemspec