Contains all the module functions for Innate, we keep them in a module so Ramaze can simply use them as well.
script_name, path_info = env[‘SCRIPT_NAME’], env[‘PATH_INFO’] answer = app.call(env) env.merge!(‘SCRIPT_NAME’ => script_name, ‘PATH_INFO’ => path_info) answer
# File lib/innate/route.rb, line 110 110: def Rewrite(key, value = nil, &block) 111: Rewrite[key] = value || block 112: end
# File lib/innate/route.rb, line 106 106: def Route(key, value = nil, &block) 107: Route[key] = value || block 108: end
Answer with object at location.
@example
class Hello include Innate::Node map '/' end Innate.at('/') # => Hello
# File lib/innate/dynamap.rb, line 78 78: def at(location) 79: DynaMap.at(location) 80: end
Treat Innate like a rack application, pass the rack env and optionally the mode the application runs in.
@param [Hash] env rack env @param [Symbol] mode indicates the mode of the application @default mode options.mode @return [Array] with [body, header, status] @author manveru
# File lib/innate.rb, line 160 160: def call(env, mode = options[:mode]) 161: middleware(mode).call(env) 162: end
# File lib/innate/state.rb, line 18 18: def defer 19: outer = ::Thread.current 20: ::Thread.new{ 21: inner = ::Thread.current 22: outer.keys.each{|k| inner[k] = outer[k] } 23: yield 24: } 25: end
@example Innate can be started by:
Innate.start :file => __FILE__ Innate.start :root => File.dirname(__FILE__)
Either setting will surpress the warning that might show up on startup and tells you it couldn’t find an explicit root.
In case these options are not passed we will try to figure out a file named `start.rb` in the process’ working directory and assume it’s a valid point.
# File lib/innate.rb, line 186 186: def go_figure_root(backtrace, options) 187: if root = options[:root] 188: root 189: elsif file = options[:file] 190: File.dirname(file) 191: elsif File.file?('start.rb') 192: Dir.pwd 193: else 194: root = File.dirname(backtrace[0][/^(.*?):\d+/, 1]) 195: Log.warn "No explicit root folder found, assuming it is #{root}" 196: root 197: end 198: end
Maps the given object or block to location, object must respond to # in order to be of any use.
@example with passed object
Innate.map('/', lambda{|env| [200, {}, "Hello, World"] }) Innate.at('/').call({}) # => [200, {}, "Hello, World"]
@example with passed block
Innate.map('/'){|env| [200, {}, ['Hello, World!']] } Innate.at('/').call({})
# File lib/innate/dynamap.rb, line 64 64: def map(location, object = nil, &block) 65: DynaMap.map(location, object || block) 66: end
# File lib/innate.rb, line 164 164: def middleware(mode = options[:mode], &block) 165: options[:middleware_compiler].build(mode, &block) 166: end
# File lib/innate.rb, line 168 168: def middleware!(mode = options[:mode], &block) 169: options[:middleware_compiler].build!(mode, &block) 170: end
# File lib/innate.rb, line 172 172: def middleware_recompile(mode = options[:mode]) 173: options[:middleware_compiler]::COMPILED[mode].compile! 174: end
Convenience method to include the Node module into node and map to a location.
@param [#] location where the node is mapped to @param [Node, nil] node the class that will be a node, will try to
look it up if not given
@return [Class, Module] the node argument or detected class will be
returned
@api external @see SingletonMethods::node_from_backtrace @author manveru
# File lib/innate/node.rb, line 1028 1028: def node(location, node = nil) 1029: node ||= node_from_backtrace(caller) 1030: node.__send__(:include, Node) 1031: node.map(location) 1032: node 1033: end
Cheap hack that works reasonably well to avoid passing self all the time to Innate::node We simply search the file that Innate::node was called in for the first class definition above the line that Innate::node was called and look up the constant. If there are any problems with this (filenames containing ’:’ or metaprogramming) just pass the node parameter explicitly to Innate::node
@param [Array
@return [Class, Module]
@api internal @see SingletonMethods::node @author manveru
# File lib/innate/node.rb, line 1050 1050: def node_from_backtrace(backtrace) 1051: filename, lineno = backtrace[0].split(':', 2) 1052: regexp = /^\s*class\s+(\S+)/ 1053: File.readlines(filename)[0..lineno.to_i].reverse.find{|ln| ln =~ regexp } 1054: const_get($1) 1055: end
# File lib/innate.rb, line 144 144: def setup_dependencies 145: options[:setup].each{|obj| obj.setup if obj.respond_to?(:setup) } 146: end
The method that starts the whole business.
Call Innate.start after you defined your application.
Usually, this is a blocking call and will not return until the adapter has finished, which usually happens when you kill the application or hit ^C.
We do return if options.started is true, which indicates that all you wanted to do is setup the environment and update options.
@example usage
# passing options Innate.start :adapter => :mongrel, :mode => :live # defining custom middleware Innate.start do |m| m.innate end
@return [nil] if options.started is true @yield [MiddlewareCompiler] @param [Proc] block will be passed to {middleware!}
@option param :host [String] (‘0.0.0.0’)
IP address or hostname that we respond to - 0.0.0.0 for all
@option param :port [Fixnum] (7000)
Port for the server
@option param :started [boolean] (false)
Indicate that calls Innate::start will be ignored
@option param :adapter [Symbol] (:webrick)
Web server to run on
@option param :setup [Array] ([Innate::Cache, Innate::Node])
Will send ::setup to each element during Innate::start
@option param :header [Hash] ({‘Content-Type’ => ‘text/html’})
Headers that will be merged into the response before Node::call
@option param :trap [String] (‘SIGINT’)
Trap this signal to issue shutdown, nil/false to disable trap
@option param :mode [Symbol] (:dev)
Indicates which default middleware to use, (:dev|:live)
# File lib/innate.rb, line 106 106: def start(options = {}, &block) 107: root, file = options.delete(:root), options.delete(:file) 108: innate_options = Innate.options 109: 110: found_root = go_figure_root(caller, :root => root, :file => file) 111: innate_options.roots = [*found_root] if found_root 112: 113: # Convert some top-level option keys to the internal ones that we use. 114: PROXY_OPTIONS.each{|given, proxy| options[proxy] = options[given] } 115: options.delete_if{|key, value| PROXY_OPTIONS[key] || value.nil? } 116: 117: # Merge the user's given options into our existing set, which contains defaults. 118: innate_options.merge!(options) 119: 120: setup_dependencies 121: middleware!(innate_options.mode, &block) if block_given? 122: 123: return if innate_options.started 124: innate_options.started = true 125: 126: signal = innate_options.trap 127: trap(signal){ stop(10) } if signal 128: 129: start! 130: end
# File lib/innate.rb, line 132 132: def start!(mode = options[:mode]) 133: Adapter.start(middleware(mode)) 134: end
# File lib/innate.rb, line 136 136: def stop(wait = 3) 137: Log.info("Shutdown within #{wait} seconds") 138: Timeout.timeout(wait){ teardown_dependencies } 139: Timeout.timeout(wait){ exit } 140: ensure 141: exit! 142: end
Use this method to achieve thread-safety for sensitive operations.
This should be of most use when manipulating files to prevent other threads from doing the same, no other code will be scheduled during execution of this method.
@param [Proc] block the things you want to execute
# File lib/innate/state.rb, line 14 14: def sync(&block) 15: SEMAPHORE.synchronize(&block) 16: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.