Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Now that we know all about our stock counts and what is in stock, we're going to create a report method to display it. We'll keep this method in our `Inventoryable` module so we can use it in all of our classes.
Code Samples
Our store code is coming along! This is the complete version at the end of the video.
module Inventoryable
def self.included(klass)
klass.extend(ClassMethods)
klass.extend(Enumerable)
end
module ClassMethods
def create(attributes)
object = new(attributes)
instances.push(object)
return object
end
def instances
@instances ||= []
end
def each(&block)
instances.each(&block)
end
def in_stock_report
puts "#{self.to_s} In Stock Report"
reportable = instances.select{ |instance| instance.in_stock? }
reportable.each do |item|
line = []
line.push("Item: #{item.attributes[:name]}")
line.push("Stock: #{item.stock_count}")
if item.attributes.include?(:size)
line.push("Size: #{item.attributes[:size]}")
end
puts line.join("\t")
end
puts "\n"
end
end
def stock_count
@stock_count ||= 0
end
def stock_count=(number)
@stock_count = number
end
def in_stock?
stock_count > 0
end
end
class Shirt
include Inventoryable
attr_accessor :attributes
def initialize(attributes)
@attributes = attributes
end
end
class Pant
include Inventoryable
attr_accessor :attributes
def initialize(attributes)
@attributes = attributes
end
end
class Accessory
include Inventoryable
attr_accessor :attributes
def initialize(attributes)
@attributes = attributes
end
end
shirt = Shirt.create(name: "MTF", size: "L")
shirt.stock_count = 10
shirt = Shirt.create(name: "MTF2", size: "L")
shirt = Shirt.create(name: "MTF", size: "M")
shirt.stock_count = 9
pant = Pant.create(name: "Jeans", size: "M")
pant.stock_count = 2
pant = Pant.create(name: "Jeans", size: "S")
pant.stock_count = 4
accessory = Accessory.create(name: "Belt", size: "M")
accessory.stock_count = 1
accessory = Accessory.create(name: "Belt", size: "L")
accessory.stock_count = 1
accessory = Accessory.create(name: "Necklace")
accessory.stock_count = 1
Shirt.in_stock_report
Pant.in_stock_report
Accessory.in_stock_report
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up