Class: Sanford::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/sanford/runner.rb,
lib/sanford/runner.rb

Direct Known Subclasses

SanfordRunner, TestRunner

Constant Summary collapse

DEFAULT_STATUS_CODE =
200.freeze
DEFAULT_STATUS_MSG =
nil.freeze
DEFAULT_DATA =
nil.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handler_class, args = nil) ⇒ Runner

Returns a new instance of Runner.



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

def initialize(handler_class, args = nil)
  @status_code, @status_msg, @data = nil, nil, nil

  args ||= {}
  @logger          = args[:logger] || Sanford::NullLogger.new
  @router          = args[:router] || Sanford::Router.new
  @template_source = args[:template_source] || Sanford::NullTemplateSource.new
  @request         = args[:request]
  @params          = args[:params] || {}

  @handler_class = handler_class
  @handler = @handler_class.new(self)
end

Instance Attribute Details

#handlerObject (readonly)

Returns the value of attribute handler.



19
20
21
# File 'lib/sanford/runner.rb', line 19

def handler
  @handler
end

#handler_classObject (readonly)

Returns the value of attribute handler_class.



19
20
21
# File 'lib/sanford/runner.rb', line 19

def handler_class
  @handler_class
end

#loggerObject (readonly)

Returns the value of attribute logger.



20
21
22
# File 'lib/sanford/runner.rb', line 20

def logger
  @logger
end

#paramsObject (readonly)

Returns the value of attribute params.



21
22
23
# File 'lib/sanford/runner.rb', line 21

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



21
22
23
# File 'lib/sanford/runner.rb', line 21

def request
  @request
end

#routerObject (readonly)

Returns the value of attribute router.



20
21
22
# File 'lib/sanford/runner.rb', line 20

def router
  @router
end

#template_sourceObject (readonly)

Returns the value of attribute template_source.



20
21
22
# File 'lib/sanford/runner.rb', line 20

def template_source
  @template_source
end

Instance Method Details

#data(value = nil) ⇒ Object



56
57
58
59
# File 'lib/sanford/runner.rb', line 56

def data(value = nil)
  @data = value if !value.nil?
  @data
end

#halt(*args) ⇒ Object



61
62
63
64
65
# File 'lib/sanford/runner.rb', line 61

def halt(*args)
  self.status(*args)
  self.data((args.pop)[:data]) if args.last.kind_of?(::Hash)
  throw :halt
end

#render(path, locals = nil) ⇒ Object



67
68
69
# File 'lib/sanford/runner.rb', line 67

def render(path, locals = nil)
  self.data(self.template_source.render(path, self.handler, locals || {}))
end

#runObject

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/sanford/runner.rb', line 37

def run
  raise NotImplementedError
end

#status(*args) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/sanford/runner.rb', line 48

def status(*args)
  if !args.empty?
    @status_msg  = (args.pop)[:message] if args.last.kind_of?(::Hash)
    @status_code = args.first           if !args.empty?
  end
  [@status_code, @status_msg]
end

#to_responseObject



41
42
43
44
45
46
# File 'lib/sanford/runner.rb', line 41

def to_response
  Sanford::Protocol::Response.new(
    [@status_code || DEFAULT_STATUS_CODE, @status_msg || DEFAULT_STATUS_MSG],
    @data.nil? ? DEFAULT_DATA : @data
  )
end