Module: TaintedLove::Utils

Included in:
TaintedLove
Defined in:
lib/tainted_love/utils.rb,
lib/tainted_love/utils/proxy.rb

Defined Under Namespace

Classes: Proxy

Instance Method Summary collapse

Instance Method Details

#hash(str) ⇒ String

Create a hex encoded MD5 hash

Parameters:

  • str (String)

    Input string

Returns:

  • (String)


59
60
61
62
63
# File 'lib/tainted_love/utils.rb', line 59

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.

Parameters:

  • klass (Class, String)

    The target class

  • method (Symbol)

    The method name to replace

  • replace_return_value (Boolean) (defaults to: false)

    If true, the return value of the function will be the value returned by the block. Otherwise, the function will return its original value.

Yields:

  • (*args, &block)

    Block to execute when the function is called



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

#tag(object, payload = {}) ⇒ 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.

Parameters:

  • object (Object)

    Object to add tracking

  • payload (Hash) (defaults to: {})

    Data to add to the object

Returns:

  • (Object)

    Given object or dup of it



49
50
51
52
53
# File 'lib/tainted_love/utils.rb', line 49

def tag(object, payload = {})
  object.tainted_love_tags << payload

  object
end