Class: Hanami::Mailer::DSL::PluckyProc Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/mailer/dsl/plucky_proc.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A plucky proc that evaluates with automatic keyword argument extraction from input.

This class encapsulates the logic for calling procs/methods in a mailer context, handling both positional and keyword arguments intelligently based on the proc's signature.

Since:

  • 3.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proc, context:) ⇒ PluckyProc

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new plucky proc

Parameters:

  • proc (Proc, Method, nil)

    the proc or method to evaluate

  • context (Object)

    the object context for instance_exec

Since:

  • 3.0.0



20
21
22
23
# File 'lib/hanami/mailer/dsl/plucky_proc.rb', line 20

def initialize(proc, context:)
  @proc = proc
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 3.0.0



14
15
16
# File 'lib/hanami/mailer/dsl/plucky_proc.rb', line 14

def context
  @context
end

#procObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 3.0.0



14
15
16
# File 'lib/hanami/mailer/dsl/plucky_proc.rb', line 14

def proc
  @proc
end

Class Method Details

.from_name(proc, name, context) ⇒ PluckyProc?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a PluckyProc from either a proc or a method name on the context

Parameters:

  • proc (Proc, Method, nil)

    the proc to use, or nil to look up a method

  • name (Symbol)

    the method name to look up if proc is nil

  • context (Object)

    the context object

Returns:

  • (PluckyProc, nil)

    a new PluckyProc or nil if no proc/method found

Since:

  • 3.0.0



32
33
34
35
36
37
38
39
40
41
# File 'lib/hanami/mailer/dsl/plucky_proc.rb', line 32

def self.from_name(proc, name, context)
  resolved_proc =
    if proc
      proc
    elsif context.respond_to?(name, _include_private = true)
      context.method(name)
    end

  new(resolved_proc, context: context) if resolved_proc
end

Instance Method Details

#call(input, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Evaluate the proc with input and optional positional arguments

Parameters:

  • input (Hash)

    input hash to extract keyword arguments from

  • args (Array)

    positional arguments to pass to the proc

Returns:

  • (Object)

    the result of evaluating the proc

Since:

  • 3.0.0



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/hanami/mailer/dsl/plucky_proc.rb', line 49

def call(input, *args)
  return nil unless proc

  keywords = extract_keywords(input)

  if keywords.empty?
    call_without_keywords(*args)
  else
    call_with_keywords(keywords, *args)
  end
end

#callable?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if this evaluator has a proc to call

Returns:

  • (Boolean)

Since:

  • 3.0.0



64
65
66
# File 'lib/hanami/mailer/dsl/plucky_proc.rb', line 64

def callable?
  !proc.nil?
end

#dependency_namesArray<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get dependency parameter names (positional args: :req, :opt)

Returns:

  • (Array<Symbol>)

    parameter names

Since:

  • 3.0.0



71
72
73
74
75
76
77
78
79
80
# File 'lib/hanami/mailer/dsl/plucky_proc.rb', line 71

def dependency_names
  @dependency_names ||=
    if proc
      proc.parameters.each_with_object([]) { |(type, name), names|
        names << name if %i[req opt].include?(type)
      }
    else
      []
    end
end

#keyword_namesArray<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get keyword parameter names (:key, :keyreq)

Returns:

  • (Array<Symbol>)

    parameter names

Since:

  • 3.0.0



85
86
87
88
89
90
91
92
93
94
# File 'lib/hanami/mailer/dsl/plucky_proc.rb', line 85

def keyword_names
  @keyword_names ||=
    if proc
      proc.parameters.each_with_object([]) { |(type, name), keys|
        keys << name if %i[key keyreq].include?(type)
      }
    else
      []
    end
end