Sha256: a4b963aec179adb3b4aeae9b3623d529f68a8cdd800ba747154696436919875d

Contents?: true

Size: 1.12 KB

Versions: 24

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true
module Msgr
  class Routes
    include Logging
    attr_reader :routes
    delegate :each, :empty?, :size, :any?, to: :@routes

    def initialize
      @routes = []
    end

    def configure(&block)
      blocks << block
      instance_eval(&block)
    end

    def files
      @files ||= []
    end

    def blocks
      @blocks ||= []
    end

    def files=(files)
      @files = Array files
    end

    def <<(file)
      files << file
    end

    def reload
      routes.clear
      blocks.each {|block| instance_eval(&block) }
      files.uniq!
      files.each do |file|
        if File.exist? file
          load file
        else
          log(:warn) { "Routes file `#{file}` does not exists (anymore)." }
        end
      end
    end

    def load(file)
      unless File.exist?(file)
        raise ArgumentError.new "File `#{file}` does not exists."
      end

      instance_eval File.read file
    end

    def route(key, opts = {})
      if (route = routes.find {|r| r.accept?(key, opts) })
        route.add key
      else
        routes << Msgr::Route.new(key, opts)
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
msgr-0.15.2.1.b157 lib/msgr/routes.rb
msgr-0.15.2.1.b156 lib/msgr/routes.rb
msgr-0.15.2.1.b155 lib/msgr/routes.rb
msgr-0.15.2.1.b154 lib/msgr/routes.rb
msgr-0.15.2.1.b152 lib/msgr/routes.rb
msgr-0.15.1.1.b151 lib/msgr/routes.rb
msgr-0.15.2 lib/msgr/routes.rb
msgr-0.15.1.1.b150 lib/msgr/routes.rb
msgr-0.15.1.1.b146 lib/msgr/routes.rb
msgr-0.15.1.1.b145 lib/msgr/routes.rb
msgr-0.15.1.1.b144 lib/msgr/routes.rb
msgr-0.15.1.1.b143 lib/msgr/routes.rb
msgr-0.15.1.1.b141 lib/msgr/routes.rb
msgr-0.15.0.1.b140 lib/msgr/routes.rb
msgr-0.15.1 lib/msgr/routes.rb
msgr-0.15.0.1.b139 lib/msgr/routes.rb
msgr-0.15.0.1.b136 lib/msgr/routes.rb
msgr-0.15.0.1.b135 lib/msgr/routes.rb
msgr-0.15.0.1.b134 lib/msgr/routes.rb
msgr-0.15.0.1.b131 lib/msgr/routes.rb