Sha256: 6e60139c2cc11ec4d9c50261a59501b68ef68d59728f9f4784e85db3c6d05240

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 KB

Contents

require 'cgi'

module SimpleNavigation
  module Adapters
    class Sinatra < Base
      def self.register
        SimpleNavigation.set_env(sinatra_root, sinatra_environment)
        ::Sinatra::Application.send(:helpers, SimpleNavigation::Helpers)
      end

      def initialize(context)
        @context = context
        @request = context.request
      end

      def context_for_eval
        context || fail('no context set for evaluation the config file')
      end

      def request_uri
        request.fullpath
      end

      def request_path
        request.path
      end

      def current_page?(url)
        url_string = CGI.unescape(url)
        uri = if url_string.index('?')
                request_uri
              else
                request_uri.split('?').first
              end

        if url_string =~ %r(^\w+://)
          uri = "#{request.scheme}://#{request.host_with_port}#{uri}"
        end

        url_string == CGI.unescape(uri)
      end

      def link_to(name, url, options = {})
        "<a href='#{url}'#{to_attributes(options)}>#{name}</a>"
      end

      def content_tag(type, content, options = {})
        "<#{type}#{to_attributes(options)}>#{content}</#{type}>"
      end

      protected

      def self.sinatra_root
        ::Sinatra::Application.root
      end

      def self.sinatra_environment
        ::Sinatra::Application.environment
      end

      def to_attributes(options)
        options.map { |k, v| v.nil? ? '' : " #{k}='#{v}'" }.join
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
simple-navigation-3.14.0 lib/simple_navigation/adapters/sinatra.rb
simple-navigation-3.13.0 lib/simple_navigation/adapters/sinatra.rb
simple-navigation-3.12.2 lib/simple_navigation/adapters/sinatra.rb
simple-navigation-3.12.1 lib/simple_navigation/adapters/sinatra.rb