Class: Bike

Inherits:
Object
  • Object
show all
Defined in:
lib/bike.rb

Overview

Author

Akira FUNAI

Copyright

Copyright © 2009 Akira FUNAI

Defined Under Namespace

Modules: Error, I18n, Meta, Parser, Path, REX, Response, Set Classes: Checkbox, Field, File, Img, Password, Radio, Select, Storage, Text, Textarea, Workflow

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](name) ⇒ Object



32
33
34
35
# File 'lib/bike.rb', line 32

def self.[](name)
  @config ||= {}
  @config[name]
end

.baseObject



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

def self.base
  self.current[:base]
end

.clientObject



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

def self.client
  self.session[:client] ||= 'nobody'
end

.client=(id) ⇒ Object



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

def self.client=(id)
  self.session[:client] = id
end

.config(config) ⇒ Object



28
29
30
# File 'lib/bike.rb', line 28

def self.config(config)
  @config = config
end

.currentObject



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

def self.current
  Thread.current
end

.libdirObject



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

def self.libdir
  ::File.dirname __FILE__
end

.sessionObject



41
42
43
# File 'lib/bike.rb', line 41

def self.session
  self.current[:session] || ($fake_session ||= {})
end

.static(env) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bike.rb', line 73

def self.static(env)
  @static ||= Rack::Directory.new Bike['skin_dir']
  response = @static.call env

  if response.first == 404
    until ::File.readable? ::File.join(
      Bike['skin_dir'],
      env['PATH_INFO'].sub(%r{(/#{Bike::REX::DIR_STATIC}/).*}, '\\1')
    )
      env['PATH_INFO'].sub!(%r{/[^/]+(?=/#{Bike::REX::DIR_STATIC}/)}, '') || break
    end
    @static.call env
  else
    response
  end
end

.tokenObject



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

def self.token
  self.session[:token] ||= rand(36 ** 32).to_s(36)
end

.transactionObject



45
46
47
# File 'lib/bike.rb', line 45

def self.transaction
  self.session[:transaction] ||= {}
end

.uriObject



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

def self.uri
  self.current[:uri]
end

Instance Method Details

#call(env) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/bike.rb', line 90

def call(env)
  uri    = "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}#{env['SCRIPT_NAME']}"
  req    = Rack::Request.new env
  method = req.request_method.downcase
  params = params_from_request req
  path   = req.path_info
  tid    = Bike::Path.tid_of path

  static_path = ::File.expand_path path
  return Bike.static(env) if (
    static_path =~ %r{/#{Bike::REX::DIR_STATIC}/} &&
    static_path !~ %r{/#{Bike::REX::TID}/} &&
    static_path !~ %r{/#{Bike::REX::ID.to_s.sub('^','')}/}
  )

  Bike::I18n.lang = env['HTTP_ACCEPT_LANGUAGE']

  Bike.current[:env]     = env
  Bike.current[:uri]     = uri
  Bike.current[:req]     = req
  Bike.current[:session] = env['rack.session']

  if Bike.transaction[tid].is_a? Bike::Field
    base = Bike.transaction[tid].item Bike::Path.steps_of(path.sub(/\A.*#{Bike::REX::TID}/, ''))
  else
    base = Bike::Path.base_of path
  end
  return Bike::Response.not_found unless base

  base[:tid] = tid
  Bike.current[:base] = base

  base.workflow.call(method, params)
end