Class: Spektr::Targets::Controller

Inherits:
Base
  • Object
show all
Defined in:
lib/spektr/targets/controller.rb

Defined Under Namespace

Classes: Action

Instance Attribute Summary collapse

Attributes inherited from Base

#ast, #name, #options, #parent, #path, #processor

Instance Method Summary collapse

Methods inherited from Base

#ast_to_exp, #find, #find_calls, #find_calls_with_block, #find_method, #find_methods, #find_xstr, #node_matches?

Constructor Details

#initialize(path, content) ⇒ Controller

Returns a new instance of Controller.



6
7
8
9
# File 'lib/spektr/targets/controller.rb', line 6

def initialize(path, content)
  super
  find_actions
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



4
5
6
# File 'lib/spektr/targets/controller.rb', line 4

def actions
  @actions
end

Instance Method Details

#concern?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/spektr/targets/controller.rb', line 11

def concern?
  !name.match('Controller')
end

#find_actionsObject



15
16
17
18
19
# File 'lib/spektr/targets/controller.rb', line 15

def find_actions
  @actions = find_methods(ast: @ast, type: :public).map do |ast|
    Action.new(ast, self)
  end
end

#find_in_set(name, set) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/spektr/targets/controller.rb', line 30

def find_in_set(name, set)
  while true
    result = set.find { |c| c.name == name }
    break if result

    split = name.split('::')
    split.shift
    name = split.join('::')
    break if name.blank?
  end
  result
end

#find_parent(controllers) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/spektr/targets/controller.rb', line 21

def find_parent(controllers)
  parent_name = @processor.parent_name
  result = find_in_set(parent_name, controllers)
  result ||= find_in_set(processor.parent_name_with_modules, controllers)
  return nil if result&.name == name

  result
end