Class: Result

Inherits:
Object
  • Object
show all
Defined in:
lib/teuton/utils/result/result.rb,
lib/teuton/utils/result/ext_array.rb,
lib/teuton/utils/result/ext_filter.rb,
lib/teuton/utils/result/ext_compare.rb

Overview

Extension of Result class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResult

Returns a new instance of Result.



21
22
23
# File 'lib/teuton/utils/result/result.rb', line 21

def initialize
  reset
end

Instance Attribute Details

#alterationsObject



25
26
27
28
29
30
31
# File 'lib/teuton/utils/result/result.rb', line 25

def alterations
  if @alterations.is_a? String
    @alterations
  else
    @alterations.join(" & ")
  end
end

#contentObject

Returns the value of attribute content.



17
18
19
# File 'lib/teuton/utils/result/result.rb', line 17

def content
  @content
end

#exitcodeObject

Returns the value of attribute exitcode.



18
19
20
# File 'lib/teuton/utils/result/result.rb', line 18

def exitcode
  @exitcode
end

Instance Method Details

#contain?(value) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/teuton/utils/result/ext_array.rb', line 28

def contain?(value)
  @expected = "Contain <#{value}> value"
  @content.contain? value
end

#countObject Also known as: count!, length, len, size



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/teuton/utils/result/ext_array.rb', line 5

def count
  @alterations << "count"
  if @content.instance_of? Array
    @content = [@content.count]
    self
  elsif @content.nil?
    @content = ["0"]
  else
    @content = [@content.to_i.to_s]
  end
  self
end

#debugObject



38
39
40
41
42
43
44
45
46
# File 'lib/teuton/utils/result/result.rb', line 38

def debug
  print "\n" + "*" * 20
  print " [DEBUG] count=#{@content.count} "
  puts "*" * 20
  @content.each_with_index do |item, index|
    puts format("%<index>2d: %<item>s", {index: index, item: item})
  end
  puts "*" * 57
end

#emptyObject Also known as: empty?



33
34
35
36
# File 'lib/teuton/utils/result/ext_array.rb', line 33

def empty
  @expected = "Empty!"
  @content.empty
end

#eq(input) ⇒ Object Also known as: eq?, equal, equal?, is_equal?



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/teuton/utils/result/ext_compare.rb', line 5

def eq(input)
  # Return true when content is equal than input
  @expected = input

  case input.class.to_s
  when "Fixnum"
    value = @content[0].to_i
    puts "[WARN] Fixnum is a Ruby deprecated class!"
    puts "       (Upgrade your Ruby version)"
  when "Float"
    value = @content[0].to_f
  when "Integer"
    value = @content[0].to_i
  when "String"
    value = @content[0].to_s
  else
    value = @content[0]
  end
  value == input
end

#expectedObject



48
49
50
# File 'lib/teuton/utils/result/result.rb', line 48

def expected
  @expected.to_s
end

#find(filter) ⇒ Object Also known as: grep, grep!, find!

TODO: Error line 102 undefined include? method for 0 Fixnum…



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/teuton/utils/result/ext_filter.rb', line 6

def find(filter)
  @alterations << "find(#{filter})"
  case filter.class.to_s
  when "Array"
    find_when_array(filter)
  when "String" || "Integer"
    @content.select! { |i| i.include?(filter.to_s) }
  when "Regexp"
    @content.select! { |i| filter.match(i) }
  end
  self
end

#firstObject



22
23
24
25
26
# File 'lib/teuton/utils/result/ext_filter.rb', line 22

def first
  @alterations << "first"
  @content = [@content.first]
  self
end

#ge(input) ⇒ Object Also known as: greater_or_equal, greater_or_equal?



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/teuton/utils/result/ext_compare.rb', line 51

def ge(input)
  @expected = "Greater or equal to #{input}"
  return false if @content.nil? || @content[0].nil?

  value = @content[0]
  case input.class.to_s
  when "Fixnum"
    value = @content[0].to_i
    puts "[WARN] Fixnum class is deprecated!"
    puts "       Upgrade your Ruby version."
  when "Float"
    value = @content[0].to_f
  when "Integer"
    value = @content[0].to_i
  end
  value >= input
end

#gt(input) ⇒ Object Also known as: greater, greater_than



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/teuton/utils/result/ext_compare.rb', line 71

def gt(input)
  @expected = "Greater than #{input}"
  return false if @content.nil? || @content[0].nil?

  value = @content[0]
  case input.class.to_s
  when "Fixnum"
    value = @content[0].to_i
    puts "[WARN] Fixnum class is deprecated!"
    puts "       Upgrade your Ruby version."
  when "Float"
    value = @content[0].to_f
  when "Integer"
    value = @content[0].to_i
  end
  value > input
end

#include?(value) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/teuton/utils/result/ext_array.rb', line 18

def include?(value)
  @expected = "Include <#{value}> value"
  @content[0].include?(value)
end

#lastObject



58
59
60
61
62
# File 'lib/teuton/utils/result/ext_filter.rb', line 58

def last
  @alterations << "last"
  @content = [@content.last]
  self
end

#le(input) ⇒ Object Also known as: lesser_or_equal, lesser_or_equal?



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/teuton/utils/result/ext_compare.rb', line 91

def le(input)
  @expected = "Lesser or equal to #{input}"

  return false if @content.nil? || @content[0].nil?

  value = @content[0]
  case input.class.to_s
  when "Fixnum"
    value = @content[0].to_i
    puts "[WARN] Fixnum class is deprecated!"
    puts "       Upgrade your Ruby version."
  when "Float"
    value = @content[0].to_f
  when "Integer"
    value = @content[0].to_i
  end
  value <= input
end

#lt(input) ⇒ Object Also known as: lesser, smaller, lesser_than



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/teuton/utils/result/ext_compare.rb', line 112

def lt(input)
  @expected = "Lesser than #{input}"
  return false if @content.nil? || @content[0].nil?

  value = @content[0]
  case input.class.to_s
  when "Fixnum"
    value = @content[0].to_i
    puts "[WARN] Fixnum class is deprecated!"
    puts "       Upgrade your Ruby version."
  when "Float"
    value = @content[0].to_f
  when "Integer"
    value = @content[0].to_i
  end
  value < input
end

#near_to?(input) ⇒ Boolean Also known as: near_to, near?, near

Return ‘true’ if the parameter value is near to the target value. To get this we consider a 10% desviation or less, as an acceptable result.

Returns:

  • (Boolean)


135
136
137
138
139
140
141
142
143
144
# File 'lib/teuton/utils/result/ext_compare.rb', line 135

def near_to?(input)
  @expected = "Is near to #{input}"
  return false if @content.nil?

  target = @content[0].to_f
  desv = (target * 10.0) / 100.0
  return true if (target - input.to_f).abs.to_f <= desv

  false
end

#neq(external) ⇒ Object Also known as: neq?, not_equal, not_equal?



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/teuton/utils/result/ext_compare.rb', line 30

def neq(external)
  @expected = "Not equal to #{external}"

  case external.class.to_s
  when "Fixnum"
    internal = @content[0].to_i
    puts "[WARN] Fixnum class is deprecated!"
    puts "       Upgrade your Ruby version."
  when "Float"
    internal = @content[0].to_f
  when "Integer"
    internal = @content[0].to_i
  else
    internal = @content[0]
  end
  internal != external
end

#not_find(filter) ⇒ Object Also known as: grep_v



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/teuton/utils/result/ext_filter.rb', line 28

def not_find(filter)
  @alterations << "not_find(#{filter})"
  return self if @content.size.zero?

  case filter.class.to_s
  when "Array"
    filter.each { |i| not_find(i.to_s) }
  when "String" || "Integer"
    @content.reject! { |i| i.include?(filter.to_s) }
  when "Regexp"
    @content.reject! { |i| filter.match(i) }
  end
  self
end

#not_include?(value) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/teuton/utils/result/ext_array.rb', line 23

def not_include?(value)
  @expected = "Not include <#{value}> value"
  !@content[0].include?(value)
end

#ok?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/teuton/utils/result/result.rb', line 61

def ok?
  @exitcode.zero?
end

#resetObject



52
53
54
55
56
57
58
59
# File 'lib/teuton/utils/result/result.rb', line 52

def reset
  @content_backup = []
  @content = []
  @exitcode = -1
  @value = nil
  @expected = nil
  @alterations = []
end

#restoreObject Also known as: restore!



65
66
67
68
69
70
# File 'lib/teuton/utils/result/result.rb', line 65

def restore
  temp = @content_backup.clone
  reset
  @content_backup = temp
  @content = temp.clone
end

#since(filter) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/teuton/utils/result/ext_filter.rb', line 44

def since(filter)
  @alterations << "since(#{filter})"
  return self if @content.size.zero?

  if filter.instance_of? String
    flag = false
    @content.select! do |i|
      flag = true if i.include?(filter.to_s)
      flag
    end
  end
  self
end

#until(filter) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/teuton/utils/result/ext_filter.rb', line 64

def until(filter)
  @alterations << "until(#{filter})"
  return self if @content.size.zero?

  if filter.instance_of? String
    flag = true
    @content.select! do |i|
      flag = false if i.include?(filter.to_s)
      flag
    end
  end
  self
end

#valueObject



73
74
75
# File 'lib/teuton/utils/result/result.rb', line 73

def value
  @content[0]
end