Module: MethodExtensions

Included in:
Method
Defined in:
lib/trax/core/ext/method.rb

Constant Summary collapse

STRATEGIES_FOR_SEND_WHEN_METHOD =

method(method_name).parameters returns an array of the parameters it accepts as well as the signature type :req = required, ordinal argument, i.e. def foo(one) :opt = optional ordinal argument, i.e. def foo(one=nil) :keyreq = required keyword argument i.e. def foo(req:) :key = optional keyword argument, i.e. def foo(one:nil) :rest = optional arguments splat, i.e. def foo(*args) :keyrest = optional keyword arguments splat, i.e. def foo(**args)

{
  :accepts_nothing? => :strategy_for_method_without_arguments,
  :accepts_arguments_and_keywords? => :strategy_for_method_with_arguments_and_keywords,
  :accepts_arguments? => :strategy_for_method_with_arguments,
  :accepts_keywords? => :strategy_for_method_with_keywords
}.freeze

Instance Method Summary collapse

Instance Method Details

#accepted_argument_signaturesObject



17
18
19
# File 'lib/trax/core/ext/method.rb', line 17

def accepted_argument_signatures
  @accepted_argument_signatures ||= self.parameters.any? ? self.parameters.map(&:first).uniq : []
end

#accepts_arguments?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/trax/core/ext/method.rb', line 29

def accepts_arguments?
  @accepts_arguments ||= requires_arguments? || accepts_optional_arguments? || accepts_arguments_splat?
end

#accepts_arguments_and_keywords?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/trax/core/ext/method.rb', line 37

def accepts_arguments_and_keywords?
  @accepts_arguments_and_keywords ||= accepts_arguments? && accepts_keywords?
end

#accepts_arguments_splat?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/trax/core/ext/method.rb', line 41

def accepts_arguments_splat?
  @accepts_arguments_splat ||= accepted_argument_signatures.include?(:rest)
end

#accepts_keywords?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/trax/core/ext/method.rb', line 33

def accepts_keywords?
  @accepts_keywords ||= requires_keywords? || accepts_optional_keywords? || accepts_keywords_splat?
end

#accepts_keywords_splat?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/trax/core/ext/method.rb', line 45

def accepts_keywords_splat?
  @accepts_keywords_splat ||= accepted_argument_signatures.include?(:keyrest)
end

#accepts_nothing?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/trax/core/ext/method.rb', line 25

def accepts_nothing?
  !accepts_something?
end

#accepts_optional_arguments?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/trax/core/ext/method.rb', line 49

def accepts_optional_arguments?
  @accepts_optional_arguments ||= accepted_argument_signatures.include?(:opt)
end

#accepts_optional_keywords?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/trax/core/ext/method.rb', line 53

def accepts_optional_keywords?
  @accepts_optional_keywords ||= accepted_argument_signatures.include?(:key)
end

#accepts_something?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/trax/core/ext/method.rb', line 21

def accepts_something?
  @accepts_something ||= arity != 0
end

#execute_call_strategy(*args, **options) ⇒ Object



57
58
59
# File 'lib/trax/core/ext/method.rb', line 57

def execute_call_strategy(*args, **options)
  __send__(strategy_for_call, *args, **options)
end

#requires_arguments?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/trax/core/ext/method.rb', line 61

def requires_arguments?
  @requires_arguments ||= accepted_argument_signatures.include?(:req)
end

#requires_keywords?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/trax/core/ext/method.rb', line 65

def requires_keywords?
  @requires_keywords ||= accepted_argument_signatures.include?(:keyreq)
end

#strategy_for_callObject



85
86
87
88
89
90
# File 'lib/trax/core/ext/method.rb', line 85

def strategy_for_call
  @strategy_for_call ||= begin
    first_matching_question = STRATEGIES_FOR_SEND_WHEN_METHOD.keys.detect{ |k| send(k) }
    STRATEGIES_FOR_SEND_WHEN_METHOD[first_matching_question]
  end
end

#strategy_for_method_with_arguments(*args, **options) ⇒ Object



77
78
79
# File 'lib/trax/core/ext/method.rb', line 77

def strategy_for_method_with_arguments(*args, **options)
  call(*args)
end

#strategy_for_method_with_arguments_and_keywords(*args, **options) ⇒ Object



81
82
83
# File 'lib/trax/core/ext/method.rb', line 81

def strategy_for_method_with_arguments_and_keywords(*args, **options)
  call(*args, **options)
end

#strategy_for_method_with_keywords(*args, **options) ⇒ Object



73
74
75
# File 'lib/trax/core/ext/method.rb', line 73

def strategy_for_method_with_keywords(*args, **options)
  call(**options)
end

#strategy_for_method_without_arguments(*args, **options) ⇒ Object



69
70
71
# File 'lib/trax/core/ext/method.rb', line 69

def strategy_for_method_without_arguments(*args, **options)
  call()
end