class Swee::Controller

Attributes

action_name[R]
controller_name[R]
env[R]
request[R]

Public Instance Methods

__actions() click to toggle source
# File lib/swee/controller.rb, line 127
def __actions
  ControllerFilter.fliter_methods[self.class.to_s]
end
_name() click to toggle source
# File lib/swee/controller.rb, line 46
def _name
  @name
end
cookies() click to toggle source

TODO

# File lib/swee/controller.rb, line 118
def cookies

end
method_missing(method_name, *args, &blk) click to toggle source
Calls superclass method
# File lib/swee/controller.rb, line 28
def method_missing method_name, *args, &blk
  if hash.key? method_name
    hash[method_name]
  else
    case method_name
    when :remote_ip; hash[:ip]
    when :post?; hash[:method] == "POST"
    when :get?;  hash[:method] == "GET"
    when :xhr?;  hash[:xhr]
    else
      super
    end
  end
end
params() click to toggle source

获取 参数

# File lib/swee/controller.rb, line 59
def params
  { "controller" => @controller_name, "action" => @action_name }.merge @rack_request.params
end
rack_request() click to toggle source
# File lib/swee/controller.rb, line 50
def rack_request
  @rack_request
end
rack_request_to_hash() click to toggle source

映射 rake request -> struct

# File lib/swee/controller.rb, line 64
def rack_request_to_hash
  _request = rack_request
  _body = {
      ip:                 _request.ip,
      cookies:            _request.cookies,
      session:            _request.session,
      session_options:    _request.session_options,
      referer:            _request.referer,
      user_agent:         _request.user_agent,
      method:             _request.request_method,
      path:               _request.path,
      path_info:          _request.path_info,
      params:             _request.params,
      port:               _request.port,
      url:                _request.url,
      base_url:           _request.base_url,
      fullpath:           _request.fullpath,
      host:               _request.host,
      host_with_port:     _request.host_with_port,
      logger:             _request.logger,
      media_type:         _request.media_type,
      media_type_params:  _request.media_type_params,
      POST:               _request.POST,
      query_string:       _request.query_string,
      scheme:             _request.scheme,
      script_name:        _request.script_name,
      ssl:                _request.ssl?,
      trace:              _request.trace?,
      xhr:                _request.xhr?,
      client:             _request['client']
  }
end
rake_format(body,type) click to toggle source
# File lib/swee/controller.rb, line 113
def rake_format body,type

end
render(options={}) click to toggle source

渲染 text: render :text => “foobar” erb: render (可省略对应寻找view) json: render :json => { foo: “bar” }

# File lib/swee/controller.rb, line 101
def render options={}
  if options.empty?
    _render_file
  else
    if options[:text]
      _render_text options
    elsif options[:json]
      _render_json options
    end
  end
end
response() click to toggle source
# File lib/swee/controller.rb, line 54
def response
  @response
end
sessions() click to toggle source

TODO

# File lib/swee/controller.rb, line 123
def sessions

end
warp_request(env,route) click to toggle source

包装request rack 的 request -> controller request .方法的 映射 -> [] 方法 Struct 简单的包装

# File lib/swee/controller.rb, line 20
def warp_request env,route
  @env = env
  @rack_request = Rack::Request.new env  # 暂时用 rack 方式接受 TODO: 解析 env
  # @response = Rack::Response.new
  @controller_name = route.controller_name
  @name = route.controller
  @action_name = route.action
  request_struct =  Struct.new(:hash) do
    def method_missing method_name, *args, &blk
      if hash.key? method_name
        hash[method_name]
      else
        case method_name
        when :remote_ip; hash[:ip]
        when :post?; hash[:method] == "POST"
        when :get?;  hash[:method] == "GET"
        when :xhr?;  hash[:xhr]
        else
          super
        end
      end
    end
  end
  @request = request_struct.new(rack_request_to_hash)
end

Private Instance Methods

_render_file(ftype="html") click to toggle source
# File lib/swee/controller.rb, line 133
def _render_file(ftype="html")
  if ftype == "html"
    _view = View.new(self)
    _body = _view.create_view
  else
    _body = "no file was loaded!"
  end
  # _length = View.calc_length(_body).to_s
  [200,{ "Content-Type" => "text/html; charset=utf8" },[_body]]
end
_render_json(options) click to toggle source
# File lib/swee/controller.rb, line 150
def _render_json options
  _body = options[:json].to_json
  # _length = View.calc_length(_body).to_s
  [200,{ "Content-Type" => "application/json; charset=utf8" },[_body]]
end
_render_text(options) click to toggle source
# File lib/swee/controller.rb, line 144
def _render_text options
  _body = options[:text]
  # _length = View.calc_length(_body).to_s
  [200,{ "Content-Type" => "text/plain; charset=utf8" },[_body]]
end