Created
November 17, 2009 20:12
-
-
Save santosh79/237229 to your computer and use it in GitHub Desktop.
inherited callback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ShippingOption | |
def self.children | |
@children ||= [] | |
end | |
def self.inherited(child_class) | |
children << child_class | |
end | |
def self.for(weight, international) | |
children.select {|cls| cls.can_ship?(weight, international)} | |
end | |
end | |
class MediaMail < ShippingOption | |
def self.can_ship?(weight, international) | |
!international | |
end | |
end | |
class PriorityFlatRate < ShippingOption | |
def self.can_ship?(weight, international) | |
!international && weight < (4 * 16) | |
end | |
end | |
options = ShippingOption.for(3 * 16, false) | |
puts options |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment