Sha256: fdb68d9a5aeccd3a51ed636d71159ef7782b57d13ac70197a9f253c49d5ae9e2

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

module Raisin
  class Router
    ALL_VERSIONS = :all

    #
    # Reset class variables on the subclass when inherited
    #
    def self.inherited(subclass)
      subclass.reset
    end

    def self.reset
      @_current_version = nil
      @_versions = { ALL_VERSIONS => [] }
    end

    class << self
      attr_internal_accessor :versions, :current_version

      def mount(api)
        if version?(:header)
          @_versions[self.current_version].concat(api.routes)
        else
          @_versions[ALL_VERSIONS].concat(version?(:path) ? pathed_routes(api.routes) : api.routes)
        end
      end

      #
      # Set version for current block
      #
      def version(version, options = {}, &block)
        version = version.to_s
        self.current_version = version
        @_versions[version] = [] if version?(:header)
        yield
        self.current_version = nil
      end

      private

      def version?(type)
        self.current_version && Configuration.version.using == type
      end

      def pathed_routes(routes)
        self.routes.map! { |via, path, opts|
          path.append('/') unless path.start_with?('/')
          path.append(current_version.version)
          [via, path, opts]
        }
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
raisin-0.0.5 lib/raisin/router.rb
raisin-0.0.4 lib/raisin/router.rb