require 'repertoire/media/exceptions/unknown_media' module Repertoire module Media # # Returns the +Hash+ of all registered Media types. # def Media.types @@media_types ||= {} end # # Returns +true+ if there is a Media type registered with the matching # _name_. # def Media.supports?(name) Media.types.has_key?(name.to_sym) end # # Returns 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 # # Returns the +Hash+ of all registered Media types and their associated # URI schemes. # def Media.schemes @@media_schemes ||= {} end # # Returns an +Array+ of all registered URI schemes. # def Media.registered_schemes Media.schemes.keys end # # Returns +true+ if a Media type was registered for the specified # _scheme_, returns +false+ otherwise. # def Media.supports_scheme?(scheme) Media.schemes.has_key?(scheme.to_s) end # # Returns 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 # # Returns the +Hash+ of all registered Media types and their # associated media storage directories. # def Media.directories @@media_directories ||= {} end # # Returns an +Array+ of all registered directories. # def Media.registered_directories Media.directories.keys end # # Returns +true+ if a Media type was registered with the specified # _directory_, returns +false+ otherwise. # def Media.recognizes_directory?(name) Media.directories.has_key?(name.to_s) end # # Returns the Media type that was registered for the specified # _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 # 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 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