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.



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/csl/loader.rb', line 76

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.



70
71
72
# File 'lib/csl/loader.rb', line 70

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

#listObject Also known as: ls



62
63
64
65
66
# File 'lib/csl/loader.rb', line 62

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 exepcted 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/csl/loader.rb', line 30

def load(input)
  case
  when input.respond_to?(:read)
    data = input.read
  when input.to_s =~ /^\s*</
    data = input.to_s
  else

    input = input.to_s

    case
    when File.exists?(input)
      location = input
    when File.exists?(extend_name(input))
      location = extend_name(input)
    when File.exists?(extend_path(input))
      location = extend_path(input)
    else
      location = input
    end

    Kernel.open(location, 'r:UTF-8') do |io|
      data = io.read
    end
  end

  parse(data)

rescue => e
  raise ParseError, "failed to load #{input.inspect}: #{e.message}"
end