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



103
104
105
106
107
108
109
110
111
112
# File 'lib/active_remote/cached.rb', line 103

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



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/active_remote/cached.rb', line 136

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

  called_match = m.match(regex)
  sorted_match = _method_missing_name(m).match(regex)

  return args unless called_match[1] && sorted_match[1]

  called_field_names = called_match[1].split('_and_')
  sorted_field_names = sorted_match[1].split('_and_')

  _reorder_args(m, args, called_field_names, sorted_field_names)
end

#_cached_delete_method_name(arguments) ⇒ Object

rubocop:enable Metrics/AbcSize



206
207
208
# File 'lib/active_remote/cached.rb', line 206

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

#_cached_exist_find_method_name(arguments) ⇒ Object



210
211
212
# File 'lib/active_remote/cached.rb', line 210

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

#_cached_exist_search_method_name(arguments) ⇒ Object



214
215
216
# File 'lib/active_remote/cached.rb', line 214

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

#_cached_find_method_name(arguments) ⇒ Object



218
219
220
# File 'lib/active_remote/cached.rb', line 218

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

#_cached_finder_options_for(method_name) ⇒ Object

A subclass inherits the finder methods its parent defined, but not the parent registry, so the lookup walks up the superclass chain.



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/active_remote/cached.rb', line 56

def _cached_finder_options_for(method_name)
  klass = self

  while klass.respond_to?(:cached_finder_options)
    options = klass.cached_finder_options[method_name]
    return options if options

    klass = klass.superclass
  end

  {}
end

#_cached_search_method_name(arguments) ⇒ Object



222
223
224
# File 'lib/active_remote/cached.rb', line 222

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



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/active_remote/cached.rb', line 170

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_hash) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/active_remote/cached.rb', line 242

def _define_cached_delete_method(method_name, *method_arguments, cached_finder_options_hash)
  method_arguments.flatten!
  expanded_method_args = method_arguments.join(',')
  expanded_cache_key_args = _expanded_cache_key_args(method_arguments)

  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_for('#{method_name}')).merge(__active_remote_cached_options)
      namespace = __active_remote_cached_options.delete(:namespace)
      argument_cache_key = #{expanded_cache_key_args}
      find_cache_key = [
        RUBY_AND_ACTIVE_SUPPORT_VERSION,
        namespace,
        name,
        "#find",
        argument_cache_key
      ].compact

      search_cache_key = [
        RUBY_AND_ACTIVE_SUPPORT_VERSION,
        namespace,
        name,
        "#search",
        argument_cache_key
      ].compact

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

  cached_finder_options[method_name] = cached_finder_options_hash
  cached_methods << method_name
end

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



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/active_remote/cached.rb', line 280

def _define_cached_exist_find_method(method_name, *method_arguments, cached_finder_options_hash)
  method_arguments.flatten!
  expanded_method_args = method_arguments.join(',')
  expanded_cache_key_args = _expanded_cache_key_args(method_arguments)

  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_for('#{method_name}')).merge(__active_remote_cached_options)
      namespace = __active_remote_cached_options.delete(:namespace)
      cache_key = [
        RUBY_AND_ACTIVE_SUPPORT_VERSION,
        namespace,
        name,
        "#find",
        #{expanded_cache_key_args}
      ].compact

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

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

  cached_finder_options[method_name] = cached_finder_options_hash
  cached_methods << method_name
  cached_methods << "#{method_name}?"
end

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



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/active_remote/cached.rb', line 311

def _define_cached_exist_search_method(method_name, *method_arguments, cached_finder_options_hash)
  method_arguments.flatten!
  expanded_method_args = method_arguments.join(',')
  expanded_cache_key_args = _expanded_cache_key_args(method_arguments)

  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_for('#{method_name}')).merge(__active_remote_cached_options)
      namespace = __active_remote_cached_options.delete(:namespace)
      cache_key = [
        RUBY_AND_ACTIVE_SUPPORT_VERSION,
        namespace,
        name,
        "#search",
        #{expanded_cache_key_args}
      ].compact

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

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

  cached_finder_options[method_name] = cached_finder_options_hash
  cached_methods << method_name
  cached_methods << "#{method_name}?"
end

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



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
381
382
383
384
# File 'lib/active_remote/cached.rb', line 342

def _define_cached_find_method(method_name, *method_arguments, cached_finder_options_hash)
  method_arguments.flatten!
  expanded_method_args = method_arguments.join(',')
  expanded_cache_key_args = _expanded_cache_key_args(method_arguments)

  expanded_search_args = _expanded_search_args(method_arguments)

  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_for('#{method_name}')).merge(__active_remote_cached_options)
      namespace = __active_remote_cached_options.delete(:namespace)
      cache_key = [
        RUBY_AND_ACTIVE_SUPPORT_VERSION,
        namespace,
        name,
        "#find",
        #{expanded_cache_key_args}
      ].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

  cached_finder_options[method_name] = cached_finder_options_hash
  cached_methods << method_name
end

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



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/active_remote/cached.rb', line 434

def _define_cached_search_bang_method(method_name, *method_arguments, cached_finder_options_hash)
  method_arguments.flatten!
  expanded_method_args = method_arguments.join(',')
  expanded_cache_key_args = _expanded_cache_key_args(method_arguments)

  expanded_search_args = _expanded_search_args(method_arguments)

  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, self if results.nil? || results.first.nil?
    #     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_for('#{method_name}')).merge(__active_remote_cached_options)
      namespace = __active_remote_cached_options.delete(:namespace)
      cache_key = [
        RUBY_AND_ACTIVE_SUPPORT_VERSION,
        namespace,
        name,
        "#search",
        #{expanded_cache_key_args}
      ].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, self if results.nil? || results.first.nil?
        results
      end
    end
  RUBY

  cached_finder_options[method_name] = cached_finder_options_hash
  cached_methods << method_name
end

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



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
# File 'lib/active_remote/cached.rb', line 386

def _define_cached_search_method(method_name, *method_arguments, cached_finder_options_hash)
  method_arguments.flatten!
  expanded_method_args = method_arguments.join(',')
  expanded_cache_key_args = _expanded_cache_key_args(method_arguments)

  expanded_search_args = _expanded_search_args(method_arguments)

  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_for('#{method_name}')).merge(__active_remote_cached_options)
      namespace = __active_remote_cached_options.delete(:namespace)
      cache_key = [
        RUBY_AND_ACTIVE_SUPPORT_VERSION,
        namespace,
        name,
        "#search",
        #{expanded_cache_key_args}
      ].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

  cached_finder_options[method_name] = cached_finder_options_hash
  cached_methods << method_name
end

#_expanded_cache_key_args(method_arguments) ⇒ Object

Returns the source text that builds the cache key for a generated method. The key names each field, so that two finders that take the same values do not share one cache entry.



233
234
235
236
237
238
239
240
# File 'lib/active_remote/cached.rb', line 233

def _expanded_cache_key_args(method_arguments)
  sorted_arguments = method_arguments.sort
  field_names = sorted_arguments.map { |argument| ":#{argument}" }.join(',')

  '::ActiveRemote::Cached::ArgumentKeys.for_fields(' \
    "[#{field_names}], [#{sorted_arguments.join(',')}], __active_remote_cached_options" \
    ').cache_key'
end

#_expanded_search_args(method_arguments) ⇒ Object



226
227
228
# File 'lib/active_remote/cached.rb', line 226

def _expanded_search_args(method_arguments)
  method_arguments.map { |method_argument| ":#{method_argument} => #{method_argument}," }.join
end

#_method_missing_name(m) ⇒ Object

Underscored Methods



127
128
129
130
131
132
133
134
# File 'lib/active_remote/cached.rb', line 127

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

#_reorder_args(m, args, called_field_names, sorted_field_names) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/active_remote/cached.rb', line 150

def _reorder_args(m, args, called_field_names, sorted_field_names)
  if args.size < called_field_names.size
    raise ::ArgumentError,
          "wrong number of arguments to #{m} (given #{args.size}, expected #{called_field_names.size})"
  end

  args_in_order = sorted_field_names.map do |field_name|
    index = called_field_names.index(field_name)
    raise ::ArgumentError, "#{m} has no argument named #{field_name}" if index.nil?

    args[index]
  end

  # Add the options hash if the caller passed one.
  args_in_order << args.last if args.size > called_field_names.size

  args_in_order
end

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



81
82
83
84
85
86
87
88
89
90
# File 'lib/active_remote/cached.rb', line 81

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_finder_optionsObject

The options each cached finder was declared with, keyed by method name. The generated methods read this at call time. Interpolating the options into the generated source instead would require every value to have a literal form, and a value such as 5.minutes does not.



50
51
52
# File 'lib/active_remote/cached.rb', line 50

def cached_finder_options
  @cached_finder_options ||= {}
end

#cached_finders(*keys) ⇒ Object



77
78
79
# File 'lib/active_remote/cached.rb', line 77

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

#cached_finders_for(*cached_finder_keys) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/active_remote/cached.rb', line 69

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



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

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

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



92
93
94
95
96
97
98
99
100
101
# File 'lib/active_remote/cached.rb', line 92

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)


114
115
116
117
118
119
120
121
122
# File 'lib/active_remote/cached.rb', line 114

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