Class: Ruby_process::Cproxy
- Inherits:
-
Object
- Object
- Ruby_process::Cproxy
- Defined in:
- lib/ruby_process_cproxy.rb
Overview
This class is used to seamlessly use leaky classes without working through ‘Ruby_process’.
Examples
Ruby_process::Cproxy.run do |data|
data[:subproc].static(:Object, :require, "rubygems")
data[:subproc].static(:Object, :require, "rexml/document")
doc = Ruby_process::Cproxy::REXML::Document.new("test")
strio = StringIO.new
doc.write(strio)
puts strio.string #=> "<test/>"
raise "REXML shouldnt be defined?" if Kernel.const_defined?(:REXML)
Constant Summary collapse
- @@lock =
Lock is used to to create new Ruby-process-instances and not doing double-counts.
Monitor.new
- @@instances =
Counts how many instances are using the Cproxy-module. This way it can be safely unset once no-body is using it again.
0- @@subproc =
This variable will hold the ‘Ruby_process’-object where sub-objects will be created.
nil
Class Method Summary collapse
-
.const_missing(name) ⇒ Object
Creates the new constant under the ‘Ruby_process::Cproxy’-namespace.
-
.load_class(const, name) ⇒ Object
Loads a new class to the given constants namespace for recursive creating of missing classes.
-
.run ⇒ Object
All use should go through this method to automatically destroy sub-processes and keep track of ressources.
-
.subproc ⇒ Object
Returns the ‘Ruby_process’-object or raises an error if it has not been set.
Class Method Details
.const_missing(name) ⇒ Object
Creates the new constant under the ‘Ruby_process::Cproxy’-namespace.
57 58 59 60 61 |
# File 'lib/ruby_process_cproxy.rb', line 57 def self.const_missing(name) Ruby_process::Cproxy.load_class(self, name) if !self.const_defined?(name) raise "Still not created on const: '#{name}'." if !self.const_defined?(name) return self.const_get(name) end |
.load_class(const, name) ⇒ Object
Loads a new class to the given constants namespace for recursive creating of missing classes.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ruby_process_cproxy.rb', line 64 def self.load_class(const, name) const.const_set(name, Class.new{ #Use 'const_missing' to auto-create missing sub-constants recursivly. def self.const_missing(name) Ruby_process::Cproxy.load_class(self, name) if !self.const_defined?(name) raise "Still not created on const: '#{name}'." if !self.const_defined?(name) return self.const_get(name) end #Manipulate 'new'-method return proxy-objects instead of real objects. def self.new(*args, &blk) name_match = self.name.to_s.match(/^Ruby_process::Cproxy::(.+)$/) class_name = name_match[1] subproc = Ruby_process::Cproxy.subproc obj = subproc.new(class_name, *args, &blk) return obj end def self.method_missing(method_name, *args, &blk) name_match = self.name.to_s.match(/^Ruby_process::Cproxy::(.+)$/) class_name = name_match[1] subproc = Ruby_process::Cproxy.subproc return subproc.static(class_name, method_name, *args, &blk) end }) end |
.run ⇒ Object
All use should go through this method to automatically destroy sub-processes and keep track of ressources.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ruby_process_cproxy.rb', line 25 def self.run #Increase count of instances that are using Cproxy and set the subproc-object if not already set. @@lock.synchronize do @@instances += 1 if !@@subproc @@subproc = Ruby_process.new @@subproc.spawn_process end end begin yield(:subproc => @@subproc) ensure @@lock.synchronize do @@instances -= 1 if @@instances <= 0 @@subproc.destroy @@subproc = nil end end end end |
.subproc ⇒ Object
Returns the ‘Ruby_process’-object or raises an error if it has not been set.
51 52 53 54 |
# File 'lib/ruby_process_cproxy.rb', line 51 def self.subproc raise "CProxy process not set for some reason?" if !@@subproc return @@subproc end |