Module: Spork
- Defined in:
- lib/spork.rb,
lib/spork/runner.rb
Defined Under Namespace
Modules: CustomIOStreams Classes: AppFramework, Diagnoser, Forker, Runner, Server
Constant Summary collapse
- BINARY =
File.(File.dirname(__FILE__) + '/../bin/spork')
- LIBDIR =
File.(File.dirname(__FILE__))
Class Method Summary collapse
-
.each_run(prevent_double_run = true, &block) ⇒ Object
Run a block AFTER the fork occurs.
-
.exec_each_run(&block) ⇒ Object
Used by the server.
-
.exec_prefork(&block) ⇒ Object
Used by the server.
-
.prefork(prevent_double_run = true, &block) ⇒ Object
Run a block, during prefork mode.
-
.state ⇒ Object
Used by the server.
-
.trap_class_method(klass, method_name) ⇒ Object
Same as trap_method, but for class methods instead.
-
.trap_method(klass, method_name) ⇒ Object
Traps an instance method of a class (or module) so any calls to it don’t actually run until Spork.exec_each_run.
-
.using_spork! ⇒ Object
Used by the server.
Class Method Details
.each_run(prevent_double_run = true, &block) ⇒ Object
Run a block AFTER the fork occurs. By default, if prefork is called twice in the same file and line number, the supplied block will only be ran once.
Parameters
-
prevent_double_run
- Pass false to disable double run prevention
22 23 24 25 26 27 28 29 |
# File 'lib/spork.rb', line 22 def each_run(prevent_double_run = true, &block) return if prevent_double_run && already_ran?(caller.first) if @state == :using_spork each_run_procs << block else yield end end |
.exec_each_run(&block) ⇒ Object
Used by the server. Called to run all of the prefork blocks.
48 49 50 51 52 |
# File 'lib/spork.rb', line 48 def exec_each_run(&block) each_run_procs.each { |p| p.call } each_run_procs.clear yield if block_given? end |
.exec_prefork(&block) ⇒ Object
Used by the server. Called when loading the prefork blocks of the code.
42 43 44 45 |
# File 'lib/spork.rb', line 42 def exec_prefork(&block) using_spork! yield end |
.prefork(prevent_double_run = true, &block) ⇒ Object
Run a block, during prefork mode. By default, if prefork is called twice in the same file and line number, the supplied block will only be ran once.
Parameters
-
prevent_double_run
- Pass false to disable double run prevention
12 13 14 15 |
# File 'lib/spork.rb', line 12 def prefork(prevent_double_run = true, &block) return if prevent_double_run && already_ran?(caller.first) yield end |
.state ⇒ Object
Used by the server. Returns the current state of Spork.
37 38 39 |
# File 'lib/spork.rb', line 37 def state @state ||= :not_using_spork end |
.trap_class_method(klass, method_name) ⇒ Object
Same as trap_method, but for class methods instead
69 70 71 |
# File 'lib/spork.rb', line 69 def trap_class_method(klass, method_name) trap_method((class << klass; self; end), method_name) end |
.trap_method(klass, method_name) ⇒ Object
Traps an instance method of a class (or module) so any calls to it don’t actually run until Spork.exec_each_run
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/spork.rb', line 55 def trap_method(klass, method_name) method_name_without_spork, method_name_with_spork = alias_method_names(method_name, :spork) klass.class_eval <<-EOF, __FILE__, __LINE__ + 1 alias :#{method_name_without_spork} :#{method_name} unless method_defined?(:#{method_name_without_spork}) def #{method_name}(*args) Spork.each_run(false) do #{method_name_without_spork}(*args) end end EOF end |
.using_spork! ⇒ Object
Used by the server. Sets the state to activate spork. Otherwise, prefork and each_run are run in passive mode, allowing specs without a Spork server.
32 33 34 |
# File 'lib/spork.rb', line 32 def using_spork! @state = :using_spork end |