File: app/models/cart_item.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  class: CartItem#9
inherits from
  Object ( Builtin-Module )
has properties
attribute: product [R] #11
attribute: quantity [R] #11
method: initialize / 1 #13
method: increment_quantity #18
method: title #22
method: price #26

Class Hierarchy

Object ( Builtin-Module )
  CartItem    #9

Code

   1  #---
   2  # Excerpted from "Agile Web Development with Rails, 3rd Ed.",
   3  # published by The Pragmatic Bookshelf.
   4  # Copyrights apply to this code. It may not be used to create training material, 
   5  # courses, books, articles, and the like. Contact us if you are in doubt.
   6  # We make no guarantees that this code is fit for any purpose. 
   7  # Visit http://www.pragmaticprogrammer.com/titles/rails3 for more book information.
   8  #---
   9  class CartItem
  10 
  11    attr_reader :product, :quantity
  12    
  13    def initialize(product)
  14      @product = product
  15      @quantity = 1
  16    end
  17    
  18    def increment_quantity
  19      @quantity += 1
  20    end
  21    
  22    def title
  23      @product.title
  24    end
  25    
  26    def price
  27      @product.price * @quantity
  28    end
  29  end