Class: Expressr::App

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/expressr/app.rb

Constant Summary collapse

DEFAULTS =
{
  'jsonp callback name' => 'callback',
  'locals' => {},
  'root' => nil,
  'view engine' => 'slim',
  'views' => 'views'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_options = {}) ⇒ App

Returns a new instance of App.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/expressr/app.rb', line 25

def initialize(server_options={})
  @router = Router.new
  @locals = {}
  set_default_root
  @server_options = { app: self }.merge(server_options)
  @server = Noder::HTTP::Server.new(@server_options)
  @router.server = @server
  request_stack = @server.event_stack('request')
  request_stack.replace(Noder::HTTP::Listeners::Request, callback: Expressr::Listeners::Request)
  request_stack.replace(Noder::HTTP::Listeners::Response, callback: Expressr::Listeners::Response)
end

Instance Attribute Details

#routerObject (readonly)

Returns the value of attribute router.



7
8
9
# File 'lib/expressr/app.rb', line 7

def router
  @router
end

#serverObject (readonly)

Returns the value of attribute server.



7
8
9
# File 'lib/expressr/app.rb', line 7

def server
  @server
end

Class Method Details

.settingsObject



20
21
22
# File 'lib/expressr/app.rb', line 20

def settings
  @settings ||= DEFAULTS
end

Instance Method Details

#all(path, &block) ⇒ Object



73
74
75
# File 'lib/expressr/app.rb', line 73

def all(path, &block)
  @router.add_route(block, path: path)
end

#closeObject



91
92
93
# File 'lib/expressr/app.rb', line 91

def close
  @server.close
end

#disable(name) ⇒ Object



53
54
55
# File 'lib/expressr/app.rb', line 53

def disable(name)
  settings[name] = false
end

#disabled?(name) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/expressr/app.rb', line 61

def disabled?(name)
  settings[name] == false
end

#enable(name) ⇒ Object



49
50
51
# File 'lib/expressr/app.rb', line 49

def enable(name)
  settings[name] = true
end

#enabled?(name) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/expressr/app.rb', line 57

def enabled?(name)
  settings[name] == true
end

#engine(value) ⇒ Object



65
66
67
# File 'lib/expressr/app.rb', line 65

def engine(value)
  set('view engine', value)
end

#get(name_or_path, &block) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/expressr/app.rb', line 41

def get(name_or_path, &block)
  if block
    @router.get(name_or_path, &block)
  else
    settings[name_or_path]
  end
end

#listen(port = nil, address = nil, &block) ⇒ Object



85
86
87
88
89
# File 'lib/expressr/app.rb', line 85

def listen(port=nil, address=nil, &block)
  @server_options[:port] = port if port
  @server_options[:address] = address if address
  @server.listen(@server_options[:port], @server_options[:address], {}, &block)
end

#localsObject



77
78
79
# File 'lib/expressr/app.rb', line 77

def locals
  settings['locals']
end

#param(name, &block) ⇒ Object



69
70
71
# File 'lib/expressr/app.rb', line 69

def param(name, &block)
  @router.add_route(block, param: name)
end

#render(view, options = {}, &block) ⇒ Object

Raises:

  • (NotImplementedError)


81
82
83
# File 'lib/expressr/app.rb', line 81

def render(view, options={}, &block)
  raise NotImplementedError
end

#set(name, value) ⇒ Object



37
38
39
# File 'lib/expressr/app.rb', line 37

def set(name, value)
  settings[name] = value
end

#settingsObject



95
96
97
# File 'lib/expressr/app.rb', line 95

def settings
  self.class.settings
end