cacheable_attr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ module CacheableAttr
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,27 @@
1
+ require 'pathname'
2
+
3
+ __DIR__ = Pathname.new(__FILE__).dirname
4
+ $:.unshift(__DIR__.to_s) unless $:.include?(__DIR__.to_s)
5
+
6
+ require 'cacheable_attr/version'
7
+
8
+ module CacheableAttr
9
+
10
+ def cacheable_attr(*attributes)
11
+ opts = { ttl: 60 }
12
+ opts = opts.merge(attributes.pop) if attributes.last.is_a?(Hash)
13
+
14
+ attributes.each do |attribute|
15
+ define_method attribute do
16
+ if !instance_variable_defined?("@#{attribute}_updated_at") || (opts[:ttl] != nil && Time.now >= instance_variable_get("@#{attribute}_updated_at") + opts[:ttl])
17
+ value = send("#{attribute}_uncached")
18
+ instance_variable_set("@#{attribute}", value)
19
+ instance_variable_set("@#{attribute}_updated_at", Time.now)
20
+ end
21
+
22
+ instance_variable_get("@#{attribute}")
23
+ end
24
+ end
25
+ end
26
+
27
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cacheable_attr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Scott Lewis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-25 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: ! '`cacheable_attr` adds cacheable attributes to standard Ruby objects'
15
+ email: c00lryguy@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/cacheable_attr/version.rb
21
+ - lib/cacheable_attr.rb
22
+ homepage: http://github.com/c00lryguy/cacheable_attr
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.10
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Cache your object's veriables with a pre-set TTL
46
+ test_files: []
47
+ has_rdoc: