Skip to content

Instantly share code, notes, and snippets.

@bit-dragon
Created January 16, 2013 15:36
Show Gist options
  • Save bit-dragon/4548024 to your computer and use it in GitHub Desktop.
Save bit-dragon/4548024 to your computer and use it in GitHub Desktop.
An example of hook class/module
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