Sha256: 77d4bf230a4adbfad8414c8198dadfbda640c938b91cd32e85deaf72e64389de

Contents?: true

Size: 1.16 KB

Versions: 4

Compression:

Stored size: 1.16 KB

Contents

# -*- coding: utf-8 -*-
module Yaks
  # RFC6906 The "profile" link relation http://tools.ietf.org/search/rfc6906
  class ProfileRegistry
    class << self
      include Util

      def create(&blk)
        blk ||= ->{}
        Class.new(self).tap(&σ(:instance_eval, &blk)).new
      end

      def profile(type, uri)
        profiles {|reg| reg.put(type, uri)}
      end

      def profiles
        @profiles ||= Yaks::Hash()
        @profiles = yield(@profiles) if block_given?
        @profiles
      end

      def inherited(child)
        child.profiles { @profiles }
      end
    end

    def find_by_type(type)
      self.class.profiles[type]
    end

    def find_by_uri(by_uri)
      self.class.profiles.detect {|type, uri| uri == by_uri}.first
    end
  end

  class NullProfileRegistry
    def find_by_type(type)
      type.to_s
    end

    def find_by_uri(uri)
      uri.to_sym
    end
  end

  class TemplateProfileRegistry
    def initialize(template)
      @template = URITemplate.new(template)
    end

    def find_by_type(type)
      @template.expand(type: type)
    end

    def find_by_uri(uri)
      @template.extract(uri).fetch(:type) { uri }.to_sym
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
yaks-0.3.1 lib/yaks/profile_registry.rb
yaks-0.3.0 lib/yaks/profile_registry.rb
yaks-0.2.0 lib/yaks/profile_registry.rb
yaks-0.1.0 lib/yaks/profile_registry.rb