Class: Tee
- Inherits:
-
Object
- Object
- Tee
- Defined in:
- lib/libisi/tee.rb
Overview
Copyright © 2007-2010 Logintas AG Switzerland
This file is part of Libisi.
Libisi is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Libisi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Libisi. If not, see <www.gnu.org/licenses/>.
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
Instance Method Summary collapse
- #call_child(child_id, method_id, arguments, block) ⇒ Object
- #deep_clone(obj) ⇒ Object
-
#initialize(children = []) ⇒ Tee
constructor
A new instance of Tee.
- #method_missing(method_id, *arguments, &block) ⇒ Object
- #signature(method_id, *arguments, &block) ⇒ Object
Constructor Details
#initialize(children = []) ⇒ Tee
Returns a new instance of Tee.
45 46 47 |
# File 'lib/libisi/tee.rb', line 45 def initialize(children = []) @children = children end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *arguments, &block) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/libisi/tee.rb', line 53 def method_missing(method_id, *arguments, &block) if block function_results, block_results = call_child(0, method_id, arguments, block) function_results[0] else $log.debug{"Calling #{signature(method_id, *arguments, &block)} sequentially"} # no block given, we can execute # methods sequentially result = nil children.each_with_index {|child, index| if index < (children.length-1) child.send(method_id, *(deep_clone(arguments))) else result = child.send(method_id, *arguments) end } result end end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
19 20 21 |
# File 'lib/libisi/tee.rb', line 19 def children @children end |
Instance Method Details
#call_child(child_id, method_id, arguments, block) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 124 125 126 127 |
# File 'lib/libisi/tee.rb', line 73 def call_child(child_id, method_id, arguments, block) raise "child with id #{child_id} does not exist" unless children[child_id] original_arguments = arguments arguments = deep_clone(arguments) raise "no block given" unless block signature = signature(method_id, *arguments, &block) $log.debug("Child ##{child_id}: Calling #{signature}") vars = [] if block.arity > 0 block.arity.times {|i| vars.push("a#{i}")} else (-block.arity - 1).times {|i| vars.push("a#{i}")} vars.push("*a") end block_text = "" block_text += "proc {|#{vars.join(",")}|\n" if child_id < (children.length-1) block_text += " call_count += 1\n" block_text += " function_results, block_results = call_child(child_id + 1, method_id, original_arguments, block) if call_count == 1\n" block_text += " br = block_results[(call_count-1)]\n" block_text += " br = deep_clone(br)\n" # block_text += " p [#{vars.join(",")}]\n" block_text += " br\n" else $log.debug("Last child, collecting results") # this is the last child # here we must collect all # block results block_text += " br = block.call(#{vars.join(",")})\n" block_text += " $log.debug{\"Result of block is: \#\{br.inspect}\"\}\n" block_text += " block_results << deep_clone(br)\n" block_text += " call_count += 1\n" block_text += " br\n" end block_text += "}\n" block_results = [] function_results = [] call_count = 0 new_block = eval(block_text) raise "Not same arity" if new_block.arity != block.arity if child_id < (children.length-1) function_results << children[child_id].send(method_id, *(deep_clone(arguments)), &new_block) else function_results << children[child_id].send(method_id, *original_arguments, &new_block) end raise "#{signature} master block execute #{block_results.length - 1} " + "times child block ##{child_id} #{call_count} times." if call_count != (block_results.length) [function_results, block_results] end |
#deep_clone(obj) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/libisi/tee.rb', line 21 def deep_clone(obj) case obj when NilClass obj when Fixnum,Bignum,Float,NilClass,FalseClass, TrueClass,Continuation, Symbol, Rational klone = obj when Hash klone = obj.clone obj.each{|k,v| klone[k] = deep_clone(v)} when Array klone = obj.clone klone.clear obj.each{|v| klone << deep_clone(v)} else klone = obj.clone end klone.instance_variables.each {|v| klone.instance_variable_set(v,deep_clone(klone.instance_variable_get(v))) } klone end |
#signature(method_id, *arguments, &block) ⇒ Object
49 50 51 |
# File 'lib/libisi/tee.rb', line 49 def signature(method_id, *arguments, &block) "#{method_id}(#{arguments.map {|a| a.inspect}.join(",")}#{",&block" if block})" end |