Class: Kangaru::Controller

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/kangaru/controller.rb

Constant Summary collapse

SUFFIX =
"Controller".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Controller

Returns a new instance of Controller.



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

def initialize(request)
  @request = request
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



9
10
11
# File 'lib/kangaru/controller.rb', line 9

def request
  @request
end

Class Method Details

.pathObject

Returns the partial path for the controller based on the class name. The first module namespace is removed as this is either Kangaru or the target gem namespace. Used to infer the location of view files.



24
25
26
# File 'lib/kangaru/controller.rb', line 24

def self.path
  name&.delete_suffix(SUFFIX)&.gsub(/^.*?::/, "")&.to_snakecase || raise
end

Instance Method Details

#executeObject



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

def execute
  return unless public_send(request.action)

  renderer_for(request.action.to_s).render(binding)
end

#target_idObject



30
31
32
# File 'lib/kangaru/controller.rb', line 30

def target_id
  request.id
end