Skip to content

Instantly share code, notes, and snippets.

@cdimartino
Created May 5, 2015 14:54
Show Gist options
  • Save cdimartino/498ed0f1292c37270cde to your computer and use it in GitHub Desktop.
Save cdimartino/498ed0f1292c37270cde to your computer and use it in GitHub Desktop.
Inheritance/Composition
module DBCLibrary
module Barcodable
attr_reader :location
attr_accessor :barcode
end
end
module DBCLibrary
class BluRay < LibraryItem
include Borrowable, Reservable
end
end
module DBCLibrary
class Book < LibraryItem
include Borrowable, Reservable
end
end
module DBCLibrary
module Borrowable
def borrow! who
@borrowed_by = who
end
def borrowed?
!!@borrowed_by
end
def return!
@borrowed_by = nil
end
end
end
# Computers cannot be borrowed
module DBCLibrary
class Computer < LibraryItem
include Reservable
end
end
module DBCLibrary
class Library
attr_reader :books, :librarian
def initialize args={}
@books = args[:books]
@librarian = args[:librarian]
end
end
end
module DBCLibrary
class LibraryItem
include Barcodable
def initialize args={}
@title = args[:title]
@description = args[:description]
end
end
end
module DBCLibrary
class Magazine < LibraryItem
include Borrowable, Reservable
end
end
module DBCLibrary
class Photocopier
include Barcodable
end
end
# Reference books cannot be borrowed
module DBCLibrary
class ReferenceBook < LibraryItem
include Reservable
end
end
module DBCLibrary
module Reservable
def reserve! who
@reserved_by << who
end
def reserved?
@reserved_by.size > 0
end
def unreserve! who
@reserved_by.delete who
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment