Class: GroupedControllers

Inherits:
Object show all
Defined in:
lib/volt/page/bindings/template_binding/grouped_controllers.rb

Overview

Some template bindings share the controller with other template bindings based on a name. This class keeps track of the number of templates using this controller and clears it once no one else is using it. Use #get or #inc to add to the count. #clear removes 1 from the count. When the count is 0, delete the controller.

Constant Summary collapse

@@controllers =
{}

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ GroupedControllers

Returns a new instance of GroupedControllers.



8
9
10
# File 'lib/volt/page/bindings/template_binding/grouped_controllers.rb', line 8

def initialize(name)
  @name = name
end

Instance Method Details

#clearObject



24
25
26
27
28
29
30
# File 'lib/volt/page/bindings/template_binding/grouped_controllers.rb', line 24

def clear
  controller = self.controller
  controller[1] -= 1
  if controller[1] == 0
    @@controllers.delete(@name)
  end
end

#getObject



12
13
14
# File 'lib/volt/page/bindings/template_binding/grouped_controllers.rb', line 12

def get
  return (controller = self.controller) && controller[0]
end

#incObject



20
21
22
# File 'lib/volt/page/bindings/template_binding/grouped_controllers.rb', line 20

def inc
  controller[1] += 1
end

#set(controller) ⇒ Object



16
17
18
# File 'lib/volt/page/bindings/template_binding/grouped_controllers.rb', line 16

def set(controller)
  @@controllers[@name] = [controller, 1]
end