Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
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 up-
gene c
13,630 PointsWhy are all instances declared and assigned to the same name 'shirt' or 'pant' instead of 'shirt1', 'shirt2' etcetc?
Posted by gene cgene c
13,630 Points1 Answer
-
Sergio Cruz
15,550 PointsWhy is it that in the in_stock? method the stock_count is defined without an @?
Posted by Sergio CruzSergio Cruz
15,550 Points3 Answers
-
Daniel Lieberman
7,813 Points6 Answers
View all discussions for this video
Related 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