Class: Requests

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

Instance Method Summary collapse

Constructor Details

#initializeRequests

Returns a new instance of Requests.



11
12
13
14
15
16
17
18
19
# File 'lib/requests.rb', line 11

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

Instance Method Details

#add(user, date, days, num, image, comment) ⇒ Object



34
35
36
37
38
39
# File 'lib/requests.rb', line 34

def add user, date, days, num, image, comment
  new_req = {:req_id=>"#{id_next}", :user_id=>"#{user}", :date_reqd=>"#{date}", :days_reqd=>"#{days}", :num_machines=>"#{num}", :image=>"#{image}", :comments=>"#{comment}"}
  @requests.push new_req
  save 
  count
end

#countObject



25
26
27
# File 'lib/requests.rb', line 25

def count
  @requests.count
end

#entry_to_hash(line) ⇒ Object



29
30
31
32
# File 'lib/requests.rb', line 29

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

#fieldsObject



21
22
23
# File 'lib/requests.rb', line 21

def fields
  [:req_id, :user_id, :date_reqd, :days_reqd, :num_machines, :image, :comments]
end

#get(r) ⇒ Object



80
81
82
# File 'lib/requests.rb', line 80

def get r
  @requests.find { |q| q[:req_id] == r } 
end

#get_allObject



76
77
78
# File 'lib/requests.rb', line 76

def get_all
  @requests
end

#id_nextObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/requests.rb', line 50

def id_next
  last_id = 0
  @requests.collect do |line|
    curr_id = line[:req_id]
    if curr_id.to_i > last_id
      last_id = curr_id.to_i
    end
  end
  last_id+1
end

#lastObject



61
62
63
# File 'lib/requests.rb', line 61

def last
  @requests[-1]
end

#req_date_range(r) ⇒ Object



84
85
86
87
88
89
# File 'lib/requests.rb', line 84

def req_date_range r
   item = get(r)
   days = item[:days_reqd].to_i
   start = Date.strptime(item[:date_reqd], '%m-%d-%Y')
   range = start..(start+days)
end

#saveObject



41
42
43
44
45
46
47
48
# File 'lib/requests.rb', line 41

def save 
  f = File.open(RFILE, "w")
  @requests.collect do |line|
    f.puts "#{line[:req_id]}|#{line[:user_id]}|#{line[:date_reqd]}|#{line[:days_reqd]}|#{line[:num_machines]}|#{line[:image]}|#{line[:comments]}"
  end
  f.close
  count
end

#showObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/requests.rb', line 65

def show
  puts "req id | user id | date | # days | # machines | image | comments"
  @requests.collect do |line|
    puts "#{line[:req_id]} | #{line[:user_id]} | #{line[:date_reqd]} | #{line[:days_reqd]} | #{line[:num_machines]} | #{line[:image]} | #{line[:comments]}"
    #if !line[:comments].nil?
    #  puts "  comments:  #{line[:comments]}"
    #end
  end 
  count
end