Class: Opal::Sprockets::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/opal/sprockets/server.rb

Defined Under Namespace

Classes: Index

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Server

Returns a new instance of Server.

Yields:

  • (_self)

Yield Parameters:



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/opal/sprockets/server.rb', line 9

def initialize options = {}
  @use_index   = true
  @public_root = nil
  @public_urls = ['/']
  @sprockets   = options.fetch(:sprockets, ::Sprockets::Environment.new)
  @debug       = options.fetch(:debug, true)
  @prefix      = options.fetch(:prefix, '/assets')

  Opal.paths.each { |p| @sprockets.append_path(p) }

  yield self if block_given?
  create_app
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



6
7
8
# File 'lib/opal/sprockets/server.rb', line 6

def debug
  @debug
end

#index_pathObject

Returns the value of attribute index_path.



6
7
8
# File 'lib/opal/sprockets/server.rb', line 6

def index_path
  @index_path
end

#mainObject

Returns the value of attribute main.



6
7
8
# File 'lib/opal/sprockets/server.rb', line 6

def main
  @main
end

#prefixObject

Returns the value of attribute prefix.



6
7
8
# File 'lib/opal/sprockets/server.rb', line 6

def prefix
  @prefix
end

#public_rootObject

Returns the value of attribute public_root.



6
7
8
# File 'lib/opal/sprockets/server.rb', line 6

def public_root
  @public_root
end

#public_urlsObject

Returns the value of attribute public_urls.



6
7
8
# File 'lib/opal/sprockets/server.rb', line 6

def public_urls
  @public_urls
end

#sprocketsObject

Returns the value of attribute sprockets.



6
7
8
# File 'lib/opal/sprockets/server.rb', line 6

def sprockets
  @sprockets
end

#use_indexObject

Returns the value of attribute use_index.



6
7
8
# File 'lib/opal/sprockets/server.rb', line 6

def use_index
  @use_index
end

Instance Method Details

#append_path(path) ⇒ Object



36
37
38
# File 'lib/opal/sprockets/server.rb', line 36

def append_path path
  @sprockets.append_path path
end

#call(env) ⇒ Object



58
59
60
# File 'lib/opal/sprockets/server.rb', line 58

def call(env)
  @app.call env
end

#create_appObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/opal/sprockets/server.rb', line 44

def create_app
  server, sprockets, prefix = self, @sprockets, self.prefix
  sprockets.logger.level ||= Logger::DEBUG

  @app = Rack::Builder.app do
    not_found = lambda { |env| [404, {}, []] }
    use Rack::Deflater
    use Rack::ShowExceptions
    use Index, server if server.use_index
    map(prefix)      { run sprockets }
    run Rack::Static.new(not_found, root: server.public_root, urls: server.public_urls)
  end
end

#public_dir=(dir) ⇒ Object



23
24
25
26
# File 'lib/opal/sprockets/server.rb', line 23

def public_dir=(dir)
  @public_root = dir
  @public_urls = ["/"]
end

#source_map=(enabled) ⇒ Object



28
29
30
# File 'lib/opal/sprockets/server.rb', line 28

def source_map=(enabled)
  Opal::Config.source_map_enabled = enabled
end

#source_map_enabledObject



32
33
34
# File 'lib/opal/sprockets/server.rb', line 32

def source_map_enabled
  Opal::Config.source_map_enabled
end

#use_gem(gem_name) ⇒ Object



40
41
42
# File 'lib/opal/sprockets/server.rb', line 40

def use_gem gem_name
  @sprockets.use_gem gem_name
end