Class: Rex::Transformer

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/transformer.rb

Overview

Transformer - more than meets the eye!

This class, aside from having a kickass name, is responsible for translating object instances of one or more types into a single list instance of one or more types. This is useful for translating object instances that be can either strings or an array of strings into an array of strings, for instance. It lets you make things take a uniform structure in an abstract manner.

Class Method Summary collapse

Class Method Details

.bomb_translation(src_instance, target) ⇒ Object (protected)

:nodoc:

Raises:

  • (ArgumentError)


103
104
105
106
107
108
109
110
111
# File 'lib/rex/transformer.rb', line 103

def Transformer.bomb_translation(src_instance, target) # :nodoc:
  error = "Invalid source class (#{src_instance.class})"

  if (target != nil)
    error += " for #{target}"
  end

  raise ArgumentError, error, caller
end

.transform(src_instance, dst_class, supported_classes, target = nil) ⇒ Object

Translates the object instance supplied in src_instance to an instance of dst_class. The dst_class parameter’s instance must support the << operator. An example call to this method looks something like:

Transformer.transform(string, Array, [ String ], target)



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rex/transformer.rb', line 25

def Transformer.transform(src_instance, dst_class, supported_classes,
    target = nil)
  dst_instance = dst_class.new

  if (src_instance.kind_of?(Array))
    src_instance.each { |src_inst|
      Transformer.transform_single(src_inst, dst_instance,
          supported_classes, target)
    }
  elsif (!src_instance.kind_of?(NilClass))
    Transformer.transform_single(src_instance, dst_instance,
        supported_classes, target)
  end

  return dst_instance
end

.transform_single(src_instance, dst_instance, supported_classes, target) ⇒ Object (protected)

Transform a single source instance.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
94
95
96
97
98
99
100
101
# File 'lib/rex/transformer.rb', line 47

def Transformer.transform_single(src_instance, dst_instance,
    supported_classes, target)
  # If the src instance's class is supported, just add it to the dst
  # instance
  if (supported_classes.include?(src_instance.class))
    dst_instance << src_instance
  # If the src instance's class is an array, then we should check to see
  # if any of the supporting classes support from_a.
  elsif (src_instance.kind_of?(Array))
    new_src_instance = nil

    # Walk each supported class calling from_a if exported
    supported_classes.each { |sup_class|
      next if (sup_class.respond_to?('from_a') == false)

      new_src_instance = sup_class.from_a(src_instance)

      if (new_src_instance != nil)
        dst_instance << new_src_instance
        break
      end
    }

    # If we don't have a valid new src instance, then we suck
    if (new_src_instance == nil)
      bomb_translation(src_instance, target)
    end

  # If the source instance is a string, query each of the supported
  # classes to see if they can serialize it to their particular data
  # type.
  elsif (src_instance.kind_of?(String))
    new_src_instance = nil

    # Walk each supported class calling from_s if exported
    supported_classes.each { |sup_class|
      next if (sup_class.respond_to?('from_s') == false)

      new_src_instance = sup_class.from_s(src_instance)

      if (new_src_instance != nil)
        dst_instance << new_src_instance
        break
      end
    }

    # If we don't have a valid new src instance, then we suck
    if (new_src_instance == nil)
      bomb_translation(src_instance, target)
    end
  # Otherwise, bomb translation
  else
    bomb_translation(src_instance, target)
  end
end