Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 295 lines (267 sloc) 9.041 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 51 52 53 54 55 56 57 58 59 60 61 62 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
class Queue
  HEADERS = [:last_name, :first_name, :email_address,
             :zipcode, :state, :city, :street, :homephone]

  def find(attendees, attribute, criteria)
    if attendees
      @data_queue = attendees.select do |attendee|
        data = attendee.send(attribute.intern).downcase.strip
        data == criteria.downcase.strip
      end
      puts "query snatched #{@data_queue.length} records"
    else
      puts ""
      puts 'stop trying to break me, you didnt load data to search from'
      puts ""
    end
  end

  def find_and(attendees, attribute1, criteria1, attribute2, criteria2)
    if attendees
      @data_queue1 = attendees.select do |attendee|
        data = attendee.send(attribute1.intern).downcase.strip
        data == criteria1.downcase.strip
      end
      @data_queue = @data_queue1.select do |attendee|
        data = attendee.send(attribute2.intern).downcase.strip
        data == criteria2.downcase.strip
      end
      puts "query snatched #{@data_queue.length} records"
    else
      puts ""
      puts 'stop trying to break me, you didnt load data to search from'
      puts ""
    end
  end

  def find_minus(attendees, attribute, criteria)
    if attendees && @data_queue != [] && @data_queue
      @data_queue_subtract = @data_queue.select do |attendee|
        data = attendee.send(attribute.intern).downcase.strip
        data == criteria.downcase.strip
      end
      @data_queue = @data_queue - @data_queue_subtract
      subt = @data_queue_subtract.length
      length = @data_queue.length
      puts "subtracted #{subt } to total #{length}"
    else
      puts "I dont have queue to subtract from"
    end
  end

  def find_plus(attendees,attribute, criteria)
    if attendees && @data_queue != [] && @data_queue
      @data_queue_add = attendees.select do |attendee|
        data = attendee.send(attribute.intern).downcase.strip
        data == criteria.downcase.strip
      end
      @original_records =@data_queue.length
      @data_queue = @data_queue | @data_queue_add
      addnum = @data_queue.length - @original_records

      puts "adds #{addnum} to total #{@data_queue.length}"
    else
      puts "no queue to add to, can only add or subtract when you have a queue"
    end
  end

  def queue_clear
    if @data_queue
      @data_queue.clear
      puts "the queue is now clear: #{ @data_queue.length } records"
    else
      puts 'there is no current queue'
    end
  end

  def queue_length
    if @data_queue
      puts "your queue is stashing #{ @data_queue.length } records"
    else
      puts 'there is no current queue- so the count is 0'
    end
  end

  def queue_print_by(attribute)
    if @data_queue
      @data_queue.sort_by! { |attendee| attendee.send(attribute.intern)}
      queue_print
    else
      puts 'there is no current queue'
    end
  end

  def queue_print
    if @data_queue && @data_queue!= []
      last_name_length
      first_name_length
      email_address_length
      state_length
      city_length
      address_length
      zipcode_length
      homephone_length

      puts "#{ 'LASTNAME'.ljust(@last_name_col, " ")} " +
           "#{'FIRSTNAME'.ljust(@first_name_col, " ")} " +
           "#{'EMAIL'.ljust(@email_col, " ")} " +
           "#{'ZIPCODE'.ljust(@zipcode_col, " ")} " +
           "#{'STATE'.ljust(@state_col, " ")} "+
           "#{'CITY'.ljust(@city_col, " ")} "+
           "#{'ADDRESS'.ljust(@address_col, " ")} "+
           "#{'PHONE'.ljust(@homephone_col, " ")}"
      if @data_queue.length <10
        @data_queue.each do |attendee|
          puts "#{attendee.last_name.to_s.ljust(@last_name_col, " ")} "+
               "#{attendee.first_name.to_s.ljust(@first_name_col, " ")} "+
               "#{attendee.email_address.to_s.ljust(@email_col, " ")} "+
               "#{attendee.zipcode.to_s.ljust(@zipcode_col, " ")} "+
               "#{attendee.state.to_s.ljust(@state_col, " ")} "+
               "#{attendee.city.to_s.ljust(@city_col, " ")} "+
               "#{attendee.street.to_s.ljust(@address_col, " ")} "+
               "#{attendee.homephone.to_s.ljust(@homephone_col, " ")}"
        end
        puts ""
        puts ""
      else
        @data_queue.each_with_index do |attendee, i|
          break if i ==10
          puts "#{attendee.last_name.to_s.ljust(@last_name_col, " ")} "+
               "#{attendee.first_name.to_s.ljust(@first_name_col, " ")} "+
               "#{attendee.email_address.to_s.ljust(@email_col, " ")} "+
               "#{attendee.zipcode.to_s.ljust(@zipcode_col, " ")} "+
               "#{attendee.state.to_s.ljust(@state_col, " ")} "+
               "#{attendee.city.to_s.ljust(@city_col, " ")} "+
               "#{attendee.street.to_s.ljust(@address_col, " ")} "+
               "#{attendee.homephone.to_s.ljust(@homephone_col, " ")}"
        end
        puts ""
        puts "printed 10 records,hit enter or space for rest or any key to end"
        char = get_character

        if char == 32 || char ==13
          @data_queue.each_with_index do |attendee, i|
            next if i <10
            puts "#{attendee.last_name.to_s.ljust(@last_name_col, " ")} "+
                 "#{attendee.first_name.to_s.ljust(@first_name_col, " ")} "+
                 "#{attendee.email_address.to_s.ljust(@email_col, " ")} "+
                 "#{attendee.zipcode.to_s.ljust(@zipcode_col, " ")} "+
                 "#{attendee.state.to_s.ljust(@state_col, " ")} "+
                 "#{attendee.city.to_s.ljust(@city_col, " ")} "+
                 "#{attendee.street.to_s.ljust(@address_col, " ")} "+
                 "#{attendee.homephone.to_s.ljust(@homephone_col, " ")}"
          end
          puts ""
        else
          puts 'thanks for querying'
          puts ""
        end
      end

    elsif @data_queue == []
      puts "#{ 'LASTNAME'.ljust(20, " ")} "+
           "#{'FIRSTNAME'.ljust(15, " ")} "+
           "#{'EMAIL'.ljust(43, " ")} "+
           "#{'ZIPCODE'.ljust(10, " ")} "+
           "#{'STATE'.ljust(10, " ")} "+
           "#{'CITY'.ljust(14, " ")} "+
           "#{'ADDRESS'.ljust(35, " ")}"+
           "#{'PHONE'.ljust(12, " ")}"
      puts "this is where records would be, but you have none \n\n"
    else
      puts "calm down buddy, you haven't even made a queue yet"
    end
  end

  def last_name_length
    if @data_queue
      last_name_lengths = @data_queue.collect do |attendee|
        attendee.last_name.length
      end
      @last_name_col = last_name_lengths.sort.last
      @last_name_col = @last_name_col + 5
    else
      @last_name_col = 20
    end
  end

  def first_name_length
    if @data_queue
      first_name_lengths = @data_queue.collect do |attendee|
        attendee.first_name.length
      end
      @first_name_col = first_name_lengths.sort.last
      @first_name_col = @first_name_col +5
    else
      @first_name_col = 20
    end
  end

  def email_address_length
    if @data_queue
      email_address_lengths = @data_queue.collect do |attendee|
        attendee.email_address.to_s.length
      end
      @email_col = email_address_lengths.sort.last
      @email_col = @email_col +5
    else
      @email_col = 41
    end
  end

  def city_length
    if @data_queue
      city_lengths = @data_queue.collect do |attendee|
        attendee.city.to_s.length
      end
      @city_col = city_lengths.sort.last
      @city_col = @city_col + 5
    else
      @city_col = 8
    end
  end

  def state_length
    if @data_queue
      state_lengths = @data_queue.collect do |attendee|
        attendee.state.to_s.length
      end
      @state_col = state_lengths.sort.last
      @state_col = @state_col +5
    else
      @state_col = 10
    end
  end

  def zipcode_length
    if @data_queue
      zipcode_lengths = @data_queue.collect do |attendee|
        attendee.zipcode.to_s.length
      end
      @zipcode_col = zipcode_lengths.sort.last
      @zipcode_col = @zipcode_col +5
    else
      @zipcode_col = 8
    end
  end

  def address_length
    if @data_queue
      address_lengths = @data_queue.collect do |attendee|
        attendee.street.to_s.length
      end
      @address_col = address_lengths.sort.last
      @address_col = @address_col +5
    else
      @address_col = 30
    end
  end

  def homephone_length
    if @data_queue
      phone_lengths = @data_queue.collect do |attendee|
        attendee.homephone.to_s.length
      end
      @homephone_col = phone_lengths.sort.last
      @homephone_col = @homephone_col +5
    else
      @homephone_col = 30
    end
  end

  def output_data(filename)
    if @data_queue
      output = CSV.open(filename,"w")
        output << HEADERS.each_with_index{|header|}
      @data_queue.each do |line|
        output << HEADERS.collect{|header| line.send(header)}
      end
      output.close
      puts "saved the queue as #{ filename }"

    elsif
        output = CSV.open(filename,"w")
        output << HEADERS.each_with_index{|header|}
        output.close
        puts "I saved the queue as #{ filename }, but theres nothing in there!"
    end
  end
end





Something went wrong with that request. Please try again.