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)


55
56
57
58
59
60
61
# File 'lib/convenient_service/utils/method/defined.rb', line 55

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.



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

def klass
  @klass
end

#methodObject (readonly)

Returns the value of attribute method.



17
18
19
# File 'lib/convenient_service/utils/method/defined.rb', line 17

def method
  @method
end

#privateObject (readonly)

Returns the value of attribute private.



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

def private
  @private
end

#protectedObject (readonly)

Returns the value of attribute protected.



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

def protected
  @protected
end

#publicObject (readonly)

Returns the value of attribute public.



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

def public
  @public
end

Instance Method Details

#callvoid

This method returns an undefined value.



66
67
68
69
70
# File 'lib/convenient_service/utils/method/defined.rb', line 66

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)


87
88
89
# File 'lib/convenient_service/utils/method/defined.rb', line 87

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

#selected_visibilitiesArray<Symbol>

Returns:



79
80
81
# File 'lib/convenient_service/utils/method/defined.rb', line 79

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