Sha256: 87fc3a326fa292a8b38cb8a8a00887eeb9be6fc372edd0e1da46bd6b5d87bb73

Contents?: true

Size: 1.42 KB

Versions: 20

Compression:

Stored size: 1.42 KB

Contents

require 'volt/server/rack/asset_files'
require 'volt/router/routes'

# Serves the main pages
module Volt
  class IndexFiles
    def initialize(app, component_paths, opal_files)
      @app             = app
      @component_paths = component_paths
      @opal_files      = opal_files

      @@router ||= Routes.new.define do
        # Load routes for each component
        component_paths.components.values.flatten.uniq.each do |component_path|
          routes_path = "#{component_path}/config/routes.rb"

          if File.exists?(routes_path)
            route_file = File.read(routes_path)
            instance_eval(route_file, routes_path, 0)
          end
        end
      end
    end

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

      return params if params

      false
    end

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

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

      ERB.new(html, nil, '-').result(binding)
    end

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

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

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
volt-0.9.1 lib/volt/server/rack/index_files.rb
volt-0.9.1.pre5 lib/volt/server/rack/index_files.rb
volt-0.9.1.pre4 lib/volt/server/rack/index_files.rb
volt-0.9.1.pre3 lib/volt/server/rack/index_files.rb
volt-0.9.1.pre2 lib/volt/server/rack/index_files.rb
volt-0.9.1.pre1 lib/volt/server/rack/index_files.rb
volt-0.9.0 lib/volt/server/rack/index_files.rb
volt-0.9.0.pre7 lib/volt/server/rack/index_files.rb
volt-0.9.0.pre6 lib/volt/server/rack/index_files.rb
volt-0.9.0.pre5 lib/volt/server/rack/index_files.rb
volt-0.9.0.pre4 lib/volt/server/rack/index_files.rb
volt-0.9.0.pre3 lib/volt/server/rack/index_files.rb
volt-0.9.0.pre2 lib/volt/server/rack/index_files.rb
volt-0.9.0.pre1 lib/volt/server/rack/index_files.rb
volt-0.8.27.beta9 lib/volt/server/rack/index_files.rb
volt-0.8.27.beta8 lib/volt/server/rack/index_files.rb
volt-0.8.27.beta7 lib/volt/server/rack/index_files.rb
volt-0.8.27.beta6 lib/volt/server/rack/index_files.rb
volt-0.8.27.beta5 lib/volt/server/rack/index_files.rb
volt-0.8.27.beta4 lib/volt/server/rack/index_files.rb