Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
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
So, when we last left off,
we had created these create and
0:00
instances class methods and
0:05
then extended the classes with them when
our inventoriable module was included.
0:07
So now, let's go ahead and
I have included the inventoriable module
0:13
in the pant class and the accessory class.
0:19
So, now lets go ahead and
create some stock counts to work with.
0:24
So, we’re gonna actually create some
items and I’m just gonna paste this in.
0:30
So, we’re gonna sell some Mike
the Frog shirts, some jeans, and
0:34
some belts, and a necklace.
0:39
Now one thing that
you'll notice right here
0:42
is that the necklace does not
have a size associated with it.
0:45
That's gonna come into play very,
very shortly.
0:49
So what we're going to do
is we wanna create a report
0:53
of everything that is currently in stock.
0:57
And we wanna do that at the class level.
1:00
So we would say shirt dot in stock report,
and
1:04
we want that to return a list of
everything that we have in stock.
1:08
Now since it's going to be a class method,
1:12
we're going to put it inside
of our class methods module.
1:14
Now, let's just go ahead and
call this in_stock_report.
1:18
So first we'll print to the screen the
name of the class that we're working with
1:26
which we can just do by typing self.to_s,
in stock report.
1:30
Now what we wanna do is select
every instance that is in stock.
1:38
So, we already have this in stock method,
1:44
wouldn't it be great if we could
just say instances.select,
1:47
instance, and then say it's in stock.
1:53
That would be great, but somehow we
have to get a numerable in here.
2:00
Well, we can do that the same way
that we extended the class methods.
2:04
We can also extend it with enumerable.
2:10
So, when our inventoriable module
gets included at the class level,
2:15
we get all of these class methods and
the enumerable module.
2:20
Since we're getting the enumerable model,
2:24
that means we need to
define our own each method.
2:27
And let's go ahead and
look back at our enumerable work and
2:33
see what we did in that one.
2:36
We just defined a method called each,
grabbed the block,
2:39
and passed it into the players object.
2:44
Well we can do the same thing here,
and just pass it in to the instances.
2:48
So now, whatever it is, if it's shirts,
2:57
we'll just be running
this against the shirts.
3:00
If it's pants, same thing, and
same thing with accessories.
3:03
So we'll just say we're reporting on
these things, so these are reportable,
3:07
and now all we have to
do is print them out.
3:14
We've gone through and selected them, so
3:17
we know we've got the ones
that are in stock.
3:19
And, what we'll do is
something kind of interesting.
3:25
We're just gonna iterate through them and
we're gonna create
3:28
an array of what we wanna report on and
then print that array out.
3:32
But, what we wanna do is, also remember
how the size wasn't necessarily in here?
3:37
We're gonna put a little
if statement there.
3:45
This will make a lot more
sense in just a moment.
3:47
We'll say align is an empty array,
and we will push the item name here,
3:50
and we have to grab the attributes for
the name.
3:57
And we will also say stock
is the item's stock count
4:06
Now remember how not every single item has
a size, like this necklace right here?
4:15
We’re not gonna print that out, if
the item doesn’t have the size attribute.
4:21
So, we’ll say if the item attributes,
which is just a hash,
4:26
include the size then we can print it out.
4:32
And now we can just print out the line and
we'll separate it with a tab character.
4:49
And then we'll end this
whole thing with a new line.
4:58
Actually we need to
interpolate that string.
5:03
Okay.
5:06
So now let's go ahead and call this and
we'll do an in stock report for
5:08
our shirts.
5:13
And let's just see what happens.
5:16
I'm going to make this
a little bit bigger.
5:18
Uh-oh.
5:20
Syntax error, expecting keyword_end.
5:22
Now when that happens, it usually
means that we forgot an end somewhere.
5:25
So let's go ahead and
see if we can figure that out.
5:30
All right, it looks like we needed
a little if statement here.
5:34
Let me clear the screen and
run that again.
5:38
Okay.
5:41
And that worked just
like we expected it to.
5:42
We have an In Stock Report for our shirts.
5:44
So let's go ahead and
do the same thing for pants, and
5:46
for our accessories as well.
5:52
I'm gonna clear my screen and
run this again.
5:58
Make this a little bit bigger so
that we can see.
6:02
Clear one more time.
6:03
Okay, we have our Shirt In Stock Report,
our Pant In Stock Report,
6:06
and our Accessory In Stock Report.
6:10
And you'll notice for our necklace item,
this does not list the size,
6:14
which is exactly what we wanted.
6:18
In our next video, we're going to
clean this up just a little bit.
6:20
You need to sign up for Treehouse in order to download course files.
Sign up