Created
January 16, 2013 15:36
-
-
Save bit-dragon/4548024 to your computer and use it in GitHub Desktop.
An example of hook class/module
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 | |
@children = [] | |
def self.inherited(child) | |
@children << child | |
end | |
def self.for(weight, international) | |
@children.select do |child| | |
child.can_ship?(weight, international) | |
end | |
end | |
end | |
# ../shipping_priority_flat_rate | |
class PriorityFlatRate < ShippingOption | |
def self.can_ship?(weight, international) | |
!international | |
end | |
end | |
# ../shipping_madia_mail.rb | |
class MediaMail < ShippingOption | |
def self.can_ship?(weight, international) | |
!international and weight < 4*16 | |
end | |
end | |
# If the files are on diferent directory | |
# Dir.glob("shipping_*.rb") { |name| require name } | |
puts ShippingOption.for(3*16, false) | |
# => PriorityFlatRate | |
# => MediaMail | |
puts ShippingOption.for(5*100, false) | |
# => PriorityFlatRate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment