Created
July 22, 2008 01:19
-
-
Save dyoder/568 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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