lib/csl/loader.rb in csl-1.0.0.pre10 vs lib/csl/loader.rb in csl-1.0.0.pre11

- old
+ new

@@ -3,31 +3,36 @@ # 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 - - # call-seq: + + # @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. def load(input) case when input.respond_to?(:read) data = input.read when input.to_s =~ /^\s*</ data = input else - + case when File.exists?(input.to_s) location = input when File.exists?(extend_name(input)) location = extend_name(input) @@ -41,38 +46,40 @@ data = io.read end end parse(data) - + rescue => 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 - - # The base class is exepcted to redefine the #parse method. - def parse(data) - raise 'Not Implemented' - end end - + end \ No newline at end of file