This is a small little module demonstrating Ruby mixins with modules.
Don't use this gist, actually.. Not using it is that easy!
| class Apartment < Dwelling | |
| include Holder | |
| attr_accessor :rent | |
| def initialize(address, rent, roommate_capacity) | |
| super(address) | |
| @rent = rent | |
| set_capacity(roommate_capacity) | |
| end | |
| def roommates | |
| holdables | |
| end | |
| def add_roommate(occupant) | |
| add_holdable(occupant) | |
| end | |
| end |
| class Dwelling | |
| attr_reader :address | |
| def initialize(address) | |
| @address = address | |
| end | |
| end |
| module Holder | |
| def set_capacity(capacity) | |
| @capacity = capacity | |
| end | |
| def full? | |
| holdables.size >= capacity | |
| end | |
| private | |
| attr_reader :capacity | |
| def holdables | |
| @holdables ||= [] | |
| end | |
| def add_holdable(holdable) | |
| holdables << holdable | |
| end | |
| end |
| class Truck | |
| include Holder | |
| def initialize(box_capacity) | |
| set_capacity(box_capacity) | |
| end | |
| def boxes | |
| holdables | |
| end | |
| def add_box(box) | |
| add_holdable(box) | |
| end | |
| end |