Module: Sparkl::Decoration

Defined in:
lib/sparkl.rb

Constant Summary collapse

ACTIONS =
%i[
  before_action
  after_action
  skip_before_action
  skip_after_action
  prepend_before_action
  prepend_after_action
].to_set.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(other) ⇒ Object



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

def self.extended(other)
  super
  module_container_for_decorator_methods = Module.new
  other.const_set DECORATORS_CONST_NAME, module_container_for_decorator_methods
  other.extend(module_container_for_decorator_methods)

  other.define_singleton_method :extended do |beyond|
    super beyond
    beyond.extend module_container_for_decorator_methods
  end
end

Instance Method Details

#decorator(name, **opts) ⇒ Object Also known as: def_decorator



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sparkl.rb', line 33

def decorator(name, **opts)
  if opts.empty? or opts.any? { |k, _| !ACTIONS.member?(k.to_sym) }
    raise InvalidOptionsError, "Invalid options #{opts}. Allowed: #{ACTIONS}"
  end

  const_get(DECORATORS_CONST_NAME).define_method name do |action_name|
    opts.each do |opt, args|
      public_send opt, *args, only: [action_name]
    end

    action_name
  end
end