Module: CSL::Loader

Included in:
Locale, Style
Defined in:
lib/csl/loader.rb

Overview

Note:

Base classes are exepcted to define a #parse method.

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#extensionObject

Returns the value of attribute extension.



12
13
14
# File 'lib/csl/loader.rb', line 12

def extension
  @extension
end

#prefixObject

Returns the value of attribute prefix.



12
13
14
# File 'lib/csl/loader.rb', line 12

def prefix
  @prefix
end

#rootObject

Returns the value of attribute root.



12
13
14
# File 'lib/csl/loader.rb', line 12

def root
  @root
end

Instance Method Details

#extend_name(string) ⇒ Object

Extends the passed-in string to a style/locale name, by prefixing and appending the default name prefix and extension.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/csl/loader.rb', line 48

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

#extend_path(string) ⇒ Object

Extends the passed-in string to a full path.



42
43
44
# File 'lib/csl/loader.rb', line 42

def extend_path(string)
  File.join(root.to_s, extend_name(string))
end

#listObject Also known as: ls



34
35
36
37
38
# File 'lib/csl/loader.rb', line 34

def list
  Dir["#{root}/#{prefix}*#{extension}"].map do |path|
    File.basename(path, extension).sub(/^#{prefix}/, '')
  end
end

#load(input) ⇒ Style, Locale

Note:

The base class is expected to define a #parse! method.

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.

Examples:

Style.load(:apa)                         -> style
Style.load('chicago-author.csl')         -> style
Locale.load('en')                        -> locale
Locale.load('http://example.com/de.xml') -> locale

Returns:

Raises:

  • ParseError



30
31
32
# File 'lib/csl/loader.rb', line 30

def load(input)
  parse! extract_data_from(input)
end