Class: Pakada
- Inherits:
-
Object
- Object
- Pakada
- Includes:
- Hooked
- Defined in:
- lib/pakada.rb,
lib/pakada/module.rb,
lib/pakada/version.rb
Defined Under Namespace
Modules: Module
Constant Summary collapse
- VERSION =
"0.3.5"
Class Attribute Summary collapse
-
.instances ⇒ Object
readonly
Returns the value of attribute instances.
-
.stack ⇒ Object
readonly
Returns the value of attribute stack.
Instance Attribute Summary collapse
-
#middleware ⇒ Object
readonly
Returns the value of attribute middleware.
-
#modules ⇒ Object
readonly
Returns the value of attribute modules.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#urls ⇒ Object
readonly
Returns the value of attribute urls.
Class Method Summary collapse
- .[](pakada_name) ⇒ Object
- .boot ⇒ Object
- .call(env) ⇒ Object
- .development? ⇒ Boolean
- .env ⇒ Object
- .instance(name_or_ins = nil) ⇒ Object
- .middleware ⇒ Object
- .modules ⇒ Object
- .path ⇒ Object
- .production? ⇒ Boolean
- .safety(original) ⇒ Object
- .staging? ⇒ Boolean
- .testing? ⇒ Boolean
- .urls ⇒ Object
Instance Method Summary collapse
- #boot ⇒ Object
- #call(req_env) ⇒ Object
-
#initialize(name, config = {}) ⇒ Pakada
constructor
A new instance of Pakada.
- #request(req_env) ⇒ Object
- #safety(original) ⇒ Object
- #to_app ⇒ Object
Constructor Details
#initialize(name, config = {}) ⇒ Pakada
Returns a new instance of Pakada.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/pakada.rb', line 113 def initialize(name, config = {}) @name = name config[:urls] ||= [] config[:modules] ||= Module.descendants config[:path] ||= name == :default ? "" : "instances/#{name}" @safety = {} path = File.(config[:path]) @urls, @path = Array(config[:urls]), Pathname.new(path) modules = resolve_modules(Array(config[:modules])) Pakada.instance(self) { @modules = modules.inject({}) {|a, e| a[e.pakada_name] = safety(e).allocate; a } @modules.each_value {|m| m.send :initialize } } @middleware = [] end |
Class Attribute Details
.instances ⇒ Object (readonly)
Returns the value of attribute instances.
15 16 17 |
# File 'lib/pakada.rb', line 15 def instances @instances end |
.stack ⇒ Object (readonly)
Returns the value of attribute stack.
15 16 17 |
# File 'lib/pakada.rb', line 15 def stack @stack end |
Instance Attribute Details
#middleware ⇒ Object (readonly)
Returns the value of attribute middleware.
111 112 113 |
# File 'lib/pakada.rb', line 111 def middleware @middleware end |
#modules ⇒ Object (readonly)
Returns the value of attribute modules.
111 112 113 |
# File 'lib/pakada.rb', line 111 def modules @modules end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
110 111 112 |
# File 'lib/pakada.rb', line 110 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
110 111 112 |
# File 'lib/pakada.rb', line 110 def path @path end |
#urls ⇒ Object (readonly)
Returns the value of attribute urls.
110 111 112 |
# File 'lib/pakada.rb', line 110 def urls @urls end |
Class Method Details
.[](pakada_name) ⇒ Object
90 91 92 |
# File 'lib/pakada.rb', line 90 def self.[](pakada_name) modules[pakada_name] if modules end |
.boot ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/pakada.rb', line 45 def self.boot instance(:default) {|ins| ins.urls = ["/"] } if instances.empty? @urlmap = if instances.keys.length > 1 Rack::URLMap.new instances.values.inject({}) {|map, ins| ins.urls.each {|url| map[url] = proc {|env| ins.call env } } map } else instances.values[0] end instances.values.each &:boot end |
.call(env) ⇒ Object
62 63 64 |
# File 'lib/pakada.rb', line 62 def self.call(env) @urlmap.call env end |
.development? ⇒ Boolean
70 71 72 |
# File 'lib/pakada.rb', line 70 def self.development? env == :development end |
.env ⇒ Object
66 67 68 |
# File 'lib/pakada.rb', line 66 def self.env (ENV["RACK_ENV"] || :development).to_sym end |
.instance(name_or_ins = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/pakada.rb', line 18 def self.instance(name_or_ins = nil) return stack.last unless name_or_ins if self === name_or_ins ins = name_or_ins else ins, name = instances[name_or_ins], name_or_ins end return ins unless block_given? unless ins dsl = Struct.new(:urls, :modules, :path).new ret = yield(dsl) config = Hash[*dsl.members.map(&:to_sym).zip(dsl.values).flatten(1)] instances[name] = new(name, config) else stack << ins begin ret = yield(ins) ensure stack.pop end end ret end |
.middleware ⇒ Object
94 95 96 |
# File 'lib/pakada.rb', line 94 def self.middleware instance.middleware if instance end |
.modules ⇒ Object
86 87 88 |
# File 'lib/pakada.rb', line 86 def self.modules instance.modules if instance end |
.path ⇒ Object
102 103 104 |
# File 'lib/pakada.rb', line 102 def self.path instance.path if instance end |
.production? ⇒ Boolean
82 83 84 |
# File 'lib/pakada.rb', line 82 def self.production? env == :production end |
.safety(original) ⇒ Object
106 107 108 |
# File 'lib/pakada.rb', line 106 def self.safety(original) instance.safety original if instance end |
.staging? ⇒ Boolean
78 79 80 |
# File 'lib/pakada.rb', line 78 def self.staging? env == :staging end |
.testing? ⇒ Boolean
74 75 76 |
# File 'lib/pakada.rb', line 74 def self.testing? env == :testing end |
.urls ⇒ Object
98 99 100 |
# File 'lib/pakada.rb', line 98 def self.urls instance.urls if instance end |
Instance Method Details
#boot ⇒ Object
135 136 137 138 139 |
# File 'lib/pakada.rb', line 135 def boot Pakada.instance(self) { [:hooks, :boot].each {|m| modules.each_value &m } } unless Pakada.testing? end |
#call(req_env) ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/pakada.rb', line 145 def call(req_env) Pakada.instance(self) { to_app.call(req_env).tap {|res| res[1].delete_if {|k, v| k =~ /^X-Pakada/ } } } end |
#request(req_env) ⇒ Object
160 161 162 |
# File 'lib/pakada.rb', line 160 def request(req_env) Rack::Response.new("Hi, I'm Pakada #{VERSION}").finish end |
#safety(original) ⇒ Object
141 142 143 |
# File 'lib/pakada.rb', line 141 def safety(original) @safety[original] ||= original.dup end |
#to_app ⇒ Object
153 154 155 156 157 158 |
# File 'lib/pakada.rb', line 153 def to_app Rack::Builder.new.tap {|b| middleware.each {|mw| b.use *(Array === mw ? mw : [mw]) } b.run proc {|req_env| request req_env } }.to_app end |