Class: Opal::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(debug_or_options = {}) {|_self| ... } ⇒ Server

Returns a new instance of Server

Yields:

  • (_self)

Yield Parameters:

  • _self (Opal::Server)

    the object that the method was called on


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/opal/sprockets/server.rb', line 68

def initialize debug_or_options = {}
  unless Hash === debug_or_options
    warn "passing a boolean to control debug is deprecated.\n"+
         "Please pass an Hash instead: Server.new(debug: true)"
    options = {:debug => debug_or_options}
  else
    options = debug_or_options
  end

  @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


65
66
67
# File 'lib/opal/sprockets/server.rb', line 65

def debug
  @debug
end

#index_pathObject

Returns the value of attribute index_path


65
66
67
# File 'lib/opal/sprockets/server.rb', line 65

def index_path
  @index_path
end

#mainObject

Returns the value of attribute main


65
66
67
# File 'lib/opal/sprockets/server.rb', line 65

def main
  @main
end

#prefixObject

Returns the value of attribute prefix


65
66
67
# File 'lib/opal/sprockets/server.rb', line 65

def prefix
  @prefix
end

#public_rootObject

Returns the value of attribute public_root


65
66
67
# File 'lib/opal/sprockets/server.rb', line 65

def public_root
  @public_root
end

#public_urlsObject

Returns the value of attribute public_urls


65
66
67
# File 'lib/opal/sprockets/server.rb', line 65

def public_urls
  @public_urls
end

#sprocketsObject

Returns the value of attribute sprockets


65
66
67
# File 'lib/opal/sprockets/server.rb', line 65

def sprockets
  @sprockets
end

#use_indexObject

Returns the value of attribute use_index


65
66
67
# File 'lib/opal/sprockets/server.rb', line 65

def use_index
  @use_index
end

Instance Method Details

#append_path(path) ⇒ Object


103
104
105
# File 'lib/opal/sprockets/server.rb', line 103

def append_path path
  @sprockets.append_path path
end

#call(env) ⇒ Object


127
128
129
# File 'lib/opal/sprockets/server.rb', line 127

def call(env)
  @app.call env
end

#create_appObject


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/opal/sprockets/server.rb', line 111

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
    assets = []
    assets << SourceMapServer.new(sprockets, prefix) if server.source_map_enabled
    assets << sprockets
    map(prefix) { run Rack::Cascade.new(assets) }
    run Rack::Static.new(not_found, :root => server.public_root, :urls => server.public_urls)
  end
end

#public_dir=(dir) ⇒ Object


90
91
92
93
# File 'lib/opal/sprockets/server.rb', line 90

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

#source_map=(enabled) ⇒ Object


95
96
97
# File 'lib/opal/sprockets/server.rb', line 95

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

#source_map_enabledObject


99
100
101
# File 'lib/opal/sprockets/server.rb', line 99

def source_map_enabled
  Opal::Processor.source_map_enabled
end

#use_gem(gem_name) ⇒ Object


107
108
109
# File 'lib/opal/sprockets/server.rb', line 107

def use_gem gem_name
  @sprockets.use_gem gem_name
end