Sha256: e4125d02f5d6c079ca64ffeac98604b3887cb31500f72054997108a0fad1d5f3

Contents?: true

Size: 1.41 KB

Versions: 11

Compression:

Stored size: 1.41 KB

Contents

module Scrivito

class Route

  class << self
    VALID_ROUTE_NAMES = %i(homepage slug_id permalink).freeze

    def register(route_set, name)
      assert_valid_route_name(name)
      registry(route_set)[name] = new(name)
    end

    def defined?(route_set, name)
      !!registry(route_set)[name]
    end

    def find(route_set, name)
      registry(route_set)[name] or raise InternalError, "route #{name} not found"
    end

    private

    def registry(route_set)
      @registry ||= {}
      @registry[route_set] ||= {}
    end

    def assert_valid_route_name(name)
      unless VALID_ROUTE_NAMES.include?(name)
        valid_values = VALID_ROUTE_NAMES.join(', ')
        raise ScrivitoError,
            %("#{name}" is not a valid scrivito route name. Valid values are: #{valid_values}.)
      end
    end
  end

  def initialize(name)
    @name = name
  end

  def helper_name
    # use a random name to prevent users from getting the idea
    # that the internally unsed routing helpers might be a stable API
    random_segment = Digest::SHA1.hexdigest(@name.to_s)[0..15]
    @helper_name ||= "scrivito_#{random_segment}"
  end

  def generate_path(context, options = {})
    generate(context, :path, options)
  end

  def generate_url(context, options = {})
    generate(context, :url, options)
  end

  def generate(context, path_or_url, options = {})
    context.public_send("#{helper_name}_#{path_or_url}", options)
  end

end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
scrivito_sdk-1.3.0.rc2 lib/scrivito/route.rb
scrivito_sdk-1.3.0.rc1 lib/scrivito/route.rb
scrivito_sdk-1.2.0 lib/scrivito/route.rb
scrivito_sdk-1.2.0.rc3 lib/scrivito/route.rb
scrivito_sdk-1.2.0.rc2 lib/scrivito/route.rb
scrivito_sdk-1.2.0.rc1 lib/scrivito/route.rb
scrivito_sdk-1.1.1 lib/scrivito/route.rb
scrivito_sdk-1.1.0 lib/scrivito/route.rb
scrivito_sdk-1.1.0.rc3 lib/scrivito/route.rb
scrivito_sdk-1.1.0.rc2 lib/scrivito/route.rb
scrivito_sdk-1.1.0.rc1 lib/scrivito/route.rb