cached_names 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,2 +1,3 @@
1
1
  require 'cached_names/base'
2
+ require 'cached_names/paranoid'
2
3
  require "rails/cached_names" if defined?(Rails)
@@ -1,5 +1,13 @@
1
1
  module CachedNames
2
2
 
3
+ @@paranoid_loaded ||= false
4
+ def self.paranoid_loaded=(value)
5
+ @@paranoid_loaded = value
6
+ end
7
+ def self.paranoid_loaded
8
+ @@paranoid_loaded
9
+ end
10
+
3
11
  def has_cached_names name_method = "value", options = {}
4
12
  @cached_names_name_method = name_method
5
13
  @cached_names_sort_field = options[:sorted_by] || @cached_names_name_method
@@ -11,27 +19,17 @@ module CachedNames
11
19
  after_destroy { self.class.load_cached_names }
12
20
 
13
21
  load_cached_names
22
+ load_paranoid_cached_names if CachedNames.paranoid_loaded
14
23
  end
15
24
 
16
25
  def names
17
26
  load_cached_names unless @cached_names_loaded
18
27
 
19
28
  begin
20
- Rails.cache.read "#{@cached_names_cache_key_prefix}_names"
21
- rescue
22
- load_cached_names
23
- Rails.cache.read "#{@cached_names_cache_key_prefix}_names"
24
- end
25
- end
26
-
27
- def names_with_deleted
28
- load_cached_names unless @cached_names_loaded
29
-
30
- begin
31
- Rails.cache.read "#{@cached_names_cache_key_prefix}_names_with_deleted"
29
+ Rails.cache.read cache_key
32
30
  rescue
33
31
  load_cached_names
34
- Rails.cache.read "#{@cached_names_cache_key_prefix}_names_with_deleted"
32
+ Rails.cache.read cache_key
35
33
  end
36
34
  end
37
35
 
@@ -39,30 +37,19 @@ module CachedNames
39
37
  load_cached_names unless @cached_names_loaded
40
38
 
41
39
  begin
42
- Rails.cache.read "#{@cached_names_cache_key_prefix}_names_group_#{key}"
43
- rescue
44
- load_cached_names
45
- Rails.cache.read "#{@cached_names_cache_key_prefix}_names_group_#{key}"
46
- end
47
- end
48
-
49
- def names_group_with_deleted key
50
- load_cached_names unless @cached_names_loaded
51
-
52
- begin
53
- Rails.cache.read "#{@cached_names_cache_key_prefix}_names_group_#{key}_with_deleted"
40
+ Rails.cache.read group_cache_key(key)
54
41
  rescue
55
42
  load_cached_names
56
- Rails.cache.read "#{@cached_names_cache_key_prefix}_names_group_#{key}_with_deleted"
43
+ Rails.cache.read group_cache_key(key)
57
44
  end
58
45
  end
59
46
 
60
47
  def load_cached_names
61
48
  begin
62
- @cached_names_instances = all(:with_deleted => true, :order => @cached_names_sort_field)
49
+ @cached_names_instances = all(:order => @cached_names_sort_field)
63
50
 
64
- load_names
65
- load_grouped_names if @cached_names_group_method
51
+ load_names(@cached_names_instances)
52
+ load_grouped_names(@cached_names_instances) if @cached_names_group_method
66
53
 
67
54
  @cached_names_loaded = true
68
55
  rescue Exception => e
@@ -74,20 +61,18 @@ module CachedNames
74
61
 
75
62
  private
76
63
 
77
- def load_names
78
- names = @cached_names_instances.map {|i| [i.deleted?, [i.send(@cached_names_name_method), i.id]] }
79
- all_names, non_deleted_names = partition_by_deleted names
64
+ def load_names(cached_instances, cache_suffix = 'names')
65
+ all_names = cached_instances.map {|i| [i.send(@cached_names_name_method), i.id] }
80
66
 
81
- Rails.cache.write "#{@cached_names_cache_key_prefix}_names_with_deleted", all_names
82
- Rails.cache.write "#{@cached_names_cache_key_prefix}_names", non_deleted_names
67
+ Rails.cache.write cache_key(cache_suffix), all_names
83
68
  end
84
69
 
85
- def load_grouped_names
86
- groups = @cached_names_instances.inject({}) do |groups, instance|
70
+ def load_grouped_names(cached_instances, cache_suffix = 'names_group')
71
+ groups = cached_instances.inject({}) do |groups, instance|
87
72
  value = instance.send(@cached_names_group_method)
88
73
 
89
74
  groups[value] ||= []
90
- groups[value] << [instance.deleted?, [instance.send(@cached_names_name_method), instance.id]]
75
+ groups[value] << [instance.send(@cached_names_name_method), instance.id]
91
76
 
92
77
  groups
93
78
  end
@@ -96,18 +81,18 @@ module CachedNames
96
81
  universal_names = groups[nil] || []
97
82
 
98
83
  groups.each do |(key, names)|
99
- all_names, non_deleted_names = partition_by_deleted(names + universal_names)
84
+ all_names = names + universal_names
100
85
 
101
- Rails.cache.write "#{@cached_names_cache_key_prefix}_names_group_#{key}_with_deleted", all_names
102
- Rails.cache.write "#{@cached_names_cache_key_prefix}_names_group_#{key}", non_deleted_names
86
+ Rails.cache.write group_cache_key(key, cache_suffix), all_names
103
87
  end
104
88
  end
105
89
 
106
- def partition_by_deleted names
107
- all_names = names.map {|(deleted, name_id_pair)| name_id_pair }
108
- non_deleted_names = names.map {|(deleted, name_id_pair)| name_id_pair unless deleted }.compact
90
+ def cache_key(cache_suffix = 'names')
91
+ "#{@cached_names_cache_key_prefix}_#{cache_suffix}"
92
+ end
109
93
 
110
- [all_names, non_deleted_names]
94
+ def group_cache_key(key, cache_suffix = 'names_group')
95
+ "#{@cached_names_cache_key_prefix}_#{key}_#{cache_suffix}"
111
96
  end
112
97
  end
113
98
 
@@ -0,0 +1,41 @@
1
+ module CachedNames
2
+ module ParanoidMethods
3
+
4
+ def names_with_deleted
5
+ load_paranoid_cached_names unless @cached_names_loaded
6
+
7
+ begin
8
+ Rails.cache.read cache_key('names_with_deleted')
9
+ rescue
10
+ load_paranoid_cached_names
11
+ Rails.cache.read cache_key('names_with_deleted')
12
+ end
13
+ end
14
+
15
+ def names_group_with_deleted key
16
+ load_paranoid_cached_names unless @cached_names_loaded
17
+
18
+ begin
19
+ Rails.cache.read group_cache_key(key, 'names_group_with_deleted')
20
+ rescue
21
+ load_paranoid_cached_names
22
+ Rails.cache.read group_cache_key(key, 'names_group_with_deleted')
23
+ end
24
+ end
25
+
26
+ def load_paranoid_cached_names
27
+ begin
28
+ @paranoid_cached_names_instances = with_deleted.all(:order => @cached_names_sort_field)
29
+
30
+ load_names(@paranoid_cached_names_instances, 'names_with_deleted')
31
+ load_grouped_names(@paranoid_cached_names_instances, 'names_group_with_deleted') if @cached_names_group_method
32
+
33
+ @paranoid_cached_names_loaded = true
34
+ rescue Exception => e
35
+ Rails.logger.error e.message
36
+ Rails.logger.error e.backtrace
37
+ @paranoid_cached_names_loaded = false
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,3 +1,3 @@
1
1
  module CachedNames
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -1,7 +1,13 @@
1
1
  module CachedNames
2
2
  class Railtie < Rails::Railtie
3
3
  initializer "cached_names.initialize" do |app|
4
- ActiveRecord::Base.send :extend, CachedNames if defined?(ActiveRecord)
4
+ if defined?(ActiveRecord)
5
+ ActiveRecord::Base.send :extend, CachedNames
6
+ if defined?(ActsAsParanoid)
7
+ ActiveRecord::Base.send :extend, CachedNames::ParanoidMethods
8
+ CachedNames.paranoid_loaded = true
9
+ end
10
+ end
5
11
  end
6
12
  end
7
13
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: cached_names
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.2
5
+ version: 0.1.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - John 'asceth' Long
@@ -59,6 +59,7 @@ files:
59
59
  - cached_names.gemspec
60
60
  - lib/cached_names.rb
61
61
  - lib/cached_names/base.rb
62
+ - lib/cached_names/paranoid.rb
62
63
  - lib/cached_names/version.rb
63
64
  - lib/rails/cached_names.rb
64
65
  - spec/spec_helper.rb