Class: PageBuilder::ScriptManager
- Inherits:
-
Object
- Object
- PageBuilder::ScriptManager
- Extended by:
- Forwardable
- Defined in:
- lib/pagebuilder/script_manager.rb
Overview
Used to keep track of added/removed javascript and to generate script tags in the correct order
Instance Method Summary collapse
-
#add_script(uri, **attributes) ⇒ Boolean
Adds a script to be loaded if it doesn’t exist or updates stored tag attributes if it does.
-
#append_tags(node) ⇒ Object
Creates the script tags requested in the correct order.
-
#initialize ⇒ ScriptManager
constructor
A new instance of ScriptManager.
-
#remove_script(uri) ⇒ Object
Removes a script that was previously added so that it won’t be output.
Constructor Details
#initialize ⇒ ScriptManager
Returns a new instance of ScriptManager.
10 11 12 13 |
# File 'lib/pagebuilder/script_manager.rb', line 10 def initialize @index = [] @scripts = {} end |
Instance Method Details
#add_script(uri, **attributes) ⇒ Boolean
Adds a script to be loaded if it doesn’t exist or updates stored tag attributes if it does
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/pagebuilder/script_manager.rb', line 19 def add_script(uri, **attributes) uri_for_index = uri.downcase if attrs = @scripts[uri_for_index] attrs.merge!(attributes) false else attributes[:src] = uri @scripts[uri_for_index] = attributes @index << uri_for_index true end end |
#append_tags(node) ⇒ Object
Creates the script tags requested in the correct order
34 35 36 37 38 39 |
# File 'lib/pagebuilder/script_manager.rb', line 34 def (node) document = node.document @index.each do |i| node << Elements::Basic.new('script', document).configure(@scripts[i]) end end |
#remove_script(uri) ⇒ Object
Removes a script that was previously added so that it won’t be output
43 44 45 46 47 |
# File 'lib/pagebuilder/script_manager.rb', line 43 def remove_script(uri) uri_for_index = uri.downcase @index.delete(uri_for_index) @scripts.delete(uri_for_index) end |