Class: RailsBestPractices::Core::Methods

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_best_practices/core/methods.rb

Overview

Method container.

Instance Method Summary collapse

Constructor Details

#initializeMethods

Returns a new instance of Methods.



6
7
8
9
# File 'lib/rails_best_practices/core/methods.rb', line 6

def initialize
  @methods = {}
  @possible_methods = {}
end

Instance Method Details

#add_method(class_name, method_name, meta = {}, access_control = "public") ⇒ Object

Add a method.

Parameters:

  • class (String)

    name

  • method (String)

    name

  • method (Hash)

    meta, file and line, => “app/models/post.rb”, “line” => 5

  • access (String)

    control, public, protected or private



17
18
19
20
21
22
23
24
# File 'lib/rails_best_practices/core/methods.rb', line 17

def add_method(class_name, method_name, meta={}, 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, meta)
  if access_control == "public"
    @possible_methods[method_name] = false
  end
end

#get_all_unused_methods(access_control = nil) ⇒ Array

Get all unused methods.

Parameters:

  • access (String)

    control

Returns:

  • (Array)

    array of Method



124
125
126
127
128
129
130
131
132
# File 'lib/rails_best_practices/core/methods.rb', line 124

def get_all_unused_methods(access_control=nil)
  @methods.inject([]) { |unused_methods, (class_name, methods)|
    unused_methods += if access_control
      methods.select { |method| method.access_control == access_control && !method.used }
    else
      methods.select { |method| !method.used }
    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.

Parameters:

  • class (String)

    name

  • method (String)

    name

  • access (String)

    control

Returns:



112
113
114
115
116
117
118
# File 'lib/rails_best_practices/core/methods.rb', line 112

def get_method(class_name, method_name, access_control=nil)
  if access_control
    methods(class_name).find { |method| method.method_name == method_name && method.access_control == access_control }
  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.

Parameters:

  • class (String)

    name

  • access (String)

    control

Returns:

  • (Array)

    all methods of a class for such access control, if access control is nil, return all public/protected/private methods



31
32
33
34
35
36
37
# File 'lib/rails_best_practices/core/methods.rb', line 31

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.

Parameters:

  • class (String)

    name

  • method (String)

    name

  • access (String)

    control

Returns:

  • (Boolean)

    has a method or not



45
46
47
48
49
50
51
# File 'lib/rails_best_practices/core/methods.rb', line 45

def has_method?(class_name, method_name, access_control=nil)
  if access_control
    !!methods(class_name).find { |method| method.method_name == method_name && method.access_control == access_control }
  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.

Parameters:

  • class (String)

    name

  • method (String)

    name



57
58
59
60
61
62
63
64
# File 'lib/rails_best_practices/core/methods.rb', line 57

def mark_parent_class_method_used(class_name, method_name)
  klass = Prepares.klasses.find { |klass| klass.to_s == class_name }
  if klass && 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 if method
  end
end

#mark_parent_class_methods_publicize(class_name, method_name) ⇒ Object

Mark parent classs’ method as public.

Parameters:

  • class (String)

    name

  • method (String)

    name



91
92
93
94
95
96
97
# File 'lib/rails_best_practices/core/methods.rb', line 91

def mark_parent_class_methods_publicize(class_name, method_name)
  klass = Prepares.klasses.find { |klass| klass.to_s == class_name }
  if klass && 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.

Parameters:

  • class (String)

    name

  • method (String)

    name



82
83
84
85
# File 'lib/rails_best_practices/core/methods.rb', line 82

def mark_publicize(class_name, method_name)
  method = get_method(class_name, method_name)
  method.publicize if method
end

#mark_subclasses_method_used(class_name, method_name) ⇒ Object

Mark sub classes’ method as used.

Parameters:

  • class (String)

    name

  • method (String)

    name



70
71
72
73
74
75
76
# File 'lib/rails_best_practices/core/methods.rb', line 70

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 if method
  end
end

#possible_public_used(method_name) ⇒ Object

Remomber the method name, the method is probably be used for the class’ public method.

Parameters:

  • method (String)

    name



102
103
104
# File 'lib/rails_best_practices/core/methods.rb', line 102

def possible_public_used(method_name)
  @possible_methods[method_name] = true
end