Class: Adornable::Decorators

Inherits:
Object
  • Object
show all
Defined in:
lib/adornable/decorators.rb

Overview

‘Adornable::Decorators` is used as the default namespace for decorator methods when a decorator method that is neither explicitly sourced (via the `decorate from: <receiver>` option) nor implicitly sourced (via the `add_decorators_from <receiver>` macro).

Class Method Summary collapse

Class Method Details

.log(context) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/adornable/decorators.rb', line 11

def self.log(context)
  method_receiver = context.method_receiver
  method_name = context.method_name
  method_args = context.method_arguments
  full_name = Adornable::Utils.formal_method_name(method_receiver, method_name)
  arguments_desc = method_args.empty? ? "no arguments" : "arguments `#{method_args.inspect}`"
  puts "Calling method #{full_name} with #{arguments_desc}"
  yield
end

.memoize(context, for_any_arguments: false, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/adornable/decorators.rb', line 21

def self.memoize(context, for_any_arguments: false, &block)
  return memoize_for_arguments(context, &block) unless for_any_arguments

  method_receiver = context.method_receiver
  method_name = context.method_name
  memo_var_name = :"@adornable_memoized_#{method_receiver.object_id}_#{method_name}"
  existing = instance_variable_get(memo_var_name)
  value = existing.nil? ? yield : existing
  instance_variable_set(memo_var_name, value)
end

.memoize_for_arguments(context) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/adornable/decorators.rb', line 32

def self.memoize_for_arguments(context)
  method_receiver = context.method_receiver
  method_name = context.method_name
  method_args = context.method_arguments
  memo_var_name = :"@adornable_memoized_for_arguments_#{method_receiver.object_id}_#{method_name}"
  memo = instance_variable_get(memo_var_name) || {}
  instance_variable_set(memo_var_name, memo)
  args_key = method_args.inspect
  memo[args_key] = yield if memo[args_key].nil?
  memo[args_key]
end