lib/ryodo/domain.rb in ryodo-0.3.0 vs lib/ryodo/domain.rb in ryodo-0.3.1

- old
+ new

@@ -1,10 +1,9 @@ module Ryodo class Domain # DomainString is a String with extended methods class DomainString < String - def reverse to_a(:r).join(".") end alias_method :r, :reverse @@ -18,28 +17,22 @@ end private def dsplit - self.split(".", -1) + split(".", -1) end end # remove own class comparison (we will use String#== via method_missing) undef_method :== def initialize(domainStr) fail TypeError, "Not a valid domain string!" unless domainStr.is_a?(String) - @domain_string = DomainString.new domainStr.downcase - no_leading_dot = @domain_string[0] != "." - parts = Ryodo::Parser.run(@domain_string) - no_dot_but_parts = no_leading_dot && parts - - @suffix = parts[0].reverse.join(".") if no_dot_but_parts - @domain = (parts[0] + parts[1]).reverse.join(".") if no_dot_but_parts && !parts[1].empty? - @secondary = parts[1].first if no_dot_but_parts && !parts[1].empty? - @subdomain = (parts[2]).reverse.join(".") if no_dot_but_parts && !parts[2].empty? + @domain_string = DomainString.new domainStr.downcase + parse_domain_string + retrieve_domain_parts end def suffix DomainString.new(@suffix) if @suffix end @@ -60,15 +53,15 @@ def subdomain DomainString.new(@subdomain) if @subdomain end def fqdn - DomainString.new("#{to_s}.") + DomainString.new("#{self}.") end def valid? - !!@suffix && !!@secondary + @suffix && @secondary && true end alias_method :is_valid?, :valid? def to_s @domain_string @@ -84,8 +77,39 @@ end # explicit definition of class' send def send(symbol, *args) __send__(symbol, *args) + end + + private + + def parse_domain_string + no_leading_dot = @domain_string[0] != "." + @_parts = Ryodo::Parser.run(@domain_string) + @_no_dot_but_parts = no_leading_dot && @_parts + end + + def retrieve_domain_parts + retrieve_suffix + retrieve_domain + retrieve_secondary + retrieve_subdomain + end + + def retrieve_suffix + @suffix = @_parts[0].reverse.join(".") if @_no_dot_but_parts + end + + def retrieve_domain + @domain = (@_parts[0] + @_parts[1]).reverse.join(".") if @_no_dot_but_parts && !@_parts[1].empty? + end + + def retrieve_secondary + @secondary = @_parts[1].first if @_no_dot_but_parts && !@_parts[1].empty? + end + + def retrieve_subdomain + @subdomain = (@_parts[2]).reverse.join(".") if @_no_dot_but_parts && !@_parts[2].empty? end end end