module CSL # # Mixin used by Locale and Style to load assets either from disk or from # the network. Classes including the Loader module are expected to provide # appropriate root, prefix and extension values and a parse method that # will be passed the contents of the asset data. # # @note # Base classes are exepcted to define a #parse method. module Loader attr_accessor :root, :prefix, :extension # @example # Style.load(:apa) -> style # Style.load('chicago-author.csl') -> style # Locale.load('en') -> locale # Locale.load('http://example.com/de.xml') -> locale # # Resolves the passed-in path/name or string and loads the asset data. # The data will be passed on to the #parse method of the base class. # Typically, this will return a new instance of the class. # # @note # The base class is exepcted to define a #parse method. # # @raise ParseError # # @return [Style, Locale] the parsed CSL resource def load(input) case when input.respond_to?(:read) data = input.read when input.to_s =~ /^\s* e raise ParseError, "failed to load #{input.inspect}: #{e.message}" end def list Dir["#{root}/#{prefix}*#{extension}"].map do |path| File.basename(path, extension).sub(/^#{prefix}/, '') end end alias ls list # Extends the passed-in string to a full path. def extend_path(string) File.join(root.to_s, extend_name(string)) end # Extends the passed-in string to a style/locale name, by prefixing and # appending the default name prefix and extension. def extend_name(string) if File.extname(string.to_s).empty? name = [string, extension].compact.join else name = string.to_s.dup end unless name.start_with?(prefix.to_s) name = [prefix, name].join end name end end end