Class: Volt::IndexFiles

Inherits:
Object show all
Defined in:
lib/volt/server/rack/index_files.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, component_paths, opal_files) ⇒ IndexFiles

Returns a new instance of IndexFiles.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/volt/server/rack/index_files.rb', line 7

def initialize(app, component_paths, opal_files)
  @app             = app
  @component_paths = component_paths
  @opal_files      = opal_files

  @@router ||= Routes.new.define do
    # Find the route file
    home_path  = component_paths.component_paths('main').first
    route_file = File.read("#{home_path}/config/routes.rb")
    eval(route_file)
  end
end

Instance Method Details

#call(env) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/volt/server/rack/index_files.rb', line 28

def call(env)
  if route_match?(env['PATH_INFO'])
    [200, { 'Content-Type' => 'text/html; charset=utf-8' }, [html]]
  else
    @app.call env
  end
end

#css_filesObject



48
49
50
# File 'lib/volt/server/rack/index_files.rb', line 48

def css_files
  AssetFiles.new('main', @component_paths).css_files
end

#htmlObject



36
37
38
39
40
41
# File 'lib/volt/server/rack/index_files.rb', line 36

def html
  index_path = File.expand_path(File.join(Volt.root, 'public/index.html'))
  html       = File.read(index_path)

  ERB.new(html).result(binding)
end

#javascript_filesObject



43
44
45
46
# File 'lib/volt/server/rack/index_files.rb', line 43

def javascript_files
  # TODO: Cache somehow, this is being loaded every time
  AssetFiles.new('main', @component_paths).javascript_files(@opal_files)
end

#route_match?(path) ⇒ Boolean

Returns:



20
21
22
23
24
25
26
# File 'lib/volt/server/rack/index_files.rb', line 20

def route_match?(path)
  params = @@router.url_to_params(path)

  return params if params

  false
end