Module: Adornable

Defined in:
lib/adornable.rb,
lib/adornable/error.rb,
lib/adornable/utils.rb,
lib/adornable/context.rb,
lib/adornable/version.rb,
lib/adornable/machinery.rb,
lib/adornable/decorators.rb

Overview

Extend the ‘Adornable` module in your class in order to have access to the `decorate` and `add_decorators_from` macros.

Defined Under Namespace

Modules: Error Classes: Context, Decorators, Machinery, Utils

Constant Summary collapse

VERSION =
"1.3.0"

Instance Method Summary collapse

Instance Method Details

#add_decorators_from(receiver) ⇒ Object



29
30
31
# File 'lib/adornable.rb', line 29

def add_decorators_from(receiver)
  adornable_machinery.register_decorator_receiver!(receiver)
end

#adornable_machineryObject



12
13
14
# File 'lib/adornable.rb', line 12

def adornable_machinery
  @adornable_machinery ||= Adornable::Machinery.new
end

#decorate(decorator_name, from: nil, defer_validation: false, **decorator_options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/adornable.rb', line 16

def decorate(decorator_name, from: nil, defer_validation: false, **decorator_options)
  if Adornable::Utils.blank?(decorator_name)
    raise Adornable::Error::InvalidDecoratorArguments, "Decorator name must be provided."
  end

  adornable_machinery.accumulate_decorator!(
    name: decorator_name,
    receiver: from,
    defer_validation: !!defer_validation,
    decorator_options: decorator_options,
  )
end

#method_added(method_name) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/adornable.rb', line 33

def method_added(method_name)
  machinery = adornable_machinery # for local variable
  return unless machinery.accumulated_decorators?

  machinery.apply_accumulated_decorators_to_instance_method!(method_name)
  original_method = instance_method(method_name)

  # NB: If you only supply `*args` to the block, you get kwargs as a trailing
  # Hash member in the `args` array. If you supply both `*args, **kwargs` to
  # the block, kwargs are excluded from the `args` array and only appear in
  # the `kwargs` argument as a Hash.
  define_method(method_name) do |*args, **kwargs|
    bound_method = original_method.bind(self)
    machinery.run_decorated_instance_method(bound_method, *args, **kwargs)
  end

  super
end

#singleton_method_added(method_name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/adornable.rb', line 52

def singleton_method_added(method_name)
  machinery = adornable_machinery # for local variable
  return unless machinery.accumulated_decorators?

  machinery.apply_accumulated_decorators_to_class_method!(method_name)
  original_method = method(method_name)

  # NB: If you only supply `*args` to the block, you get kwargs as a trailing
  # Hash member in the `args` array. If you supply both `*args, **kwargs` to
  # the block, kwargs are excluded from the `args` array and only appear in
  # the `kwargs` argument as a Hash.
  define_singleton_method(method_name) do |*args, **kwargs|
    machinery.run_decorated_class_method(original_method, *args, **kwargs)
  end

  super
end