perry 0.5.5 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -27,6 +27,11 @@ require 'ostruct'
27
27
  require 'perry/core_ext/kernel/singleton_class'
28
28
 
29
29
  module Perry
30
+
31
+ autoload :Middlewares, 'perry/middlewares'
32
+ autoload :Processors, 'perry/processors'
33
+ autoload :Caching, 'perry/caching'
34
+
30
35
  @@log_file = nil
31
36
 
32
37
  def self.logger
@@ -6,8 +6,6 @@ require 'perry/serialization'
6
6
  require 'perry/relation'
7
7
  require 'perry/scopes'
8
8
  require 'perry/adapters'
9
- require 'perry/middlewares'
10
- require 'perry/processors'
11
9
 
12
10
  class Perry::Base
13
11
  include Perry::Associations::Contains
@@ -0,0 +1,87 @@
1
+
2
+ class Perry::Caching
3
+
4
+ ##
5
+ # Resets all registered caching services. These can be middlewares, processors, adapters, or
6
+ # anything that needs to be called upon a cache reset.
7
+ #
8
+ def self.reset
9
+ registry.each { |method| method.call }
10
+ end
11
+
12
+ ##
13
+ # Register a method to be reset
14
+ #
15
+ def self.register(method=nil, &block)
16
+ registry
17
+ @registry << (method || block)
18
+ end
19
+
20
+ def self.registry
21
+ @registry ||= []
22
+ end
23
+
24
+ ##
25
+ # Return a list of registered methods
26
+ #
27
+ def self.registered
28
+ @registry || []
29
+ end
30
+
31
+ ##
32
+ # Enable caching flag
33
+ #
34
+ def self.enable
35
+ @enabled = true
36
+ end
37
+
38
+ ##
39
+ # Disable caching flag
40
+ #
41
+ def self.disable
42
+ @enabled = false
43
+ end
44
+
45
+ ##
46
+ # Return enabled flag status
47
+ #
48
+ def self.enabled?
49
+ @enabled
50
+ end
51
+
52
+ ##
53
+ # Accept a block to use caching in -- cache will be reset after the block and the original state
54
+ # of the enabled flag will be set after the block exits
55
+ #
56
+ def self.use(&block)
57
+ caching_block(true, &block)
58
+ end
59
+
60
+ ##
61
+ # Accept a block to NOT cache in -- caching will be disabled for this block and then returned to
62
+ # the previous status.
63
+ #
64
+ def self.forgo(&block)
65
+ caching_block(false, &block)
66
+ end
67
+
68
+ def self.clean_registry
69
+ @registry = []
70
+ @enabled = nil
71
+ end
72
+
73
+ protected
74
+
75
+ def self.caching_block(status)
76
+ initial_status = @enabled
77
+ begin
78
+ @enabled = status
79
+ yield
80
+ ensure
81
+ reset if status
82
+ @enabled = initial_status
83
+ end
84
+ end
85
+
86
+ end
87
+
@@ -12,12 +12,20 @@ class Perry::Middlewares::CacheRecords
12
12
  # TRP: Default to a 5 minute cache
13
13
  DEFAULT_LONGEVITY = 5*60
14
14
 
15
+ def self.global_cache
16
+ @@global_cache ||= {}
17
+ end
18
+
19
+ def self.reset_global_cache
20
+ @@global_cache = {}
21
+ end
22
+
15
23
  def reset_cache_store(default_longevity=DEFAULT_LONGEVITY)
16
- @cache_store = Perry::Middlewares::CacheRecords::Store.new(default_longevity)
24
+ self.class.global_cache[self] = Perry::Middlewares::CacheRecords::Store.new(default_longevity)
17
25
  end
18
26
 
19
27
  def cache_store
20
- @cache_store || reset_cache_store
28
+ self.class.global_cache[self] || reset_cache_store
21
29
  end
22
30
 
23
31
  def initialize(adapter, config={})
@@ -26,7 +34,7 @@ class Perry::Middlewares::CacheRecords
26
34
  end
27
35
 
28
36
  def call(options)
29
- if options[:relation]
37
+ if options[:relation] && Perry::Caching.enabled?
30
38
  call_with_cache(options)
31
39
  else
32
40
  @adapter.call(options)
@@ -67,3 +75,7 @@ class Perry::Middlewares::CacheRecords
67
75
  !options[:noop]
68
76
  end
69
77
  end
78
+
79
+ # Register the cache clearing method with the Caching class
80
+ Perry::Caching.register(Perry::Middlewares::CacheRecords.method(:reset_global_cache))
81
+
@@ -2,8 +2,8 @@ module Perry
2
2
  module Version
3
3
 
4
4
  MAJOR = 0
5
- MINOR = 5
6
- TINY = 5
5
+ MINOR = 6
6
+ TINY = 0
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: perry
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 5
9
- - 5
10
- version: 0.5.5
8
+ - 6
9
+ - 0
10
+ version: 0.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Travis Petticrew
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-18 00:00:00 -05:00
18
+ date: 2011-05-25 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -148,6 +148,7 @@ files:
148
148
  - lib/perry/associations/contains.rb
149
149
  - lib/perry/associations/external.rb
150
150
  - lib/perry/base.rb
151
+ - lib/perry/caching.rb
151
152
  - lib/perry/core_ext/kernel/singleton_class.rb
152
153
  - lib/perry/errors.rb
153
154
  - lib/perry/logger.rb