Skip to content

Instantly share code, notes, and snippets.

@ciprianna
Last active August 29, 2015 14:23
Show Gist options
  • Save ciprianna/15f97c542ed91574ee1f to your computer and use it in GitHub Desktop.
Save ciprianna/15f97c542ed91574ee1f to your computer and use it in GitHub Desktop.
Shoe Inventory - ORM
# Driver
require "sqlite3"
require_relative "menu_module.rb"
require_relative "validity_module.rb"
require_relative "database_class_methods.rb"
require_relative "database_instance_methods.rb"
require_relative "shoes.rb"
require_relative "location.rb"
require_relative "category.rb"
# Creates the database connection
DATABASE = SQLite3::Database.new("shoe_inventory.db")
# Creates the table
DATABASE.execute("CREATE TABLE IF NOT EXISTS shoes (id INTEGER PRIMARY KEY, name TEXT NOT NULL, cost INTEGER NOT NULL, color TEXT NOT NULL, category_id INTEGER, location_id INTEGER, location_stock INTEGER);")
DATABASE.execute("CREATE TABLE IF NOT EXISTS categories (id INTEGER PRIMARY KEY, name TEXT);")
DATABASE.execute("CREATE TABLE IF NOT EXISTS locations (id INTEGER PRIMARY KEY, name TEXT);")
# Returns the results as a Hash
DATABASE.results_as_hash = true
################################################################################
# Main menu in ux to get an initial choice from the user
choice = Menu.main
# Begins the loop when a correct input from the user has been entered.
# If zero, it skips the loop entirely and exits the program.
while choice != 0
##### Displays all products-----------------------------------------------------
if choice == 1
Shoe.all.each do |shoe_object|
puts "ID: #{shoe_object.id}, Name: #{shoe_object.name}, Cost: #{shoe_object.cost}, Color: #{shoe_object.color}, Category: #{shoe_object.category_id}, Location: #{shoe_object.location_id}, Quantity: #{shoe_object.location_stock}"
end
end
##### Quantity information menu; gives a list of sub-options--------------------
if choice == 2
quantity_choice = Menu.quantity
while quantity_choice != 0
##### Views all stock quantities------------------------------------------
if quantity_choice == 1
Shoe.all.each do |shoe_object|
puts "#{shoe_object.id} - #{shoe_object.name} (#{shoe_object.location_stock})"
end
puts "Total stock quantity - #{Shoe.total_stock}"
end
##### Shows all items where_quantity_is_low-------------------------------
if quantity_choice == 2
Shoe.where_quantity_is_low.each do |shoe_object|
puts "#{shoe_object.id} - #{shoe_object.name} (#{shoe_object.location_stock})"
end
end
##### Updates an item's quantity------------------------------------------
if quantity_choice == 3
puts "Which product quantity would you like to update?"
quantity_range = Menu.list_all_store_range(Shoe)
print ">> "
shoe_to_change = gets.to_i
shoe_to_change = Valid.response_check(quantity_range, shoe_to_change)
shoe = Shoe.find(shoe_to_change)
puts "Okay, and how many are you adding? If removing quantity, enter a negative number."
print ">> "
change = gets.to_i
shoe.update_quantity(change)
if shoe.save_valid
puts "Quantity updated"
else
puts "Quantity update failed."
end
end
##### Re-asks for the menu options----------------------------------------
quantity_choice = Menu.quantity
end
end
##### Adds a new item to the inventory------------------------------------------
if choice == 3
puts "Okay, there's a new shoe to add to the inventory. Press anything to continue or type '0' to exit."
continue = gets.chomp
while continue != '0'
new_shoe_object = Shoe.new
puts "Shoe name:"
print ">> "
new_shoe_object.name = gets.chomp
puts "Cost:"
print ">> "
new_shoe_object.cost = gets.to_f
puts "Color:"
print ">> "
new_shoe_object.color = gets.chomp
puts "Category:"
category_range = Menu.list_all_store_range(Category)
print ">> "
category_id = gets.to_i
new_shoe_object.category_id = Valid.response_check(category_range, category_id)
puts "Storage location:"
location_range = Menu.list_all_store_range(Location)
print ">> "
location_id = gets.to_i
new_shoe_object.location_id = Valid.response_check(location_range, location_id)
puts "Quantity:"
print ">> "
new_shoe_object.location_stock = gets.to_i
if new_shoe_object.add_to_database
puts "Product added to inventory."
else
puts "Product could not be added - Missing Information."
end
puts "Press anything to add another new shoe to the Inventory. Otherwise, type '0' to exit."
continue = gets.chomp
end
end
##### Updates a product's information-------------------------------------------
if choice == 4
puts "Which product would you like to update?"
shoe_range = Menu.list_all_store_range(Shoe)
print ">> "
shoe = gets.to_i
shoe = Valid.response_check(shoe_range, shoe)
shoe_to_change = Shoe.find(shoe)
##### Displays all information pertaining to the selected shoe--------------
puts "ID: #{shoe_to_change.id}, Name: #{shoe_to_change.name}, Cost: #{shoe_to_change.cost}, Color: #{shoe_to_change.color}, Category: #{shoe_to_change.category_id}, Location: #{shoe_to_change.location_id}, Quantity: #{shoe_to_change.location_stock}"
##### Sub-menu for updating-------------------------------------------------
40.times {print "-"}
puts "\n"
puts "What would you like to update?"
to_update = Menu.update_product
##### Begins loop once a valid input is entered; if zero, exits the sub-menu
while to_update != 0
##### Updates the name of the shoe----------------------------------------
if to_update == 1
puts "What is the new name for this shoe?"
print ">> "
new_name = gets.chomp
shoe_to_change.name = new_name
end
##### Updates the cost of the shoe----------------------------------------
if to_update == 2
puts "What is the new cost for this shoe?"
print ">> "
new_cost = gets.to_f
shoe_to_change.cost = new_cost
end
##### Updates the color of the shoe---------------------------------------
if to_update == 3
puts "What is the new color of the shoe?"
print ">> "
new_color = gets.chomp
shoe_to_change.color = new_color
end
##### Updates the category_id of the shoe---------------------------------
if to_update == 4
puts "What is the new category of the shoe?"
category_range = Menu.list_all_store_range(Category)
print ">> "
new_category_id = gets.to_i
new_category_id = Valid.response_check(category_range, new_category_id)
shoe_to_change.category_id = new_category_id
end
##### Updates the location_id of the shoe---------------------------------
if to_update == 5
puts "What location is this shoe moving to?"
location_range = Menu.list_all_store_range(Location)
print ">> "
new_location_id = gets.to_i
new_location_id = Valid.response_check(locaiton_range, new_location_id)
shoe_to_change.location_id = new_location_id
end
##### Re-asks what option the user would like to choose-------------------
40.times {print "-"}
puts "\n"
puts "Is there anything else to update for this product?"
to_update = Menu.update_product
end
if shoe_to_change.save_valid
puts "Product update successful."
else
puts "Product update failed to save."
end
end
##### Displays information by pricing category----------------------------------
if choice == 5
puts "Which pricing category would you like to see?"
puts "High - $100+"
puts "Medium - $50-$99"
puts "Low - $0-$49"
print ">> "
pricing_category = gets.chomp.downcase
while (pricing_category != "high") && (pricing_category != "medium") && (pricing_category != "low")
puts "Please select 'high', 'medium', or 'low':"
print ">> "
pricing_category = gets.chomp.downcase
end
shoes_by_price = Shoe.where_cost(pricing_category)
shoes_by_price.each do |shoe|
puts "ID: #{shoe.id}, Name: #{shoe.name}, Cost: #{shoe.cost}, Color: #{shoe.color}, Category: #{shoe.category_id}, Location: #{shoe.location_id}, Quantity: #{shoe.location_stock}"
end
end
##### Displays options for the locations menu-----------------------------------
if choice == 6
location_choice = Menu.locations
##### Begins loop once a valid input is entered; if zero, exits the sub-menu
while location_choice != 0
##### Displays all locations----------------------------------------------
if location_choice == 1
puts "Locations:"
Menu.list_all(Location)
end
##### Displays all products at an instance of a location------------------
if location_choice == 2
puts "Which location would you like to view products at?"
location_range = Menu.list_all_store_range(Location)
print ">> "
location = gets.to_i
location = Valid.response_check(location_range, location)
location_to_view = Location.find(location)
location_to_view.shoes.each do |shoes|
puts "#{shoes.id} - #{shoes.name} (#{shoes.location_stock})"
end
end
##### Changes the name of a location--------------------------------------
if location_choice == 3
puts "Which location would you like to change the name of?"
location_range = Menu.list_all_store_range(Location)
print ">> "
location = gets.to_i
location = Valid.response_check(location_range, location)
location_to_change = Location.find(location)
puts "What is the new name of this location?"
print ">> "
new_location_name = gets.chomp
location_to_change.name = new_location_name
if location_to_change.save_valid
puts "Location name changed."
else
puts "Name change failed."
end
end
##### Adds a new location-------------------------------------------------
if location_choice == 4
puts "What's the name of the new location?"
print ">> "
new_location = gets.chomp
location_to_add = Location.new
location_to_add.name = new_location
if location_to_add.add_to_database
puts "Location added."
else
puts "Failed to add location."
end
end
##### Deletes a location, after checking to ensure nothing is stored there
if location_choice == 5
puts "Which location would you like to delete?"
location_range = Menu.list_all_store_range(Location)
print ">> "
location = gets.to_i
location = Valid.response_check(location_range, location)
location_to_delete = Location.find(location)
if location_to_delete.delete_location
puts "Location deleted"
else
puts "Cannot delete this location while shoes are stored here."
location_to_delete.shoes.each do |shoes|
puts "#{shoes.id} - #{shoes.name} (#{shoes.location_stock})"
end
end
end
##### Re-asks the user to input an option---------------------------------
location_choice = Menu.locations
end
end
##### Displays the options in the Category Menu---------------------------------
if choice == 7
category_choice = Menu.categories
##### Begins loop once a valid input is entered; if zero, exits the sub-menu
while category_choice != 0
##### Displays all categories---------------------------------------------
if category_choice == 1
puts "Categories:"
Menu.list_all(Category)
end
##### Displays all products in a category instance------------------------
if category_choice == 2
puts "Which category would you like to view products in?"
category_range = Menu.list_all_store_range(Category)
print ">> "
category = gets.to_i
category = Valid.response_check(category_range, category)
category_to_view = Category.find(category)
category_to_view.shoes.each do |shoes|
puts "#{shoes.id} - #{shoes.name} (#{shoes.location_stock})"
end
end
##### Changes the name of a category--------------------------------------
if category_choice == 3
puts "Which category would you like to change the name of?"
category_range = Menu.list_all_store_range(Category)
print ">> "
category = gets.to_i
category = Valid.response_check(category_range, category)
category_to_change = Category.find(category)
puts "What is the new name of this category?"
print ">> "
new_category_name = gets.chomp
category_to_change.name = new_category_name
if category_to_change.save_valid
puts "Category name changed."
else
puts "Name change failed."
end
end
##### Adds a new category-------------------------------------------------
if category_choice == 4
puts "What's the name of the new category?"
print ">> "
new_category = gets.chomp
category_to_add = Category.new
category_to_add.name = new_category
if category_to_add.add_to_database
puts "Category added."
else
puts "Failed to add category."
end
end
##### Deletes a category after checking to ensure no products are assigned
##### to that category--------------------------------------------------
if category_choice == 5
puts "Which category would you like to delete?"
category_range = Menu.list_all_store_range(Category)
print ">> "
category = gets.to_i
category = Valid.response_check(category_range, category)
category_to_delete = Category.find(category)
if category_to_delete.delete_category
puts "Category deleted"
else
puts "Cannot delete this category while shoes are assigned."
category_to_delete.shoes.each do |shoes|
puts "#{shoes.id} - #{shoes.name} (#{shoes.location_stock})"
end
end
end
##### Re-asks the user for an option--------------------------------------
category_choice = Menu.categories
end
end
##### Deletes a product from the inventory--------------------------------------
if choice == 8
puts "Which product would you like to delete?"
shoe_range = Menu.list_all_store_range(Shoe)
print ">> "
shoe_choice = gets.to_i
shoe_choice = Valid.response_check(shoe_range, shoe_choice)
shoe_to_delete = Shoe.find(shoe_choice)
puts "ID: #{shoe_to_delete.id}, Name: #{shoe_to_delete.name}, Cost: #{shoe_to_delete.cost}, Color: #{shoe_to_delete.color}, Category: #{shoe_to_delete.category_id}, Location: #{shoe_to_delete.location_id}, Quantity: #{shoe_to_delete.location_stock}"
puts "Are you sure you wish to delete this product? (yes/no)"
print ">> "
sure = gets.chomp.downcase
if sure == "yes"
if shoe_to_delete.delete
puts "Shoe successfully deleted."
else
puts "Deletion failed."
end
end
end
##### Re-asks the user for an option--------------------------------------------
choice = Menu.main
end
##### Exit menu message---------------------------------------------
puts "Bye!"
# Category Class
require_relative "database_class_methods.rb"
require_relative "database_instance_methods.rb"
class Category
extend DatabaseClassMethods
include DatabaseInstanceMethods
attr_reader :id
attr_accessor :name
# Assigns an id for identification in instance methods
#
# options - emtpy Hash
# - id (optional) - Integer assigned as the primary key from the id column
# - name (optional) - String
#
# Returns the category object created
def initialize(options = {})
@id = options["id"]
@name = options["name"]
end
# Adds a Category Object to the categories table
#
# Returns Category Object if created, else returns false
def add_to_database
if Valid.name?(self)
self.add
else
return false
end
end
# Reads all shoes in a category object
#
# Returns all shoe information in a category from the shoes table as an Array
# of Shoe Objects. Each Object represents a row with the given category_id.
def shoes
results = DATABASE.execute("SELECT * FROM shoes WHERE category_id = #{@id};")
store_results = []
results.each do |hash|
store_results << Shoe.new(hash)
end
return store_results
end
# Update method for the categories table
#
# Returns true/false Boolean
def save_valid
if Valid.name?(self)
self.save
else
return false
end
end
# Delete a category from the categories table
#
# Returns true/false Boolean
def delete_category
if self.shoes.empty?
self.delete
else
return false
end
end
end
# DatabaseClassMethods
require "active_support"
require "active_support/inflector"
module DatabaseClassMethods
# Selects all information from a database table
#
# Returns an Array of Objects
def all
table = self.to_s.pluralize.underscore
results = DATABASE.execute("SELECT * FROM #{table};")
store_results = []
results.each do |hash|
store_results << self.new(hash)
end
return store_results
end
# Locates an existing row in a table
#
# record_id - Integer indicating the primary key for the row
#
# Returns an Array containing a Hash for the row
def find(record_id)
table = self.to_s.pluralize.underscore
results = DATABASE.execute("SELECT * FROM #{table} WHERE id = #{record_id};").first
self.new(results)
end
# Creates a new row in a given table in the database
#
# options - Hash containing information about the Object (attributes
# and values)
#
# Returns the new Object
def add(options = {})
columns = options.keys
values = options.values
columns_for_sql = columns.join(", ")
individual_values_for_sql = []
values.each do |value|
if value.is_a?(String)
individual_values_for_sql << "'#{value}'"
else
individual_values_for_sql << value
end
end
values_for_sql = individual_values_for_sql.join(", ")
table = self.to_s.pluralize.underscore
DATABASE.execute("INSERT INTO #{table} (#{columns_for_sql}) VALUES (#{values_for_sql});")
id = DATABASE.last_insert_row_id
options["id"] = id
self.new(options)
end
end
# DatabaseInstanceMethods
require "active_support"
require "active_support/inflector"
module DatabaseInstanceMethods
# Reads a specific field for a given row in a table
#
# field - String for the column name to read
#
# Returns value of field
def get(field)
table = self.to_s.pluralize.underscore
result = DATABASE.execute("SELECT * FROM #{table} WHERE id = #{@id}").first
result[field]
end
# Deletes a row from a table
#
# Returns true/false Boolean
def delete
table = self.class.to_s.pluralize.underscore
DATABASE.execute("DELETE FROM #{table} WHERE id = #{@id};")
end
# Updates/saves a row's information in a table
#
# Returns updated Object
def save
table = self.class.to_s.pluralize.underscore
instance_variables = self.instance_variables
attribute_hash = {}
instance_variables.each do |variable|
attribute_hash["#{variable.slice(1..-1)}"] = self.send("#{variable.slice(1..-1)}")
end
individual_instance_variables = []
attribute_hash.each do |key, value|
if value.is_a?(String)
individual_instance_variables << "#{key} = '#{value}'"
else
individual_instance_variables << "#{key} = #{value}"
end
end
for_sql = individual_instance_variables.join(', ')
DATABASE.execute("UPDATE #{table} SET #{for_sql} WHERE id = #{self.id}")
return self
end
end
# Location Class
require_relative "database_class_methods.rb"
require_relative "database_instance_methods.rb"
class Location
extend DatabaseClassMethods
include DatabaseInstanceMethods
attr_reader :id
attr_accessor :name
# Assigns an id for identification in instance methods
#
# options - Empty Hash
# - id (optional) - Integer assigned as the primary key
# - name (optional) - String
#
# Returns newly created Location Object
def initialize(options = {})
@id = options["id"]
@name = options["name"]
end
# Adds a Location Object to the locations table
#
# Returns Location Object, else returns false
def add_to_database
if Valid.name?(self)
self.add
else
return false
end
end
# Reads all shoes at a location object
#
# Returns all shoe information at one location from the shoes table as an
# Array of Objects. Each Object corresponds to a row of data which is stored
# at the passed id.
def shoes
results = DATABASE.execute("SELECT * FROM shoes WHERE location_id = #{@id};")
store_results = []
results.each do |hash|
store_results << Shoe.new(hash)
end
return store_results
end
# Update method for the locations table
#
# Returns true/false Boolean
def save_valid
if Valid.name?(self)
self.save
else
return false
end
end
# Delete a category row from the categories table
#
# Returns true/false Boolean
def delete_location
if self.shoes.empty?
self.delete
else
return false
end
end
end
# Module for Menu Items
module Menu
# Main menu in ux to get an initial choice from the user
#
# Returns choice - Integer
def self.main
60.times {print "-"}
puts "\n"
puts "What would you like to do with the Cutesie Bootsie Inventory?"
60.times {print "-"}
puts "\n"
puts "1".ljust(10) + "View current stock".rjust(30)
puts "2".ljust(10) + "View quantity information".rjust(30)
puts "3".ljust(10) + "Add new product".rjust(30)
puts "4".ljust(10) + "Update product information".rjust(30)
puts "5".ljust(10) + "View products by cost".rjust(30)
puts "6".ljust(10) + "View location information".rjust(30)
puts "7".ljust(10) + "View category information".rjust(30)
puts "8".ljust(10) + "Delete product".rjust(30)
puts "0".ljust(10) + "Exit Cutesie Bootsie Inventory".rjust(30)
print ">> "
choice = gets.to_i
range = [1, 2, 3, 4, 5, 6, 7, 8, 0]
Valid.response_check(range, choice)
return choice
end
# Sub-Menu for tasks related to product quantity
#
# Returns quantity_choice - Integer
def self.quantity
40.times {print "-"}
puts "\n"
puts "What would you like to do?"
40.times {print "-"}
puts "\n"
puts "1".ljust(10) + "View all stock quantities".rjust(30)
puts "2".ljust(10) + "View low quantities".rjust(30)
puts "3".ljust(10) + "Update stock quantities".rjust(30)
puts "0".ljust(10) + "Exit quantity information".rjust(30)
print ">> "
quantity_choice = gets.to_i
quantity_choice_range = [0, 1, 2, 3]
Valid.response_check(quantity_choice_range, quantity_choice)
return quantity_choice
end
# Sub-Menu for tasks relating to updating a product
#
# Returns to_update - Integer, menu selection
def self.update_product
40.times {print "-"}
puts "\n"
puts "1".ljust(10) + "Name".rjust(30)
puts "2".ljust(10) + "Cost".rjust(30)
puts "3".ljust(10) + "Color".rjust(30)
puts "4".ljust(10) + "Category".rjust(30)
puts "5".ljust(10) + "Location".rjust(30)
puts "0".ljust(10) + "Exit product update".rjust(30)
print ">> "
to_update = gets.to_i
range_for_updates = [0, 1, 2, 3, 4, 5]
Valid.response_check(range_for_updates, to_update)
return to_update
end
# Sub-Menu for location-related tasks to select.
#
# Returns location_choice - Integer
def self.locations
40.times {print "-"}
puts "\n"
puts "What would you like to do?"
40.times {print "-"}
puts "\n"
puts "1".ljust(10) + "View all locations".rjust(30)
puts "2".ljust(10) + "View all products at a location".rjust(30)
puts "3".ljust(10) + "Change location name".rjust(30)
puts "4".ljust(10) + "Add new location".rjust(30)
puts "5".ljust(10) + "Delete location".rjust(30)
puts "0".ljust(10) + "Exit location information".rjust(30)
print ">> "
location_choice = gets.to_i
range_for_locations = [0, 1, 2, 3, 4, 5]
Valid.response_check(range_for_locations, location_choice)
return location_choice
end
def self.categories
40.times {print "-"}
puts "\n"
puts "What would you like to do?"
40.times {print "-"}
puts "\n"
puts "1".ljust(10) + "View all categories".rjust(30)
puts "2".ljust(10) + "View all products in a category".rjust(30)
puts "3".ljust(10) + "Change category name".rjust(30)
puts "4".ljust(10) + "Add new category".rjust(30)
puts "5".ljust(10) + "Delete category".rjust(30)
puts "0".ljust(10) + "Exit category information".rjust(30)
print ">> "
category_choice = gets.to_i
range_for_categories = [0, 1, 2, 3, 4, 5]
Valid.response_check(range_for_categories, category_choice)
return category_choice
end
# Lists all Objects in a Class like a menu
#
# class_to_list - Class to use the .all method on
#
# Returns a String containing Object information
def self.list_all(class_to_list)
class_to_list.all.each do |object|
puts "#{object.id} - #{object.name}"
end
end
# Lists all Objects in a Class like a menu and stores the ids in an Array.
#
# class_to_list - Class to use the .all method on
#
# Returns the Array of id's - Integers
def self.list_all_store_range(class_to_list)
range = []
class_to_list.all.each do |object|
puts "#{object.id} - #{object.name}"
range << object.id
end
return range
end
end
# Shoe Class
require_relative "database_class_methods.rb"
require_relative "database_instance_methods.rb"
class Shoe
extend DatabaseClassMethods
include DatabaseInstanceMethods
attr_reader :id
attr_accessor :name, :cost, :color, :category_id, :location_id, :location_stock
# Assigns an id for identification in instance methods
#
# options - empty Hash
# - id (optional) - Integer assigned as the primary key from the id column
# - name (optional) - String that is the shoe's name
# - cost (optional) - Integer that represents the cost of the shoe
# - color (optional) - String for the color of the shoe
# - category_id (optional) - Integer, foreign key from the categories table
# - location_id (optional) - Integer, foreign key from the locations table
# - location_stock (optional) - Integer indicating quantity of product
#
# Returns Shoe object created
def initialize(options = {})
@id = options["id"]
@name = options["name"]
@cost = options['cost']
@color = options['color']
@category_id = options['category_id']
@location_id = options['location_id']
@location_stock = options['location_stock']
end
# Adds a Shoe Object to the database.
#
# Returns id of the object if added - Integer, else returns False.
def add_to_database
if Valid.shoe?(self)
self.add
else
false
end
end
# Sums all of the product inventory
#
# Returns the sum of all location_stock values - Integer
def self.total_stock
sum = DATABASE.execute("SELECT SUM(location_stock) FROM shoes;").first
sum = sum[0]
return sum
end
# Shows all products by cost categories
#
# cost_category - String, should be categories of high, medium, or low
#
# Returns an Array containing rows as Shoe objects.
def self.where_cost(cost_category)
if cost_category == "high"
results = DATABASE.execute("SELECT * FROM shoes WHERE cost >= 100;")
elsif cost_category == "medium"
results = DATABASE.execute("SELECT * FROM shoes WHERE cost >= 50 AND cost < 100;")
else
results = DATABASE.execute("SELECT * FROM shoes WHERE cost < 50;")
end
store_results = []
results.each do |hash|
store_results << Shoe.new(hash)
end
return store_results
end
# Reads all products with low quantities in location_stock.
#
# Returns an Array containing rows as Shoe Objects.
def self.where_quantity_is_low
results = DATABASE.execute("SELECT * FROM shoes WHERE location_stock < 5;")
store_results = []
results.each do |hash|
store_results << Shoe.new(hash)
end
return store_results
end
# Updates the shoes table in the database.
#
# Returns true/false Boolean.
def save_valid
if Valid.shoe?(self)
self.save
else
return false
end
end
# Updates the quantity of a product for a given id
#
# to_add - Integer for the number to add to the location_stock value
#
# Returns the location_stock attribute - Integer.
def update_quantity(to_add)
DATABASE.execute("UPDATE shoes SET location_stock = #{@location_stock + to_add} WHERE id = #{@id};").first
@location_stock += to_add
end
end
# Validity Check Module
module Valid
# While loop to check that a response is valid
#
# range - Array of valid choices for the user
# choice - Integer that the user chooses
#
# Returns choice - Integer
def self.response_check(range, choice)
while !range.include?(choice)
puts "Please choose an item from the menu:"
print ">> "
choice = gets.to_i
end
return choice
end
# Method to ensure that all fields are valid in a Shoe Object
#
# object - Shoe Object to check values of
#
# Returns valid - true/false Boolean
def self.shoe?(object)
valid = true
if object.name.nil? || object.name == ""
valid = false
end
if object.cost.nil? || object.cost == ""
valid = false
end
if object.color.nil? || object.color == ""
valid = false
end
if object.location_stock.nil? || object.location_stock == ""
valid = false
end
return valid
end
# Method to ensure that name field is valid for Location/Category Objects.
#
# object - Location or Category Object to check name value of
#
# Returns valid - true/false Boolean
def self.name?(object)
valid = true
if object.name.nil? || object.name == ""
valid = false
end
return valid
end
end
@sumeetjain
Copy link

https://gist.github.com/ciprianna/15f97c542ed91574ee1f#file-category-rb-L81-L95 - I don't think this will ever return false. I could be wrong. But – other than a Ruby error (which would cause the program to halt anyways) – is there a way for the method to return false?

@ciprianna
Copy link
Author

Sumeet, you're right. It didn't serve much purpose as it stood. It was in there as a reminder for me to add in a validity check. Updated now to better reflect that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment