Class: Sanford::Runner
- Inherits:
-
Object
- Object
- Sanford::Runner
- Defined in:
- lib/sanford/runner.rb,
lib/sanford/runner.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_STATUS_CODE =
200.freeze
- DEFAULT_STATUS_MSG =
nil.freeze
- DEFAULT_DATA =
nil.freeze
Instance Attribute Summary collapse
-
#handler ⇒ Object
readonly
Returns the value of attribute handler.
-
#handler_class ⇒ Object
readonly
Returns the value of attribute handler_class.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#router ⇒ Object
readonly
Returns the value of attribute router.
-
#template_source ⇒ Object
readonly
Returns the value of attribute template_source.
Instance Method Summary collapse
- #data(value = nil) ⇒ Object
- #halt(*args) ⇒ Object
-
#initialize(handler_class, args = nil) ⇒ Runner
constructor
A new instance of Runner.
- #render(path, locals = nil) ⇒ Object
- #run ⇒ Object
- #status(*args) ⇒ Object
- #to_response ⇒ Object
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
#handler ⇒ Object (readonly)
Returns the value of attribute handler.
19 20 21 |
# File 'lib/sanford/runner.rb', line 19 def handler @handler end |
#handler_class ⇒ Object (readonly)
Returns the value of attribute handler_class.
19 20 21 |
# File 'lib/sanford/runner.rb', line 19 def handler_class @handler_class end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
20 21 22 |
# File 'lib/sanford/runner.rb', line 20 def logger @logger end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
21 22 23 |
# File 'lib/sanford/runner.rb', line 21 def params @params end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
21 22 23 |
# File 'lib/sanford/runner.rb', line 21 def request @request end |
#router ⇒ Object (readonly)
Returns the value of attribute router.
20 21 22 |
# File 'lib/sanford/runner.rb', line 20 def router @router end |
#template_source ⇒ Object (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 |
#run ⇒ Object
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_response ⇒ Object
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 |