Module: Prmd::MultiLoader

Defined in:
lib/prmd/multi_loader/yajl.rb,
lib/prmd/multi_loader/json.rb,
lib/prmd/multi_loader/yaml.rb,
lib/prmd/multi_loader/toml.rb,
lib/prmd/multi_loader/loader.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Json, Loader, Toml, Yajl, Yaml Classes: LoaderNotFound

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.file_extensionsObject

Returns the value of attribute file_extensions


11
12
13
# File 'lib/prmd/multi_loader/loader.rb', line 11

def file_extensions
  @file_extensions
end

Class Method Details

.autoload_loader(name) ⇒ Boolean

Attempts to autoload a Loader named name

Parameters:

  • (String)

Returns:

  • (Boolean)

    load success


18
19
20
21
22
23
24
25
26
# File 'lib/prmd/multi_loader/loader.rb', line 18

def self.autoload_loader(name)
  # extension names are preceeded with a .
  # TODO. probably just remove the first .
  loader_name = name.gsub('.', '')
  require "prmd/multi_loader/#{loader_name}"
  true
rescue
  false
end

.load_data(ext, data) ⇒ Object

Parameters:

  • ext (String)
  • data (String)

59
60
61
# File 'lib/prmd/multi_loader/loader.rb', line 59

def self.load_data(ext, data)
  loader(ext).load_data(data)
end

.load_file(filename) ⇒ Object

Shortcut for loading any supported file

Parameters:

  • ext (String)
  • filename (String)

77
78
79
80
# File 'lib/prmd/multi_loader/loader.rb', line 77

def self.load_file(filename)
  ext = File.extname(filename)
  loader(ext).load_file(filename)
end

.load_stream(ext, stream) ⇒ Object

Parameters:

  • ext (String)

    name of the loader also the extension of the stream

  • stream (IO)

67
68
69
# File 'lib/prmd/multi_loader/loader.rb', line 67

def self.load_stream(ext, stream)
  loader(ext).load_stream(stream)
end

.loader(name) ⇒ Prmd::MultiLoader::Loader

Locates and returns a loader for the given ext If no extension is found the first time, MultiLoader will attempt to load one of the same name.

Parameters:

  • ext (String)

Returns:


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/prmd/multi_loader/loader.rb', line 40

def self.loader(name)
  tried_autoload = false
  begin
    @file_extensions.fetch(name)
  rescue KeyError
    if tried_autoload
      raise LoaderNotFound, "Loader for extension (#{name}) was not found."
    else
      autoload_loader(name)
      tried_autoload = true
      retry
    end
  end
end