Class: LeasesFile

Inherits:
Object show all
Defined in:
lib/fission.old/leasesfile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ LeasesFile

Returns a new instance of LeasesFile


20
21
22
23
24
# File 'lib/fission.old/leasesfile.rb', line 20

def initialize(filename)
  @filename=filename
  @leases=Array.new
  load
end

Instance Attribute Details

#leasesObject (readonly)

Returns the value of attribute leases


18
19
20
# File 'lib/fission.old/leasesfile.rb', line 18

def leases
  @leases
end

Instance Method Details

#all_leasesObject


52
53
54
# File 'lib/fission.old/leasesfile.rb', line 52

def all_leases
  return @leases
end

#current_leasesObject


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fission.old/leasesfile.rb', line 56

def current_leases
  hash_list=Hash.new
  @leases.each do |lease|
    hash_list[lease.name]=lease
  end
  collapsed_list=Array.new
  hash_list.each do |key,value|
    collapsed_list << value
  end
  return collapsed_list
end

#find_lease_by_mac(mac) ⇒ Object


68
69
70
71
72
# File 'lib/fission.old/leasesfile.rb', line 68

def find_lease_by_mac(mac)
  matches=current_leases.select{|l| l.mac==mac}
  return nil if matches.nil?
  return matches[0]
end

#loadObject


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fission.old/leasesfile.rb', line 26

def load
  @leases=Array.new
  File.open(@filename,"r") do |leasefile|
    lease=nil
    while (line = leasefile.gets)

      line=line.lstrip.gsub(';','')
      case line
      when /^lease/
        @leases << lease unless lease.nil?
        name=line.split(' ')[1]
        lease=Lease.new(name)
      when /^hardware/
        lease.mac=line.split(" ")[2]
      when /^starts/
        lease.start=DateTime.parse(line.split(" ")[2..3].join(" "))
      when /^ends/
        lease.end=DateTime.parse(line.split(" ")[2..3].join(" "))
      end

    end
    @leases << lease unless lease.nil?
  end
  return @leases
end