Sha256: 4c3b30746fd30069c9c24269a92fac60122be1e0b0130f08a60297a81f7c21a7

Contents?: true

Size: 924 Bytes

Versions: 3

Compression:

Stored size: 924 Bytes

Contents

require 'singleton'

module Syncano
  module Resources
    class Paths
      include Singleton

      attr_accessor :collections, :members

      def initialize
        self.collections = self.class::Collection.new
        self.members = self.class::Member.new
      end

      private

      class Collection
        def initialize
          @map = {}
        end

        def define(path, resource)
          @map[Regexp.new("\\A#{path.gsub(/{[^}]*}/, '([^\/]+)')}\\z")] = resource
        end

        def match(path)
          _, resouce = @map.find { |pattern, _| pattern =~ path }
          resouce
        end
      end

      class Member
        def initialize
          @map = {}
        end

        def define(path, resource)
          resource_name = resource.name
          @map[resource_name] = path
        end

        def find(resource)
          @map[resource.name]
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
syncano-4.0.0.alpha4 lib/syncano/resources/paths.rb
syncano-4.0.0.alpha3 lib/syncano/resources/paths.rb
syncano-4.0.0.alpha2 lib/syncano/resources/paths.rb