flagpole_sitta 0.5.1 → 0.5.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,67 +1,71 @@
1
1
  module FlagpoleSittaHelper
2
2
 
3
3
  def update_index_array_cache model, key
4
- model.try(:update_index_array_cache, key)
4
+ model.try(:update_array_cache, key)
5
5
  end
6
6
 
7
7
  def update_show_array_cache model, key, route_id
8
- model.try(:update_show_array_cache, key, route_id)
8
+ model.try(:update_array_cache, key, route_id)
9
9
  end
10
10
 
11
11
  #AR - cache_sitta helper
12
-
13
12
  #NOTE This is not safe for .builder xml files.
14
-
15
- #Options are for cache_sitta
16
-
17
- #key_args, passed to route_id if its a proc or lamdba
18
- #all args must be passed in this manner if proc or lamdba
19
- #so start your proc or lamdba with |options = {}|
20
-
21
- #call_args, passed to call_path if its a proc of lamdba
22
- #all args must be passed in this manner if proc or lamdba
23
- #so start your proc or lamdba with |options = {}|
24
-
25
- #models_in_index
13
+ #Options
14
+ #-------
15
+ #:section
16
+ #The section of the page the cache represents. This is
17
+ #best used in connection with -content_for. Can be any
18
+ #string you want it to be. If not provided will default to
19
+ #body. Also looks for the calls using sections. Will assume calls
20
+ #are in the instance variable '@#{options[:section]_calls'
21
+ #-------
22
+ #:model
23
+ #The model of the object, or objects that you want to link
24
+ #the cache too. Pass the actually model, or an array of models.
25
+ #Must also have a corresponding route_id. If model is an array,
26
+ #route_id must also be an array of equal length. model[i] is
27
+ #connected to route_id[i].
28
+ #-------
29
+ #:route_id
30
+ #The unique identifier of the object, most likely what you route on
31
+ #for showing the object or objects that you want to link
32
+ #the cache too. Pass as a string, or an array of strings.
33
+ #Must also have a corresponding model. If route_id is an array,
34
+ #model must also be an array of equal length. model[i] is
35
+ #connected to route_id[i].
36
+ #-------
37
+ #:models_in_index
26
38
  #Use this if the fragment you are rendering is an index
27
39
  #pass it all the different types of models/classes could be
28
40
  #included in the index. All the include classes must have cache
29
41
  #sitta enabled. The cache for the used index pages will then be
30
42
  #wiped clear when anyone of these models/classes has an object
31
43
  #created or updated.
44
+ #-------
45
+ #:index_only
46
+ #Use this if the cache should not be associated with any object,
47
+ #but rather only a model. Use this if your cache is an index, or
48
+ #can be 'random'.
49
+ #-------
50
+ #:sub_route_id
51
+ #Use this if options on the url can result in a difference in
52
+ #the cache. So if you had an page where you could pass
53
+ #in a year and month would be a great place for this.
54
+ #That way your caching each possible version of the page
55
+ #instead of just one.
56
+ #-------
57
+ #:calls_args
58
+ #Any args you want to pass to your calls. Can only take one argument.
59
+ #The best idea is to pass an option hash.
32
60
  def cache_sitta options={}, &block
33
61
 
34
- #AR - If its a string, then just use that value, other wise it
35
- #assumes that the route_id is a proc or lamdba and call its
36
- #with the provide args.
37
-
38
- if options[:route_id_args]
39
- options[:route_id] = options[:route_id].call(options[:route_id_args])
40
- elsif options[:route_id].class.eql?(Proc)
41
- options[:route_id] = options[:route_id].call()
42
- end
43
-
44
62
  if options[:route_id].class.eql?(Array)
45
63
  main_route_id = options[:route_id][0]
46
64
  else
47
65
  main_route_id = options[:route_id]
48
66
  end
49
67
 
50
-
51
- #Use subroute idea if the view can differ on things like current day, or any type of passed params that can effect how
52
- #the page will look.
53
- if options[:sub_route_id_args]
54
- options[:sub_route_id] = options[:sub_route_id].call(options[:sub_route_id_args])
55
- elsif options[:sub_route_id_args].class.eql?(Proc)
56
- options[:sub_route_id] = options[:sub_route_id].call()
57
- end
58
-
59
68
  if options[:model]
60
- if options[:model_args]
61
- options[:model] = options[:model].call(options[:model_args])
62
- elsif options[:model].class.eql?(Proc)
63
- options[:model] = options[:model].call()
64
- end
65
69
 
66
70
  if options[:model].class.eql?(Array)
67
71
  main_model = options[:model][0]
@@ -115,7 +119,7 @@ module FlagpoleSittaHelper
115
119
  if calls
116
120
  calls.each do |c|
117
121
  if instance_variable_get("@#{c[0]}").nil?
118
- if options[:calls_args]
122
+ if options[:calls_args] && (c.parameters.length > 0)
119
123
  instance_variable_set("@#{c[0]}", c[1].call(options[:calls_args]))
120
124
  else
121
125
  instance_variable_set("@#{c[0]}", c[1].call())
@@ -10,56 +10,71 @@ module FlagpoleSitta
10
10
 
11
11
  module ClassMethods
12
12
 
13
- def initialize_index_array_cache
13
+ def mid_key_gen route_id
14
+ if route_id
15
+ mid_key = "#{route_id}/ShowArray"
16
+ else
17
+ mid_key = "IndexArray"
18
+ end
19
+ end
20
+
21
+ def initialize_array_cache route_id = nil
22
+
23
+
24
+ mid_key = mid_key_gen route_id
14
25
 
15
26
  clazz = self
16
27
 
17
28
  flag = {:space => - 1}
18
29
 
19
- Rails.cache.write("#{clazz}/IndexArray/Flag", flag)
30
+ Rails.cache.write("#{clazz}/#{mid_key}/Flag", flag)
20
31
 
21
32
  flag
22
33
 
23
34
  end
24
35
 
25
- def update_index_array_cache key
36
+ def update_array_cache key, route_id = nil
37
+
38
+ mid_key = mid_key_gen route_id
26
39
 
27
40
  clazz = self
28
41
 
29
- flag = Rails.cache.read("#{clazz}/IndexArray/Flag")
42
+ flag = Rails.cache.read("#{clazz}/#{mid_key}/Flag")
30
43
 
31
44
  #AR - If it doesn't exist start the process of creating it
32
45
  if flag.nil?
33
- flag = initialize_index_array_cache
46
+ flag = initialize_array_cache route_id
34
47
  end
35
48
 
36
49
  #AR - update the array's end point
37
50
  flag[:space] = flag[:space] + 1
38
51
 
39
52
  #AR - write out the new index at the end of the array
40
- Rails.cache.write("#{clazz}/IndexArray/#{flag[:space]}", {:key => key})
53
+ Rails.cache.write("#{clazz}/#{mid_key}/#{flag[:space]}", {:key => key})
41
54
 
42
55
  #AR - update flag in the cache
43
- Rails.cache.write("#{clazz}/IndexArray/Flag", flag)
56
+ Rails.cache.write("#{clazz}/#{mid_key}/Flag", flag)
44
57
 
45
58
  end
46
59
 
47
- def each_index_cache &block
60
+ def each_cache route_id = nil, &block
61
+
62
+ mid_key = mid_key_gen route_id
48
63
 
49
64
  clazz = self
50
65
 
51
- flag = Rails.cache.read("#{clazz}/IndexArray/Flag")
66
+ flag = Rails.cache.read("#{clazz}/#{mid_key}/Flag")
52
67
 
53
68
  #AR - If it doesn't exist start the process of creating it
54
69
  if flag.nil?
55
- flag = initialize_index_array_cache
70
+ flag = initialize_array_cache route_id
56
71
  end
57
72
 
58
73
  #AR - If there aren't any index do nothing.
59
74
  #Else wise loop through every index.
60
75
  #If it actually does exist then yield.
61
76
  for i in 0..flag[:space] do
62
- hash = Rails.cache.read("#{clazz}/IndexArray/#{i}")
77
+ hash = Rails.cache.read("#{clazz}/#{mid_key}/#{i}")
63
78
  if hash
64
79
  yield hash[:key]
65
80
  end
@@ -69,86 +84,17 @@ module FlagpoleSitta
69
84
 
70
85
  end
71
86
 
72
- def destroy_index_array_cache
73
-
74
- clazz = self
75
-
76
- each_index_cache do |key|
77
- Rails.cache.delete(key)
78
- end
79
-
80
- Rails.cache.delete("#{clazz}/IndexArray/Flag")
81
- end
82
-
83
- def initialize_show_array_cache route_id
84
-
85
- clazz = self
86
-
87
- #AR - Its negative one to stop the for loops in the each method if its empty
88
- flag = {:space => - 1}
89
-
90
- Rails.cache.write("#{clazz}/#{route_id}/ShowArray/Flag", flag)
91
-
92
- flag
93
-
94
- end
95
-
96
- def update_show_array_cache key, route_id
97
-
98
- clazz = self
99
-
100
- flag = Rails.cache.read("#{clazz}/#{route_id}/ShowArray/Flag")
101
-
102
- #AR - If it doesn't exist start the process of creating it
103
- if flag.nil?
104
- flag = initialize_show_array_cache(route_id)
105
- end
106
-
107
- #AR - Update the array's end point
108
- flag[:space] = flag[:space] + 1
109
-
110
- #AR - Write out the new index at the end of the array
111
- Rails.cache.write("#{clazz}/#{route_id}/ShowArray/#{flag[:space]}", {:key => key})
112
-
113
- #AR - Update flag in the cache
114
- Rails.cache.write("#{clazz}/#{route_id}/ShowArray/Flag", flag)
115
-
116
- end
117
-
118
- def each_show_cache route_id, &block
87
+ def destroy_array_cache route_id = nil
119
88
 
120
- clazz = self
121
-
122
- flag = Rails.cache.read("#{clazz}/#{route_id}/ShowArray/Flag")
123
-
124
- #AR - If it doesn't exist start the process of creating it
125
- if flag.nil?
126
- flag = initialize_show_array_cache(route_id)
127
- end
128
-
129
- #AR - If there aren't any shows caches do nothing, this happens when space is -1.
130
- #Else wise loop through every caches.
131
- #If it actually does exist then yield.
132
- for i in 0..flag[:space] do
133
- hash = Rails.cache.read("#{clazz}/#{route_id}/ShowArray/#{i}")
134
- if hash
135
- yield hash[:key]
136
- end
137
- end
138
-
139
- nil
140
-
141
- end
142
-
143
- def destroy_show_array_cache route_id
89
+ mid_key = mid_key_gen route_id
144
90
 
145
91
  clazz = self
146
92
 
147
- each_show_cache route_id do |k|
148
- Rails.cache.delete(k)
93
+ each_cache route_id do |key|
94
+ Rails.cache.delete(key)
149
95
  end
150
96
 
151
- Rails.cache.delete("#{clazz}/#{route_id}/ShowArray/Flag")
97
+ Rails.cache.delete("#{clazz}/#{mid_key}/Flag")
152
98
  end
153
99
 
154
100
  end
@@ -167,18 +113,18 @@ module FlagpoleSitta
167
113
  # this is because the new updated object for a sub class, could have also been in a cache for
168
114
  # said sub class, but also in a cache for its super.
169
115
  clazz = original_clazz
170
- while(clazz.respond_to? :destroy_show_array_cache)
116
+ while(clazz.respond_to? :destroy_array_cache)
171
117
 
172
118
  #AR - Clear all caches related to the old route_id
173
- clazz.destroy_show_array_cache(self.try(:send, ("#{clazz.route_id}_was")).to_s)
119
+ clazz.destroy_array_cache(self.try(:send, ("#{clazz.route_id}_was")).to_s)
174
120
  #AR - Clear all caches related to the new route_id just in case
175
- clazz.destroy_show_array_cache(self.try(:send, ("#{clazz.route_id}")).to_s)
121
+ clazz.destroy_array_cache(self.try(:send, ("#{clazz.route_id}")).to_s)
176
122
  #AR - If the new and old are the same All that will happen on the second call is that
177
123
  #it will write the flag out and then destroy it. A very tiny bit of work
178
124
  #for a great amount of extra protection.
179
125
 
180
126
  # AR - Remember to include models_in_index in your helper call in the corresponding index cache.
181
- clazz.destroy_index_array_cache
127
+ clazz.destroy_array_cache
182
128
 
183
129
  clazz = clazz.superclass
184
130
  end
@@ -1,3 +1,3 @@
1
1
  module FlagpoleSitta
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.9"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flagpole_sitta
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: