Class: RailsBestPractices::Core::Methods
- Inherits:
-
Object
- Object
- RailsBestPractices::Core::Methods
- Defined in:
- lib/rails_best_practices/core/methods.rb
Overview
Method container.
Instance Method Summary collapse
-
#add_method(class_name, method_name, meta = {}, access_control = 'public') ⇒ Object
Add a method.
-
#get_all_unused_methods(access_control = nil) ⇒ Array
Get all unused methods.
-
#get_method(class_name, method_name, access_control = nil) ⇒ Method
Get a method in a class.
-
#get_methods(class_name, access_control = nil) ⇒ Array
Get methods of a class.
-
#has_method?(class_name, method_name, access_control = nil) ⇒ Boolean
If a class has a method.
-
#initialize ⇒ Methods
constructor
A new instance of Methods.
-
#mark_parent_class_method_used(class_name, method_name) ⇒ Object
Mark parent class’ method as used.
-
#mark_parent_class_methods_publicize(class_name, method_name) ⇒ Object
Mark parent classs’ method as public.
-
#mark_publicize(class_name, method_name) ⇒ Object
Mark the method as public.
-
#mark_subclasses_method_used(class_name, method_name) ⇒ Object
Mark sub classes’ method as used.
-
#possible_public_used(method_name) ⇒ Object
Remomber the method name, the method is probably be used for the class’ public method.
Constructor Details
#initialize ⇒ Methods
Returns a new instance of Methods.
7 8 9 10 |
# File 'lib/rails_best_practices/core/methods.rb', line 7 def initialize @methods = {} @possible_methods = {} end |
Instance Method Details
#add_method(class_name, method_name, meta = {}, access_control = 'public') ⇒ Object
Add a method.
18 19 20 21 22 23 24 25 26 |
# File 'lib/rails_best_practices/core/methods.rb', line 18 def add_method(class_name, method_name, = {}, access_control = 'public') return if class_name == '' return if has_method?(class_name, method_name) methods(class_name) << Method.new(class_name, method_name, access_control, ) if access_control == 'public' @possible_methods[method_name] = false end end |
#get_all_unused_methods(access_control = nil) ⇒ Array
Get all unused methods.
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/rails_best_practices/core/methods.rb', line 130 def get_all_unused_methods(access_control = nil) @methods.inject([]) do |unused_methods, (_class_name, methods)| unused_methods += if access_control methods.select { |method| method.access_control == access_control && !method.used } else methods.reject(&:used) end end.reject { |method| method.access_control == 'public' && @possible_methods[method.method_name] } end |
#get_method(class_name, method_name, access_control = nil) ⇒ Method
Get a method in a class.
116 117 118 119 120 121 122 123 124 |
# File 'lib/rails_best_practices/core/methods.rb', line 116 def get_method(class_name, method_name, access_control = nil) if access_control methods(class_name).find do |method| method.method_name == method_name && method.access_control == access_control end else methods(class_name).find { |method| method.method_name == method_name } end end |
#get_methods(class_name, access_control = nil) ⇒ Array
Get methods of a class.
33 34 35 36 37 38 39 |
# File 'lib/rails_best_practices/core/methods.rb', line 33 def get_methods(class_name, access_control = nil) if access_control methods(class_name).select { |method| method.access_control == access_control } else methods(class_name) end end |
#has_method?(class_name, method_name, access_control = nil) ⇒ Boolean
If a class has a method.
47 48 49 50 51 52 53 54 55 |
# File 'lib/rails_best_practices/core/methods.rb', line 47 def has_method?(class_name, method_name, access_control = nil) if access_control !!methods(class_name).find do |method| method.method_name == method_name && method.access_control == access_control end else !!methods(class_name).find { |method| method.method_name == method_name } end end |
#mark_parent_class_method_used(class_name, method_name) ⇒ Object
Mark parent class’ method as used.
61 62 63 64 65 66 67 68 |
# File 'lib/rails_best_practices/core/methods.rb', line 61 def mark_parent_class_method_used(class_name, method_name) klass = Prepares.klasses.find { |klass| klass.to_s == class_name } if klass&.extend_class_name mark_parent_class_method_used(klass.extend_class_name, method_name) method = get_method(klass.extend_class_name, method_name) method&.mark_used end end |
#mark_parent_class_methods_publicize(class_name, method_name) ⇒ Object
Mark parent classs’ method as public.
95 96 97 98 99 100 101 |
# File 'lib/rails_best_practices/core/methods.rb', line 95 def mark_parent_class_methods_publicize(class_name, method_name) klass = Prepares.klasses.find { |klass| klass.to_s == class_name } if klass&.extend_class_name mark_parent_class_methods_publicize(klass.extend_class_name, method_name) mark_publicize(class_name, method_name) end end |
#mark_publicize(class_name, method_name) ⇒ Object
Mark the method as public.
86 87 88 89 |
# File 'lib/rails_best_practices/core/methods.rb', line 86 def mark_publicize(class_name, method_name) method = get_method(class_name, method_name) method&.publicize end |
#mark_subclasses_method_used(class_name, method_name) ⇒ Object
Mark sub classes’ method as used.
74 75 76 77 78 79 80 |
# File 'lib/rails_best_practices/core/methods.rb', line 74 def mark_subclasses_method_used(class_name, method_name) Prepares.klasses.select { |klass| klass.extend_class_name == class_name }.each do |klass| mark_subclasses_method_used(klass.to_s, method_name) method = get_method(klass.to_s, method_name) method&.mark_used end end |
#possible_public_used(method_name) ⇒ Object
Remomber the method name, the method is probably be used for the class’ public method.
106 107 108 |
# File 'lib/rails_best_practices/core/methods.rb', line 106 def possible_public_used(method_name) @possible_methods[method_name] = true end |