Class: Volt::ComponentHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/volt/server/component_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(component_paths) ⇒ ComponentHandler

Returns a new instance of ComponentHandler.



7
8
9
# File 'lib/volt/server/component_handler.rb', line 7

def initialize(component_paths)
  @component_paths = component_paths
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/volt/server/component_handler.rb', line 11

def call(env)
  req            = Rack::Request.new(env)

  # TODO: Sanatize template path
  component_name = req.path.strip.gsub(/^\/components\//, '').gsub(/[.]js$/, '')

  javascript_code = compile_for_component(component_name)

  [200, { 'Content-Type' => 'application/javascript; charset=utf-8' }, StringIO.new(javascript_code)]
end

#compile_for_component(component_name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/volt/server/component_handler.rb', line 22

def compile_for_component(component_name)
  code = ComponentCode.new(component_name, @component_paths).code

  # Add the lib directory to the load path
  Opal.append_path(Volt.root + '/lib')

  # Compile the code
  javascript_code = Opal.compile(code)

  javascript_code
end