Module: BEL::Extension::Format

Defined in:
lib/bel/extension_format.rb,
lib/bel/extensions/bel.rb,
lib/bel/extensions/jgf.rb,
lib/bel/extensions/xbel.rb,
lib/bel/extensions/json/oj.rb,
lib/bel/extensions/json/json.rb,
lib/bel/extensions/json/jrjackson.rb,
lib/bel/extensions/json/ruby_json.rb,
lib/bel/extensions/json/multi_json.rb,
lib/bel/extensions/rdf/rdf.rb

Overview

The Format module defines a framework for adding new document formats. This is useful when reading, writing, and translating BEL data.

A Format extension is defined and registered in the following steps:

  • Create a ruby source file located under bel/extensions/ on the $LOAD_PATH.

  • Within the file, create a class that implements the protocol specified by Formatter.

  • Instantiate and register your format extension by calling Format.register_formatter.

To see how to define a new format extension have a look at the Formatter module.

Defined Under Namespace

Modules: Formatter, JSONImplementation, RDFReader, RDFWriter Classes: BELYielder, EvidenceYielder, FormatBEL, FormatError, FormatJGF, FormatJSON, FormatRDF, FormatXBEL

Class Method Summary collapse

Class Method Details

.formatters(*values) ⇒ Format::Formatter Array<Format::Formatter>

Returns the formatters found for the values splat. The returned matches have the same cardinality as the values splat to allow destructuring.

Examples:

Retrieve a single formatter by id.

bel_formatter  = Format.formatters(:bel)

Retrieve a single formatter by file extension.

xbel_formatter = Format.formatters(:xml)

Retrieve a single formatter by media type.

json_formatter = Format.formatters(%s(application/json))

Retrieve multiple formatters using mixed values.

belf, xbelf, jsonf = Format.formatters(:bel, :xml, "application/json")

Parameters:

  • values (Array<#to_s>)

    the splat that identifies formatters

Returns:

  • (Format::Formatter Array<Format::Formatter>)

    for each consecutive value in the values splat; if the values splat contains one value then a single formatter reference is returned (e.g. not an Array)



86
87
88
89
90
# File 'lib/bel/extension_format.rb', line 86

def self.formatters(*values)
  FORMATTER_MUTEX.synchronize {
    _formatters(*values)
  }
end

.register_formatter(formatter) ⇒ Format::Formatter

Registers the formatter object as available to callers. The formatter should respond to the methods defined in the Formatter module.

Parameters:

Returns:



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
61
62
63
64
65
66
# File 'lib/bel/extension_format.rb', line 32

def self.register_formatter(formatter)
  FORMATTER_MUTEX.synchronize {
    @@formatters ||= []
    # vivified hash, like: { :foo => { :bar => [] } }
    @@index ||= (
      Hash.new do |h0, k0|
        h0[k0] = Hash.new do |h1, k1|
          h1[k1] = []
        end
      end
    )
    
    if _formatters(formatter.id)
      raise ExtensionRegistrationError.new(formatter.id)
    end

    # track registered formatters
    @@formatters << formatter

    # index formatter by its id
    @@index[:id][symbolize(formatter.id)] << formatter

    # index formatter by one or more file extensions
    [formatter.file_extensions].flatten.compact.to_a.each do |file_ext|
      @@index[:file_extension][symbolize(file_ext)] << formatter
    end

    # index formatter by one or more media types
    [formatter.media_types].flatten.compact.to_a.each do |media_type|
      @@index[:media_type][symbolize(media_type)] << formatter
    end

    formatter
  }
end