Class: Machines

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

Instance Method Summary collapse

Constructor Details

#initializeMachines

Returns a new instance of Machines.



12
13
14
15
16
17
18
19
20
21
# File 'lib/machines.rb', line 12

def initialize
  #puts  "in initialize"
  #puts "#{MFILE}"
  lines = File.open(MFILE, 'r').readlines
  @entries = lines[0..-1]
  @machines = @entries.collect do |line|
    entry_to_hash line.chomp
  end
  count
end

Instance Method Details

#add(id) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/machines.rb', line 36

def add id
  f = File.open(MFILE, "a")
  @machines.collect do |line|
    if (id == line[:mach_id]) 
      puts "machine already exists, not added"
      break
    end
  end
  f.write "#{id}|"
  f.close
  initialize
  # @machines.push "#{id}|"
end

#add_req(machine_name, request_id) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/machines.rb', line 95

def add_req machine_name, request_id
  f = File.open(MFILE, "w")
  @machines.collect do |line|
    if line[:mach_id] != machine_name
      f.puts "#{line[:mach_id]}|#{line[:req_id]}"
    else
      # update the request id field
      if line[:req_id] == nil
        new_r_id = request_id 
      else
        new_r_id = "#{line[:req_id]}, #{request_id}"
      end
      f.puts "#{line[:mach_id]}|#{new_r_id}"
    end
  end
  f.close
  Machines.new.get machine_name
end

#countObject



32
33
34
# File 'lib/machines.rb', line 32

def count
  @machines.count
end

#entry_to_hash(line) ⇒ Object



27
28
29
30
# File 'lib/machines.rb', line 27

def entry_to_hash line
  values = line.split("|")
  Hash[fields.zip values]
end

#fieldsObject



23
24
25
# File 'lib/machines.rb', line 23

def fields
  [:mach_id, :req_id]
end

#find_free(date, days) ⇒ Object



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/machines.rb', line 63

def find_free date, days
  desired_range = Date.strptime(date, '%m-%d-%Y')..(Date.strptime(date, '%m-%d-%Y') + days.to_i)
  # puts "desired_range: #{desired_range}"
  free_machines = Array.new
  @machines.collect do |line|
    # puts line
    if line[:req_id].nil? 
      free_machines.push line[:mach_id]  
    else
      avail = true
      r = line[:req_id].split(", ")
      # puts "r: #{r}"
      # account for multiple requests being fulfilled by a given machine
      r.collect do |rq|
        # puts "rq: #{rq}"
        rq_range = Requests.new.req_date_range rq
        # puts "rq_range: #{rq_range}"
        rq_range.map do |date| 
          if desired_range === date
            avail = false 
          end
        end
        # puts "avail: #{avail}"
      end
      if avail == true
        free_machines.push line[:mach_id]  
      end
    end
  end
  free_machines
end

#get(m) ⇒ Object



114
115
116
# File 'lib/machines.rb', line 114

def get m
  @machines.find { |i| i[:mach_id] == m }
end

#saveObject



50
51
52
53
54
55
56
# File 'lib/machines.rb', line 50

def save
  f = File.open(MFILE, "w")
  @machines.collect do |line|
    f.puts "#{line[:mach_id]}|#{line[:req_id]}"
  end
  f.close
end

#showObject



58
59
60
61
# File 'lib/machines.rb', line 58

def show
  puts @machines
  count 
end