Skip to content

Instantly share code, notes, and snippets.

@dyoder
Created July 22, 2008 01:19
Show Gist options
  • Save dyoder/568 to your computer and use it in GitHub Desktop.
Save dyoder/568 to your computer and use it in GitHub Desktop.
# first we will give Domains a little help
class Domain < String
def subdomain( rank = 1 ) ; self.split('.')[0..(rank-1)].join('.') ; end
def level( rank ) ; self.split('.')[(rank*-1)..-1].join('.') ; end
def top_level ; level(1) ; end
end
# now we can treat them like strings but also access domain components naturally
d = Domain.new("store.apple.co.uk")
d == "store.apple.co.uk" # => true
d =~ /^store/ # => 0
d.subdomain # => "store"
d.subdomain(2) # => "store.apple"
d.top_level # => "uk"
d.level(2) # => "co.uk"
d.level(3) # => "apple.co.uk"
# within request mappings, we can pass lambda constraints:
:domain => lambda { |d| d.level(2) == "co.uk" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment