Class: Noraneko::NConst

Inherits:
Object
  • Object
show all
Defined in:
lib/noraneko/nconst.rb

Direct Known Subclasses

NClass, NModule

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(qualified_name, path, line) ⇒ NConst

Returns a new instance of NConst.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/noraneko/nconst.rb', line 9

def initialize(qualified_name, path, line)
  @qualified_name = qualified_name
  @namespace = qualified_name.split('::')
  @path = path
  @line = line
  @methods = []
  @included_module_names = []
  @extended_module_names = []
  @registered_callbacks = []
  @called_views = []
  @called_methods = []
  @default_scope = :public
  @default_type = :instance
end

Instance Attribute Details

#extended_module_namesObject

Returns the value of attribute extended_module_names.



5
6
7
# File 'lib/noraneko/nconst.rb', line 5

def extended_module_names
  @extended_module_names
end

#included_module_namesObject

Returns the value of attribute included_module_names.



5
6
7
# File 'lib/noraneko/nconst.rb', line 5

def included_module_names
  @included_module_names
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



7
8
9
# File 'lib/noraneko/nconst.rb', line 7

def namespace
  @namespace
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/noraneko/nconst.rb', line 7

def path
  @path
end

#qualified_nameObject (readonly)

Returns the value of attribute qualified_name.



7
8
9
# File 'lib/noraneko/nconst.rb', line 7

def qualified_name
  @qualified_name
end

#registered_callbacksObject

Returns the value of attribute registered_callbacks.



5
6
7
# File 'lib/noraneko/nconst.rb', line 5

def registered_callbacks
  @registered_callbacks
end

Instance Method Details

#add_cmethod(name, line) ⇒ Object



93
94
95
96
97
# File 'lib/noraneko/nconst.rb', line 93

def add_cmethod(name, line)
  nmethod = NMethod.class_method(self, name, line)
  @methods << nmethod
  nmethod
end

#add_method(name, line) ⇒ Object



87
88
89
90
91
# File 'lib/noraneko/nconst.rb', line 87

def add_method(name, line)
  nmethod = NMethod.new(self, name, line, @default_scope, @default_type)
  @methods << nmethod
  nmethod
end

#all_instance_methodsObject



71
72
73
# File 'lib/noraneko/nconst.rb', line 71

def all_instance_methods
  @methods.select { |method| method.instance_method? }
end

#all_methodsObject



67
68
69
# File 'lib/noraneko/nconst.rb', line 67

def all_methods
  @methods
end

#all_private_methodsObject



75
76
77
# File 'lib/noraneko/nconst.rb', line 75

def all_private_methods
  @methods.select { |method| method.in_private? }
end

#all_public_methodsObject



79
80
81
# File 'lib/noraneko/nconst.rb', line 79

def all_public_methods
  @methods.select { |method| method.in_public? }
end

#all_used_modulesObject



83
84
85
# File 'lib/noraneko/nconst.rb', line 83

def all_used_modules
  @included_module_names + @extended_module_names
end

#called?(target_name) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/noraneko/nconst.rb', line 114

def called?(target_name)
  @called_methods.any? { |name| name == target_name }
end

#called_view(view_name) ⇒ Object



99
100
101
# File 'lib/noraneko/nconst.rb', line 99

def called_view(view_name)
  @called_views << view_name
end

#child_qualified_name(names) ⇒ Object



36
37
38
# File 'lib/noraneko/nconst.rb', line 36

def child_qualified_name(names)
  qualify(@namespace + names)
end

#controller?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/noraneko/nconst.rb', line 63

def controller?
  name.end_with?('Controller')
end

#find_method(method_name) ⇒ Object



49
50
51
52
53
# File 'lib/noraneko/nconst.rb', line 49

def find_method(method_name)
  @methods.find do |method|
    method.name == method_name
  end
end

#locObject



24
25
26
# File 'lib/noraneko/nconst.rb', line 24

def loc
  "#{@path}:#{@line}"
end

#make_method_private(name) ⇒ Object



103
104
105
106
# File 'lib/noraneko/nconst.rb', line 103

def make_method_private(name)
  target = @methods.find { |method| method.name == name }
  target.private!
end

#merge_singleton(other) ⇒ Object



108
109
110
111
112
# File 'lib/noraneko/nconst.rb', line 108

def merge_singleton(other)
  cm = other.all_instance_methods
  cm.each(&:class_method!)
  @methods += cm
end

#method_default_as_class!Object



59
60
61
# File 'lib/noraneko/nconst.rb', line 59

def method_default_as_class!
  @default_type = :class
end

#nameObject



28
29
30
# File 'lib/noraneko/nconst.rb', line 28

def name
  @namespace.last || ''
end

#parent_nameObject



32
33
34
# File 'lib/noraneko/nconst.rb', line 32

def parent_name
  qualify(@namespace[0..-2])
end

#private!Object



55
56
57
# File 'lib/noraneko/nconst.rb', line 55

def private!
  @default_scope = :private
end

#register_csend(called_method_name) ⇒ Object



45
46
47
# File 'lib/noraneko/nconst.rb', line 45

def register_csend(called_method_name)
  @called_methods << called_method_name
end

#register_send(method_name, called_method_name) ⇒ Object



40
41
42
43
# File 'lib/noraneko/nconst.rb', line 40

def register_send(method_name, called_method_name)
  method = find_method(method_name)
  method.called_methods << called_method_name if method
end

#rel_path_from_controllerObject



134
135
136
137
138
# File 'lib/noraneko/nconst.rb', line 134

def rel_path_from_controller
  @path
    .split('/controllers/').drop(1).join('')
    .split('_controller.rb').first + '/'
end

#used?(target_method) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
122
# File 'lib/noraneko/nconst.rb', line 118

def used?(target_method)
  return true if controller? && action_of_this?(target_method)
  return true if registered_callback?(target_method.name)
  all_methods.any? { |method| method.called?(target_method.name) }
end

#used_view?(target_view_name) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
127
128
129
130
131
132
# File 'lib/noraneko/nconst.rb', line 124

def used_view?(target_view_name)
  explicit = @called_views.any? { |name| name == target_view_name }
  return true if explicit
  return false unless target_view_name.start_with?(rel_path_from_controller)
  tokens = target_view_name.split('/')
  return false if tokens.size < 2
  method_name = tokens.last.to_sym
  all_public_methods.any? { |m| m.name == method_name }
end