Sha256: e2d554cb1c6ce7ad9f3ed9fc42beaabec6f94098a31bcce70adc4726b949f57d

Contents?: true

Size: 1.49 KB

Versions: 1

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
        raise 'no context set for evaluation the config file' unless context
        context
      end

      def request_uri
        request.fullpath
      end

      def request_path
        request.path
      end

      def current_page?(url)
        url_string = CGI.unescape(url)
        if url_string.index("?")
          uri = request_uri
        else
          uri = request_uri.split('?').first
        end
        if url_string =~ /^\w+:\/\//
          url_string == "#{request.scheme}://#{request.host_with_port}#{uri}"
        else
          url_string == uri
        end
      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| "#{k}='#{v}'"}.join(' ')
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple-navigation-3.3.2 lib/simple_navigation/adapters/sinatra.rb