Class: ConvenientService::Utils::Method::Defined

Inherits:
Support::Command show all
Defined in:
lib/convenient_service/utils/method/defined.rb

Constant Summary collapse

METHOD_DEFINED_CHECKERS =
{
  public: ->(klass, method) { klass.public_method_defined?(method) },
  protected: ->(klass, method) { klass.protected_method_defined?(method) },
  private: ->(klass, method) { klass.private_method_defined?(method) }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Support::Command

[], call

Constructor Details

#initialize(method, klass, public: true, protected: true, private: false) ⇒ void

Parameters:

  • method (Symbol, String)
  • klass (Class)
  • public (Boolean) (defaults to: true)
  • protected (Boolean) (defaults to: true)
  • private (Boolean) (defaults to: false)


60
61
62
63
64
65
66
# File 'lib/convenient_service/utils/method/defined.rb', line 60

def initialize(method, klass, public: true, protected: true, private: false)
  @method = method.to_s
  @klass = klass
  @public = public
  @protected = protected
  @private = private
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



28
29
30
# File 'lib/convenient_service/utils/method/defined.rb', line 28

def klass
  @klass
end

#methodObject (readonly)

Returns the value of attribute method.



22
23
24
# File 'lib/convenient_service/utils/method/defined.rb', line 22

def method
  @method
end

#privateObject (readonly)

Returns the value of attribute private.



46
47
48
# File 'lib/convenient_service/utils/method/defined.rb', line 46

def private
  @private
end

#protectedObject (readonly)

Returns the value of attribute protected.



40
41
42
# File 'lib/convenient_service/utils/method/defined.rb', line 40

def protected
  @protected
end

#publicObject (readonly)

Returns the value of attribute public.



34
35
36
# File 'lib/convenient_service/utils/method/defined.rb', line 34

def public
  @public
end

Instance Method Details

#callvoid

This method returns an undefined value.



71
72
73
74
75
# File 'lib/convenient_service/utils/method/defined.rb', line 71

def call
  return false if selected_visibilities.none?

  selected_visibilities.any? { |visibility| method_defined?(visibility) }
end

#method_defined?(visibility) ⇒ Boolean

Parameters:

  • visibility (Symbol)

Returns:

  • (Boolean)


92
93
94
# File 'lib/convenient_service/utils/method/defined.rb', line 92

def method_defined?(visibility)
  METHOD_DEFINED_CHECKERS[visibility].call(klass, method)
end

#selected_visibilitiesArray<Symbol>

Returns:



84
85
86
# File 'lib/convenient_service/utils/method/defined.rb', line 84

def selected_visibilities
  @selected_visibilities ||= {public: public, protected: protected, private: private}.keep_if { |_, should_be_checked| should_be_checked }.keys
end