Module: ActiveRemote::Cached::ClassMethods

Defined in:
lib/active_remote/cached.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/active_remote/cached.rb', line 73

def method_missing(m, *args, &block)
  method_name = _method_missing_name(m)

  if method_name.nil? || !cached_methods.include?(method_name.to_s)
    super(m, *args, &block)
  else
    new_args = _args_in_sorted_order(m, args)
    __send__(method_name, *new_args, &block)
  end
end

Instance Method Details

#_args_in_sorted_order(m, args) ⇒ Object

rubocop:disable Metrics/AbcSize



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/active_remote/cached.rb', line 107

def _args_in_sorted_order(m, args)
  regex = /cached_(?:delete|exist_search|search|exist_find|find)_by_([0-9a-zA-Z_]*)(!|\?)?/

  method_name = _method_missing_name(m)

  match_1 = m.match(regex)
  match_2 = method_name.match(regex)

  args_in_order = []

  if match_1[1] && match_2[1]
    orignal_args_name = match_1[1].split('_and_')
    args_names_in_order = match_2[1].split('_and_')

    args_names_in_order.each do |arg_name|
      index = orignal_args_name.index(arg_name)
      args_in_order << args[index]
    end

    if args.size > args_in_order.size
      # Add options if passed
      args_in_order << args.last
    end

    args_in_order
  else
    args
  end
end

#_cached_delete_method_name(arguments) ⇒ Object

rubocop:enable Metrics/AbcSize



175
176
177
# File 'lib/active_remote/cached.rb', line 175

def _cached_delete_method_name(arguments)
  "cached_delete_by_#{arguments.sort.join('_and_')}"
end

#_cached_exist_find_method_name(arguments) ⇒ Object



179
180
181
# File 'lib/active_remote/cached.rb', line 179

def _cached_exist_find_method_name(arguments)
  "cached_exist_find_by_#{arguments.sort.join('_and_')}"
end

#_cached_exist_search_method_name(arguments) ⇒ Object



183
184
185
# File 'lib/active_remote/cached.rb', line 183

def _cached_exist_search_method_name(arguments)
  "cached_exist_search_by_#{arguments.sort.join('_and_')}"
end

#_cached_find_method_name(arguments) ⇒ Object



187
188
189
# File 'lib/active_remote/cached.rb', line 187

def _cached_find_method_name(arguments)
  "cached_find_by_#{arguments.sort.join('_and_')}"
end

#_cached_search_method_name(arguments) ⇒ Object



191
192
193
# File 'lib/active_remote/cached.rb', line 191

def _cached_search_method_name(arguments)
  "cached_search_by_#{arguments.sort.join('_and_')}"
end

#_create_cached_finder_for(cached_finder_key, options = {}) ⇒ Object

rubocop:disable Metrics/AbcSize



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/active_remote/cached.rb', line 139

def _create_cached_finder_for(cached_finder_key, options = {})
  cached_finder_key_set = [cached_finder_key].flatten.sort

  delete_method_name = _cached_delete_method_name(cached_finder_key_set)
  exist_find_method_name = _cached_exist_find_method_name(cached_finder_key_set)
  exist_search_method_name = _cached_exist_search_method_name(cached_finder_key_set)
  find_method_name = _cached_find_method_name(cached_finder_key_set)
  search_method_name = _cached_search_method_name(cached_finder_key_set)
  search_bang_method_name = "#{search_method_name}!"

  unless cached_methods.include?(delete_method_name)
    _define_cached_delete_method(delete_method_name, cached_finder_key_set, options)
  end

  unless cached_methods.include?(exist_find_method_name)
    _define_cached_exist_find_method(exist_find_method_name, cached_finder_key_set, options)
  end

  unless cached_methods.include?(exist_search_method_name)
    _define_cached_exist_search_method(exist_search_method_name, cached_finder_key_set, options)
  end

  unless cached_methods.include?(find_method_name)
    _define_cached_find_method(find_method_name, cached_finder_key_set, options)
  end

  unless cached_methods.include?(search_bang_method_name)
    _define_cached_search_bang_method(search_bang_method_name, cached_finder_key_set, options)
  end

  return if cached_methods.include?(search_method_name)

  _define_cached_search_method(search_method_name, cached_finder_key_set, options)
end

#_define_cached_delete_method(method_name, *method_arguments, cached_finder_options) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/active_remote/cached.rb', line 195

def _define_cached_delete_method(method_name, *method_arguments, cached_finder_options)
  method_arguments.flatten!
  expanded_method_args = method_arguments.join(',')
  sorted_method_args = method_arguments.sort.join(',')
  cached_methods << method_name

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    # def self.cached_delete_by_user_guid(user_guid, options = {})
    #   ::ActiveRemote::Cached.cache.delete([name, user_guid])
    # end
    def self.#{method_name}(#{expanded_method_args}, __active_remote_cached_options = {})
      __active_remote_cached_options = ::ActiveRemote::Cached.default_options.merge(#{cached_finder_options}).merge(__active_remote_cached_options)
      namespace = __active_remote_cached_options.delete(:namespace)
      find_cache_key = [
        RUBY_AND_ACTIVE_SUPPORT_VERSION,
        namespace,
        name,
        "#find",
        ::ActiveRemote::Cached::ArgumentKeys.new(#{sorted_method_args}, __active_remote_cached_options).cache_key
      ].compact

      search_cache_key = [
        RUBY_AND_ACTIVE_SUPPORT_VERSION,
        namespace,
        name,
        "#search",
        ::ActiveRemote::Cached::ArgumentKeys.new(#{sorted_method_args}, __active_remote_cached_options).cache_key
      ].compact

      ::ActiveRemote::Cached.cache.delete(find_cache_key)
      ::ActiveRemote::Cached.cache.delete(search_cache_key)
    end
  RUBY
end

#_define_cached_exist_find_method(method_name, *method_arguments, cached_finder_options) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/active_remote/cached.rb', line 230

def _define_cached_exist_find_method(method_name, *method_arguments, cached_finder_options)
  method_arguments.flatten!
  expanded_method_args = method_arguments.join(',')
  sorted_method_args = method_arguments.sort.join(',')
  cached_methods << method_name
  cached_methods << "#{method_name}?"

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    # def self.cached_exist_find_by_user_guid(user_guid, options = {})
    #   ::ActiveRemote::Cached.cache.exist?([name, user_guid])
    # end
    def self.#{method_name}(#{expanded_method_args}, __active_remote_cached_options = {})
      __active_remote_cached_options = ::ActiveRemote::Cached.default_options.merge(#{cached_finder_options}).merge(__active_remote_cached_options)
      namespace = __active_remote_cached_options.delete(:namespace)
      cache_key = [
        RUBY_AND_ACTIVE_SUPPORT_VERSION,
        namespace,
        name,
        "#find",
        ::ActiveRemote::Cached::ArgumentKeys.new(#{sorted_method_args}, __active_remote_cached_options).cache_key
      ].compact

      ::ActiveRemote::Cached.cache.exist?(cache_key)
    end
  RUBY

  singleton_class.send(:alias_method, "#{method_name}?", method_name)
end

#_define_cached_exist_search_method(method_name, *method_arguments, cached_finder_options) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/active_remote/cached.rb', line 259

def _define_cached_exist_search_method(method_name, *method_arguments, cached_finder_options)
  method_arguments.flatten!
  expanded_method_args = method_arguments.join(',')
  sorted_method_args = method_arguments.sort.join(',')
  cached_methods << method_name
  cached_methods << "#{method_name}?"

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    # def self.cached_exist_search_by_user_guid(user_guid, options = {})
    #   ::ActiveRemote::Cached.cache.exist?([namespace, name, "#search", user_guid])
    # end
    def self.#{method_name}(#{expanded_method_args}, __active_remote_cached_options = {})
      __active_remote_cached_options = ::ActiveRemote::Cached.default_options.merge(#{cached_finder_options}).merge(__active_remote_cached_options)
      namespace = __active_remote_cached_options.delete(:namespace)
      cache_key = [
        RUBY_AND_ACTIVE_SUPPORT_VERSION,
        namespace,
        name,
        "#search",
        ::ActiveRemote::Cached::ArgumentKeys.new(#{sorted_method_args}, __active_remote_cached_options).cache_key
      ].compact

      ::ActiveRemote::Cached.cache.exist?(cache_key)
    end
  RUBY

  singleton_class.send(:alias_method, "#{method_name}?", method_name)
end

#_define_cached_find_method(method_name, *method_arguments, cached_finder_options) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/active_remote/cached.rb', line 288

def _define_cached_find_method(method_name, *method_arguments, cached_finder_options)
  method_arguments.flatten!
  expanded_method_args = method_arguments.join(',')
  sorted_method_args = method_arguments.sort.join(',')
  cached_methods << method_name

  expanded_search_args = ''
  method_arguments.each do |method_argument|
    expanded_search_args << ":#{method_argument} => #{method_argument},"
  end

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    # def self.cached_find_by_user_guid(user_guid, options = {})
    #   options = ::ActiveRemote::Cached.default_options.merge({}).merge(options)
    #
    #   ::ActiveRemote::Cached.cache.fetch([namespace, name, "#find", user_guid], options) do
    #     self.find(:user_guid => user_guid)
    #   end
    # end
    #
    # If a block is given, it is incumbent on the caller to make sure the expectation
    # of the result object is maintained for requests/responses
    #
    def self.#{method_name}(#{expanded_method_args}, __active_remote_cached_options = {})
      __active_remote_cached_options = ::ActiveRemote::Cached.default_options.merge(#{cached_finder_options}).merge(__active_remote_cached_options)
      namespace = __active_remote_cached_options.delete(:namespace)
      cache_key = [
        RUBY_AND_ACTIVE_SUPPORT_VERSION,
        namespace,
        name,
        "#find",
        ::ActiveRemote::Cached::ArgumentKeys.new(#{sorted_method_args}, __active_remote_cached_options).cache_key
      ].compact

      ::ActiveRemote::Cached.cache.fetch(cache_key, __active_remote_cached_options) do
        if block_given?
          yield
        else
          self.find(#{expanded_search_args})
        end
      end
    end
  RUBY
end

#_define_cached_search_bang_method(method_name, *method_arguments, cached_finder_options) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/active_remote/cached.rb', line 382

def _define_cached_search_bang_method(method_name, *method_arguments, cached_finder_options)
  method_arguments.flatten!
  expanded_method_args = method_arguments.join(',')
  sorted_method_args = method_arguments.sort.join(',')
  cached_methods << method_name

  expanded_search_args = ''
  method_arguments.each do |method_argument|
    expanded_search_args << ":#{method_argument} => #{method_argument},"
  end

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    # def self.cached_search_by_user_guid!(user_guid, options = {})
    #   options = ::ActiveRemote::Cached.default_options.merge({}).merge(options)
    #
    #   ::ActiveRemote::Cached.cache.fetch([namespace, name, "#search", user_guid], options) do
    #     results = []
    #
    #     if block_given?
    #       results = yield
    #     else
    #       results = self.search(:user_guid => user_guid)
    #     end
    #
    #     raise ::ActiveRemote::RemoteRecordNotFound.new(self.class) if results.size <= 0
    #     results
    #   end
    # end
    #
    # If a block is given, it is incumbent on the caller to make sure the expectation
    # of the result object is maintained for requests/responses
    #
    def self.#{method_name}(#{expanded_method_args}, __active_remote_cached_options = {})
      __active_remote_cached_options = ::ActiveRemote::Cached.default_options.merge(#{cached_finder_options}).merge(__active_remote_cached_options)
      namespace = __active_remote_cached_options.delete(:namespace)
      cache_key = [
        RUBY_AND_ACTIVE_SUPPORT_VERSION,
        namespace,
        name,
        "#search",
        ::ActiveRemote::Cached::ArgumentKeys.new(#{sorted_method_args}, __active_remote_cached_options).cache_key
      ].compact

      ::ActiveRemote::Cached.cache.fetch(cache_key, __active_remote_cached_options) do
        results = []

        if block_given?
          results = yield
        else
          results = self.search(#{expanded_search_args})
        end

        raise ::ActiveRemote::RemoteRecordNotFound.new(self.class) if results.first.nil?
        results
      end
    end
  RUBY
end

#_define_cached_search_method(method_name, *method_arguments, cached_finder_options) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/active_remote/cached.rb', line 333

def _define_cached_search_method(method_name, *method_arguments, cached_finder_options)
  method_arguments.flatten!
  expanded_method_args = method_arguments.join(',')
  sorted_method_args = method_arguments.sort.join(',')
  cached_methods << method_name

  expanded_search_args = ''
  method_arguments.each do |method_argument|
    expanded_search_args << ":#{method_argument} => #{method_argument},"
  end

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    # def self.cached_search_by_user_guid(user_guid, options = {})
    #   options = ::ActiveRemote::Cached.default_options.merge({}).merge(options)
    #
    #   ::ActiveRemote::Cached.cache.fetch([namespace, name, "#search", user_guid], options) do
    #     if block_given?
    #       yield
    #     else
    #       self.search(:user_guid => user_guid)
    #     end
    #   end
    # end
    #
    # If a block is given, it is incumbent on the caller to make sure the expectation
    # of the result object is maintained for requests/responses
    #
    def self.#{method_name}(#{expanded_method_args}, __active_remote_cached_options = {})
      __active_remote_cached_options = ::ActiveRemote::Cached.default_options.merge(#{cached_finder_options}).merge(__active_remote_cached_options)
      namespace = __active_remote_cached_options.delete(:namespace)
      cache_key = [
        RUBY_AND_ACTIVE_SUPPORT_VERSION,
        namespace,
        name,
        "#search",
        ::ActiveRemote::Cached::ArgumentKeys.new(#{sorted_method_args}, __active_remote_cached_options).cache_key
      ].compact

      ::ActiveRemote::Cached.cache.fetch(cache_key, __active_remote_cached_options) do
        if block_given?
          yield
        else
          self.search(#{expanded_search_args})
        end
      end
    end
  RUBY
end

#_method_missing_name(m) ⇒ Object

Underscored Methods



97
98
99
100
101
102
103
104
# File 'lib/active_remote/cached.rb', line 97

def _method_missing_name(m)
  regex = /(cached_(?:delete|exist_search|search|exist_find|find)_by_)([0-9a-zA-Z_]*)(!|\?)?/

  return unless m.to_s =~ regex

  params = ::Regexp.last_match(2).split('_and_')
  "#{::Regexp.last_match(1)}#{params.sort.join('_and_')}#{::Regexp.last_match(3)}".to_sym
end

#cached_find(argument_hash, options = {}, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/active_remote/cached.rb', line 51

def cached_find(argument_hash, options = {}, &block)
  method_name = _cached_find_method_name(argument_hash.keys)
  arguments = argument_hash.keys.sort.map { |k| argument_hash[k] }

  if block_given?
    __send__(method_name, *arguments, options, &block)
  else
    __send__(method_name, *arguments, options)
  end
end

#cached_finders(*keys) ⇒ Object



47
48
49
# File 'lib/active_remote/cached.rb', line 47

def cached_finders(*keys)
  cached_finders_for(*keys)
end

#cached_finders_for(*cached_finder_keys) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/active_remote/cached.rb', line 39

def cached_finders_for(*cached_finder_keys)
  options = cached_finder_keys.extract_options!

  cached_finder_keys.each do |cached_finder_key|
    _create_cached_finder_for(cached_finder_key, options)
  end
end

#cached_methodsObject



34
35
36
37
# File 'lib/active_remote/cached.rb', line 34

def cached_methods
  @cached_methods ||= []
  @cached_methods
end

#cached_search(argument_hash, options = {}, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/active_remote/cached.rb', line 62

def cached_search(argument_hash, options = {}, &block)
  method_name = _cached_search_method_name(argument_hash.keys)
  arguments = argument_hash.keys.sort.map { |k| argument_hash[k] }

  if block_given?
    __send__(method_name, *arguments, options, &block)
  else
    __send__(method_name, *arguments, options)
  end
end

#respond_to_missing?(m, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
# File 'lib/active_remote/cached.rb', line 84

def respond_to_missing?(m, include_private = false)
  method_name = _method_missing_name(m)

  if !method_name.nil? && cached_methods.include?(method_name.to_s)
    true
  else
    super
  end
end