Sha256: 52d80c6e1a4c64ddc772bff98e627d2f8e136fed09be706182bc9d8a035d15ab

Contents?: true

Size: 1.12 KB

Versions: 8

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

8 entries across 8 versions & 1 rubygems

Version Path
msgr-1.7.0 lib/msgr/routes.rb
msgr-1.6.1 lib/msgr/routes.rb
msgr-1.6.0 lib/msgr/routes.rb
msgr-1.5.0 lib/msgr/routes.rb
msgr-1.4.0 lib/msgr/routes.rb
msgr-1.3.2 lib/msgr/routes.rb
msgr-1.3.1 lib/msgr/routes.rb
msgr-1.3.0 lib/msgr/routes.rb