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.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/Reflection.rb', line 112

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.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/Reflection.rb', line 133

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



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/Reflection.rb', line 150

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
89
90
91
92
93
# 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(@inputs)
        @status = PASS
      else
        @status = FAIL
      end
    end

    # Run reflection.
    @output = @clone.send(@method, *@inputs)

    # Validate output with controls.
    unless @ruler.nil?
      if @ruler.validate_output(@output)
        @status = PASS
      else
        @status = FAIL
      end
      return
    end

  # When fail.
  rescue StandardError => message
    @status = FAIL
    @message = message

  # When no validation and execution fails.
  else
    @status = PASS
  end

end

#resultObject



95
96
97
98
99
100
101
102
103
104
# File 'lib/Reflection.rb', line 95

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