Class: Threshold::Thresholds

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/threshold/thresholds.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thresholds = []) ⇒ Thresholds

Returns a new instance of Thresholds.



17
18
19
# File 'lib/threshold/thresholds.rb', line 17

def initialize(thresholds = [])
  @thresholds = thresholds
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



13
14
15
# File 'lib/threshold/thresholds.rb', line 13

def file
  @file
end

#readonlyObject

Returns the value of attribute readonly.



13
14
15
# File 'lib/threshold/thresholds.rb', line 13

def readonly
  @readonly
end

Instance Method Details

#flushObject

Write changes to the file



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/threshold/thresholds.rb', line 22

def flush
  begin 
    valid_existing_file?(@file)
    raise ReadOnlyThresholdsFile if @readonly
    hash = current_hash
    
    file = File.open(@file, 'w+')
    raise ThresholdAtomicLockFailure, 'The @file state/hash changed before we could flush the file' unless stored_hash == hash
    file.write self.sort.to_s
    file.close

  rescue NonExistantThresholdFile
    raise ReadOnlyThresholdsFile if @readonly

    file = File.open(@file, 'w')
    file.write self.sort.to_s
    file.close
  end
  return true 
end

#loadfileObject

Append in the thresholds.conf file to current collection



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/threshold/thresholds.rb', line 50

def loadfile
  valid_existing_file?(@file)

  results = Threshold::Parser.new(@file)
  @stored_hash= results.filehash
  #puts stored_hash
  results.caps.each do |result|
     builder = Threshold::Builder.new(result)
     self << builder.build
  end

end

#loadfile!Object

Clears current collection and Read in the thresholds.conf file



44
45
46
47
# File 'lib/threshold/thresholds.rb', line 44

def loadfile!
  @thresholds.clear
  loadfile
end

#reject(&blk) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/threshold/thresholds.rb', line 110

def reject(&blk)
  if block_given? 
    Thresholds.new(@thresholds.reject(&blk))
  else
    Thresholds.new(@thresholds.reject)
  end   
end

#reverseObject



102
103
104
# File 'lib/threshold/thresholds.rb', line 102

def reverse
  Thresholds.new(@thresholds.reverse)
end

#select(&blk) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/threshold/thresholds.rb', line 118

def select(&blk)
  if block_given? 
    Thresholds.new(@thresholds.select(&blk))
  else
    Thresholds.new(@thresholds.select)
  end   
end

#shuffleObject



106
107
108
# File 'lib/threshold/thresholds.rb', line 106

def shuffle
  Thresholds.new(@thresholds.shuffle)
end

#sortObject

Array(@thresholds) Creates a new Array on @threshold.sort so.. direct forwardable delegation fails.



98
99
100
# File 'lib/threshold/thresholds.rb', line 98

def sort
  Thresholds.new(@thresholds.sort)
end

#stored_hashObject



89
90
91
# File 'lib/threshold/thresholds.rb', line 89

def stored_hash
  @stored_hash
end

#to_s(skip = false) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/threshold/thresholds.rb', line 78

def to_s(skip = false)
  output = ""

  raise InvalidThresholdsObject, "Container object has unknown objects" unless valid?

  self.each do |threshold|
    output << threshold.to_s(skip) + "\n"
  end
  return output
end

#uniq(&blk) ⇒ Object

Uniques by default to printable output



127
128
129
130
131
132
133
# File 'lib/threshold/thresholds.rb', line 127

def uniq(&blk)
  if block_given? 
    Thresholds.new(@thresholds.uniq(&blk))
  else
    Thresholds.new(@thresholds.uniq{ |lineitem| lineitem.to_s(true) })
  end   
end

#valid?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/threshold/thresholds.rb', line 63

def valid?
  begin 
    self.each do |threshold| 
      if threshold.respond_to?(:valid?)
        return false unless threshold.valid?
      else
        raise InvalidThresholdsObject, "Container object has unknown objects"
      end
    end
    return true
  rescue InvalidThresholdsObject
    return false
  end
end