Sha256: b994c86702113e2d861fd6a5b5fb21b812e371dc7f81cf7ad38966f3bccdf7d0

Contents?: true

Size: 888 Bytes

Versions: 10

Compression:

Stored size: 888 Bytes

Contents

# frozen_string_literal: true

class Rage::Router::Strategies::Host
  attr_reader :name, :must_match_when_derived

  def initialize
    @name = "host"
    @must_match_when_derived = false
  end

  def storage
    HostStorage.new
  end

  def custom?
    false
  end

  def validate(value)
    if !value.is_a?(String) && !value.is_a?(Regexp)
      raise ArgumentError, "Host should be a string or a Regexp"
    end
  end

  class HostStorage
    def initialize
      @hosts = {}
      @regexp_hosts = []
    end

    def get(host)
      exact = @hosts[host]
      return exact if exact

      @regexp_hosts.each do |regexp|
        return regexp[:value] if regexp[:host] =~ host.to_s
      end

      nil
    end

    def set(host, value)
      if host.is_a?(Regexp)
        @regexp_hosts << { host: host, value: value }
      else
        @hosts[host] = value
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rage-rb-1.14.0 lib/rage/router/strategies/host.rb
rage-rb-1.13.0 lib/rage/router/strategies/host.rb
rage-rb-1.12.0 lib/rage/router/strategies/host.rb
rage-rb-1.11.0 lib/rage/router/strategies/host.rb
rage-rb-1.10.1 lib/rage/router/strategies/host.rb
rage-rb-1.10.0 lib/rage/router/strategies/host.rb
rage-rb-1.9.0 lib/rage/router/strategies/host.rb
rage-rb-1.8.0 lib/rage/router/strategies/host.rb
rage-rb-1.7.0 lib/rage/router/strategies/host.rb
rage-rb-1.6.0 lib/rage/router/strategies/host.rb