#!/usr/bin/ruby # Placed into a module for testing # Step 5 - methods to update the category column module Categories def find_category(sql_row) chairs = sql_row['number_of_chairs'].to_i if sql_row['post_code'].start_with?("LS1 ") case(chairs) when 0..10 category="LS1 Small" when 11..100 category="LS1 Medium" when 101..1000 category="LS1 Large" else category="Not a supported number" end elsif sql_row['post_code'].start_with?("LS2 ") case(chairs) when 0..10 category="LS2 Small" when 11..100 category="LS2 Medium" when 101..1000 category="LS2 Large" else category="Not a supported number" end else category="other" end category end def category_update(con) begin # Run our query rows = con.sync_exec "SELECT * FROM street_cafes" # Check the results rows.each do |row| category = find_category(row) con.exec "UPDATE street_cafes SET category='#{category}' WHERE id=#{row['id']}" puts "%s %s" % [ row['cafe'], row['post_code']] end rescue PG::Error => e puts e.message ensure rows.clear if rows end end end