Class: PageBuilder::ScriptManager

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeScriptManager

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

Parameters:

  • uri (String)

    the src of the script

Returns:

  • (Boolean)

    true if script is added, false if uri already existed



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

Parameters:

  • node (Nokogiri::XML::Node)

    to append script tags to



34
35
36
37
38
39
# File 'lib/pagebuilder/script_manager.rb', line 34

def append_tags(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

Parameters:

  • uri (String)

    the uri of the script to remove



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