Class: Reflection

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

Direct Known Subclasses

Control

Constant Summary collapse

TIME =

Keys.

"t"
INPUT =
"i"
OUTPUT =
"o"
TYPE =
"T"
COUNT =
"C"
VALUE =
"V"
STATUS =
"s"
MESSAGE =
"m"
PASS =

Values.

"p"
FAIL =
"f"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(execution, method, ruler) ⇒ Reflection

Returns a new instance of Reflection.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/Reflection.rb', line 18

def initialize(execution, method, ruler)

  @execution = execution
  @method = method
  @ruler = ruler

  # Clone the execution's object.
  @clone = execution.object.clone
  @clone_id = nil

  # Result.
  @status = nil
  @time = Time.now.to_i
  @inputs = []
  @output = nil

end

Instance Attribute Details

#cloneObject

Returns the value of attribute clone.



16
17
18
# File 'lib/Reflection.rb', line 16

def clone
  @clone
end

Instance Method Details

#normalize_input(args) ⇒ Object

Normalize inputs.

Parameters:

  • args
    • The actual inputs.

Returns:

    • A generic inputs representation.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/Reflection.rb', line 107

def normalize_input(args)
  inputs = []
  args.each do |arg|
    input = {
      TYPE => arg.class.to_s,
      VALUE => normalize_value(arg)
    }
    if (arg.class == Array)
      input[COUNT] = arg.count
    end
    inputs << input
  end
  inputs
end

#normalize_output(input) ⇒ Object

Normalize output.

Parameters:

  • input
    • The actual output.

Returns:

    • A generic output representation.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/Reflection.rb', line 128

def normalize_output(input)

  output = {
    TYPE => input.class.to_s,
    VALUE => normalize_value(input)
  }

  if (input.class == Array || input.class == Hash)
    output[COUNT] = input.count
  elsif (input.class == TrueClass || input.class == FalseClass)
    output[TYPE] = :Boolean
  end

  return output

end

#normalize_value(value) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/Reflection.rb', line 145

def normalize_value(value)

  unless value.nil?
    value = value.to_s.gsub(/\r?\n/, " ").to_s
    if value.length >= 30
      value = value[0, value.rindex(/\s/,30)].rstrip() + '...'
    end
  end

  return value

end

#reflect(*args) ⇒ Object

Reflect on a method.

Creates a shadow execution stack.

Parameters:

  • method
    • The name of the method.

  • *args
    • The method arguments.

Returns:

    • A reflection hash.



46
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
# File 'lib/Reflection.rb', line 46

def reflect(*args)

  # Create deviated arguments.
  args.each do |arg|
    case arg
    when Integer
      @inputs << rand(999)
    else
      @inputs << arg
    end
  end

  # Action method with new arguments.
  begin
    # Validate input with controls.
    unless @ruler.nil?
      if @ruler.validate_inputs(@execution.caller_class, @method, @inputs)
        @status = PASS
      else
        @status = FAIL
      end
    end
    # Run reflection.
    @output = @clone.send(@method, *@inputs)
  # When fail.
  rescue StandardError => message
    @status = FAIL
    @message = message
  # When pass.
  else
    # Validate output with controls.
    unless @ruler.nil?
      if @ruler.validate_output(@execution.caller_class, @method, @output)
        @status = PASS
      else
        @status = FAIL
      end
      return
    end
    @status = PASS
  end

end

#resultObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/Reflection.rb', line 90

def result()
  # Build reflection.
  reflection = {
    TIME => @time,
    STATUS => @status,
    INPUT => normalize_input(@inputs),
    OUTPUT => normalize_output(@output),
    MESSAGE => @message
  }
end