Sha256: ed114038123ca3f47ca9d70eacae233eaea160d49258da61fcbd63ce3eab6389

Contents?: true

Size: 1.07 KB

Versions: 4

Compression:

Stored size: 1.07 KB

Contents

module Gambiarra
  class Router
    attr_reader :path, :params, :routes, :history, :url

    def initialize
      @routes  = []
      @history = History.new

      base_view = View.descendants[:BaseView]
      draw_route '', to: base_view.descendants[:Index]
      base_view.descendants.each do |name, view|
        next if name == :Index
        draw_route name.to_s.underscore.humanize.downcase, to: view
      end
    end

    def get(**params)
      path = params.delete(:path)
      @url  = build_url(path || history.current_route.path, **params)
      route = routes.detect { |route| route.path == path }
      return history.refresh(params) unless route
      history.add(route)
      route.respond(**params)
    end

    def previous_path
      history.previous
    end

    private

    def build_url(path, params)
      [
        path,
        (params || {}).map do |k,v|
          [k,v.gsub(' ', '-')].join(':')
        end
      ].compact.join(' ')
    end

    def draw_route(*args, **keyword_args, &block)
      @routes << Route.new(*args, **keyword_args, &block)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gambiarra-0.0.4 lib/gambiarra/router.rb
gambiarra-0.0.3 lib/gambiarra/router.rb
gambiarra-0.0.2 lib/gambiarra/router.rb
gambiarra-0.0.1 lib/gambiarra/router.rb