Class: MagLove::Server

Inherits:
Object
  • Object
show all
Includes:
Workspace
Defined in:
lib/maglove/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Workspace

#gem_dir, #theme_base_dir, #theme_base_file, #theme_config, #theme_dir, #theme_file, #workspace_dir, #workspace_file

Constructor Details

#initialize(options) ⇒ Server

Returns a new instance of Server.



19
20
21
# File 'lib/maglove/server.rb', line 19

def initialize(options)
  @options = options
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



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

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.start(port, theme, templates) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/maglove/server.rb', line 9

def self.start(port, theme, templates)
  app = Rack::Builder.new do
    use MagLove::Middleware::LiveReload, mount: "/maglove", theme: theme, templates: templates
    use Rack::Static, urls: ["/fonts", "/themes"], root: "dist"
    use Rack::Static, urls: ["/images"], root: "dist/themes/#{theme}"
    run MagLove::Server.new(theme: theme, templates: templates, port: port)
  end
  Rack::Server.start(app: app, Port: port, server: :puma)
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/maglove/server.rb', line 23

def call(env)
  template = env["PATH_INFO"].sub("/", "")
  if env["PATH_INFO"] == "/maglove.js"
    [200, { 'Content-Type' => 'text/javascript' }, [gem_dir.file("maglove.js").read]]
  elsif env["PATH_INFO"] == "/maglove.css"
    [200, { 'Content-Type' => 'text/css' }, [gem_dir.file("maglove.css").read]]
  elsif @options[:templates].include?(template)
    [200, { 'Content-Type' => 'text/html' }, [process(template)]]
  else
    [200, { 'Content-Type' => 'text/html' }, [gem_dir.file("index.haml").read_hamloft(@options)]]
  end
end

#process(template) ⇒ Object



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

def process(template)
  css_contents = theme_dir(root: "dist").file("theme.css").read
  js_contents = theme_dir(root: "dist").file("theme.js").read
  template_file = theme_dir.file("templates/#{template}.haml")
  gem_dir.file("maglove.haml").read_hamloft(theme: @options[:theme], contents: template_file.asset.contents, css_contents: css_contents, js_contents: js_contents, template: template, asset_uri: ".", port: @options[:port] || 3000)
end

#setup(theme) ⇒ Object



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

def setup(theme)
  @widget_stamps = {}
  Dir["widgets/*.rb"].each { |file| @widget_stamps[file] = File.mtime(file).to_i }
  @options[:templates].each do |template|
    mount("/#{template}") do |req, res|
      Dir["widgets/*.rb"].each do |file|
        stamp = File.mtime(file).to_i
        next unless @widget_stamps[file] and @widget_stamps[file] < stamp
        @widget_stamps[file] = stamp
        debug("▸ reloading widget: #{file}")
        load(file)
      end
    end
  end
end