Module: TaintedLove::Utils
- Included in:
- TaintedLove
- Defined in:
- lib/tainted_love/utils.rb
Instance Method Summary collapse
-
#add_tracking(object, payload = {}) {|Object| ... } ⇒ Object
Adds information about the object.
-
#hash(str) ⇒ Object
Create a hex encoded MD5 hash.
-
#proxy_method(klass, method, replace_return_value = false) {|*args, &block| ... } ⇒ Object
Replaces a method defined in klass.
Instance Method Details
#add_tracking(object, payload = {}) {|Object| ... } ⇒ Object
Adds information about the object. The information can be about where the object is coming from, validation that has been done on the object, etc.
If the object is frozen, the given block will be called with a new object. The caller has the responsability of replacing the frozen object with this new object.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/tainted_love/utils.rb', line 54 def add_tracking(object, payload = {}, &block) frozen = object.frozen? return if frozen && block.nil? payload[:stacktrace] = StackTrace.current object = object.dup if frozen object.tainted_love_tracking << payload block.call(object) if frozen object end |
#hash(str) ⇒ Object
Create a hex encoded MD5 hash
74 75 76 77 78 |
# File 'lib/tainted_love/utils.rb', line 74 def hash(str) h = Digest::MD5.new h.update(str) h.hexdigest end |
#proxy_method(klass, method, replace_return_value = false) {|*args, &block| ... } ⇒ Object
Replaces a method defined in klass.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/tainted_love/utils.rb', line 15 def proxy_method(klass, method, replace_return_value = false, &block) if klass.is_a?(String) if Object.const_defined?(klass) klass = Object.const_get(klass) else return end end original_method = "_tainted_love_original_#{method}" klass.class_eval do alias_method original_method, method define_method method do |*args, &given_block| return_value = send(original_method, *args, &given_block) block_return = block.call(return_value, *args, self, &block) if replace_return_value block_return else return_value end end end end |