require 'repertoire/media/exceptions/unknown_media' module Repertoire module Media # # All registered media types. # # @return [Hash] # The media types. # def Media.types @@media_types ||= {} end # # Determines if a media type has been registered. # # @param [Symbol, String] name # The name of the media type to check for. # # @return [Boolean] # Specifies whether there is a Media type registered with the # matching name. # def Media.supports?(name) Media.types.has_key?(name.to_sym) end # # Finds a media type by name. # # @param [Symbol, String] name # The name of the media type to search for. # # @return [Type] # The Media type with the matching name. # def Media.get(name) name = name.to_sym unless Media.supports?(name) raise(UnknownMedia,"media type #{name} is not registered",caller) end return Media.types[name] end # # The media types associated to various URI schemes. # # @return [Hash] # All registered Media types and their associated URI schemes. # def Media.schemes @@media_schemes ||= {} end # # URI schemes associated to media types. # # @return [Array] # All registered URI schemes. # def Media.registered_schemes Media.schemes.keys end # # Determines whether a given URI scheme is supported. # # @param [String, Symbol] scheme # The URI scheme to check for. # # @return [Boolean] # Specifies whether a Media type was registered for the specified # scheme. # def Media.supports_scheme?(scheme) Media.schemes.has_key?(scheme.to_s) end # # Finds the media type that supports a given URI scheme. # # @param [String, Symbol] name # The URI scheme to search for. # # @return [Type] # The Media type that was registered for the specified scheme. # def Media.supports_scheme(name) name = name.to_s unless Media.supports_scheme?(name) raise(UnknownMedia,"media type for scheme #{name.dump} is not registered",caller) end return Media.schemes[name] end # # Media types associated to media storage directories. # # @return [Hash] # All registered Media types and their associated media storage # directories. # def Media.directories @@media_directories ||= {} end # # Media storage directories associated with media types. # # @return [Array] # All registered media storage directories. # def Media.registered_directories Media.directories.keys end # # Determines if the media storage directory is recognized by any # media type. # # @param [String, Symbol] name # The media storage directory to search for. # # @return [Boolean] # Specifies whether a Media type was registered with the specified # media storage directory. # def Media.recognizes_directory?(name) Media.directories.has_key?(name.to_s) end # # Finds the media type which uses a given media storage directory. # # @param [String, Symbol] name # The media storage directory to search for. # # @return [Type] # The Media type that was registered for the specified media storage # directory. # def Media.recognizes_directory(name) name = name.to_s unless Media.recognizes_directory?(name) raise(UnknownMedia,"media type for directory #{name.dump} is not registered",caller) end return Media.directories[name] end # # Get the Media type that was registered for the scheme of the given # URI. # # @param [String, URI::HTTP] uri # The URI to infer the media type from. # # @return [Type] # The media type which handles the specified URI. # def Media.guess_from_uri(uri) Media.supports_scheme(uri.to_s.scan(/^[^:\/@]+:/).first.chop) end # # Attempts to determine the correct Media type for a given path. # # @param [String] path # The path to infer the media type from. # # @return [Type] # The media type which handles the repository at the specified path. # def Media.guess_from_path(path) path = File.expand_path(path) Media.directories.each do |directory,media| if File.directory?(File.join(path,directory)) return media end end raise(UnknownMedia,"the media type for #{path.dump} is unknown",caller) end end end