Class: Nephos::Controller
- Inherits:
-
Object
- Object
- Nephos::Controller
- Defined in:
- lib/nephos-server/controller.rb
Overview
This class must be inherited by the other Controllers. It contains a constructor (you should not rewrite it) It contains some helpers too, like an access to the environment, and the parameters.
Direct Known Subclasses
Constant Summary collapse
- @@before_action =
{:'*' => []}
- @@after_action =
{:'*' => []}
Instance Attribute Summary collapse
-
#callpath ⇒ Object
readonly
Returns the value of attribute callpath.
-
#cookies ⇒ Object
readonly
Returns the value of attribute cookies.
-
#extension ⇒ Object
(also: #format)
readonly
Returns the value of attribute extension.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#req ⇒ Object
readonly
Returns the value of attribute req.
Class Method Summary collapse
-
.after_action(method, opt = nil) ⇒ Object
see #self#self.before_action.
- .before_action(method, opt = nil) ⇒ Object
- .parse_action_opts(opt) ⇒ Object
Instance Method Summary collapse
-
#execute_after_action(call) ⇒ Object
see #self#self.execute_before_action.
-
#execute_before_action(call) ⇒ Object
It calls every registred hooks added to the @before_action list, including ‘*’.
- #html? ⇒ Boolean
-
#initialize(req, callpath, extension = nil) ⇒ Controller
constructor
A new instance of Controller.
- #json? ⇒ Boolean
- #log(*args) ⇒ Object
- #plain? ⇒ Boolean
-
#url_for(path = "") ⇒ String
An url formated as “scheme://host:port/path”.
Constructor Details
#initialize(req, callpath, extension = nil) ⇒ Controller
Returns a new instance of Controller.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/nephos-server/controller.rb', line 15 def initialize req, callpath, extension=nil raise ArgumentError, "req must be a Rack::Request" unless req.is_a? Rack::Request raise ArgumentError, "call must be a Hash" unless callpath.is_a? Hash @req= req @callpath= callpath @params= req.params rescue {} params = @req.path.split("/") params.shift @params.merge! Hash[callpath[:params].zip(params)] @params.select!{|k,v| not k.to_s.empty?} @params = Params.new(@params) @cookies = Cookies.new(@req.) @extension = extension.to_s.split(".").last end |
Instance Attribute Details
#callpath ⇒ Object (readonly)
Returns the value of attribute callpath.
9 10 11 |
# File 'lib/nephos-server/controller.rb', line 9 def callpath @callpath end |
#cookies ⇒ Object (readonly)
Returns the value of attribute cookies.
9 10 11 |
# File 'lib/nephos-server/controller.rb', line 9 def @cookies end |
#extension ⇒ Object (readonly) Also known as: format
Returns the value of attribute extension.
9 10 11 |
# File 'lib/nephos-server/controller.rb', line 9 def extension @extension end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
9 10 11 |
# File 'lib/nephos-server/controller.rb', line 9 def params @params end |
#req ⇒ Object (readonly)
Returns the value of attribute req.
9 10 11 |
# File 'lib/nephos-server/controller.rb', line 9 def req @req end |
Class Method Details
.after_action(method, opt = nil) ⇒ Object
see Nephos::Controller#self#self.before_action
80 81 82 83 84 85 |
# File 'lib/nephos-server/controller.rb', line 80 def self.after_action(method, opt=nil) parse_action_opts(opt) do |call| @@after_action[call] ||= [] @@after_action[call] << method.to_sym end end |
.before_action(method, opt = nil) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/nephos-server/controller.rb', line 72 def self.before_action(method, opt=nil) parse_action_opts(opt) do |call| @@before_action[call] ||= [] @@before_action[call] << method.to_sym end end |
.parse_action_opts(opt) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/nephos-server/controller.rb', line 51 def self.parse_action_opts(opt) if opt.nil? if block_given? then yield :'*' else return [:'*'] end elsif opt.is_a? Hash only = Array(opt[:only]) if block_given? then only.each{|e| yield e} else return only end except = opt[:except] raise "No implemented yet (except)" if except # parse :only and :except elsif opt.is_a? String or opt.is_a? Symbol if block_given? then yield opt else return [opt] end else raise ArgumentError, "Invalid opt" end end |
Instance Method Details
#execute_after_action(call) ⇒ Object
see Nephos::Controller#self#self.execute_before_action
100 101 102 103 104 105 106 107 108 |
# File 'lib/nephos-server/controller.rb', line 100 def execute_after_action(call) call = call.to_sym methods = [] methods += Array(@@after_action[call]) methods += @@after_action[:'*'] methods.each do |method| self.send(method) end end |
#execute_before_action(call) ⇒ Object
It calls every registred hooks added to the @before_action list, including ‘*’
89 90 91 92 93 94 95 96 97 |
# File 'lib/nephos-server/controller.rb', line 89 def execute_before_action(call) call = call.to_sym methods = [] methods += Array(@@before_action[call]) methods += @@before_action[:'*'] methods.each do |method| self.send(method) end end |
#html? ⇒ Boolean
33 34 35 |
# File 'lib/nephos-server/controller.rb', line 33 def html? %w(htm html xhtml).include? extension end |
#json? ⇒ Boolean
36 37 38 |
# File 'lib/nephos-server/controller.rb', line 36 def json? %w(json).include? extension end |
#log(*args) ⇒ Object
110 111 112 |
# File 'lib/nephos-server/controller.rb', line 110 def log *args Logger.write(*args) end |
#plain? ⇒ Boolean
39 40 41 |
# File 'lib/nephos-server/controller.rb', line 39 def plain? %w(txt raw).include? extension end |
#url_for(path = "") ⇒ String
Returns an url formated as “scheme://host:port/path”.
44 45 46 |
# File 'lib/nephos-server/controller.rb', line 44 def url_for(path="") (URI(req.env["rack.url_scheme"] + "://" + req.env['HTTP_HOST'] + "/") + path).to_s end |