module BELParser::Language

Language defines the concepts needed to define, represent, and validate a BEL specification. This includes:

Constants

DEFAULT_VERSION
LOCK

Mutex to synchronize creation of BEL specifications.

Public Class Methods

default_specification() click to toggle source
# File lib/bel_parser/language.rb, line 63
def self.default_specification
  specification(DEFAULT_VERSION)
end
default_version() click to toggle source
# File lib/bel_parser/language.rb, line 59
def self.default_version
  DEFAULT_VERSION
end
defines_version?(version) click to toggle source

Indicates if version is a defined BEL specification.

@param [#to_s] version the BEL version string (e.g. +“2.0”+) @return [Boolean] true if version is defined; false if not

# File lib/bel_parser/language.rb, line 35
def self.defines_version?(version)
  specification(version)
  true
rescue ArgumentError, LoadError
  false
end
latest_supported_specification() click to toggle source

Returns the latest supported specification according to the MAJOR.MINOR pattern.

@return [Specification] latest supported specification

# File lib/bel_parser/language.rb, line 79
def self.latest_supported_specification
  latest_version = versions.max_by { |version| version.to_f }
  specification(latest_version)
end
latest_supported_version() click to toggle source

Returns the latest supported version string according to the MAJOR.MINOR pattern.

@return [String] latest supported version

# File lib/bel_parser/language.rb, line 71
def self.latest_supported_version
  versions.max_by { |version| version.to_f }
end
specification(version) click to toggle source

Returns the {Specification} for a BEL specification version.

@param [#to_s] version the BEL version string (e.g. +“2.0”+) @return [Specification] the BEL specification @raise [ArgumentError] if the version string is malformed or is not

supported

@see {.defines?}

# File lib/bel_parser/language.rb, line 98
def self.specification(version)
  version_string         =  version.to_s
  unless version_string  =~ /^[0-9]+\.[0-9]+/
    raise ArgumentError, 'Version format incorrect; expecting MAJOR.MINOR'
  end

  LOCK.synchronize do
    version_sym           = version_string.to_sym
    @specs              ||= {}
    @specs[version_sym] ||= create_specification(version_string)
    @specs[version_sym]
  end
end
specifications() click to toggle source

Returns all language {Specification specifications}.

@return [Array<Specification>] BEL specifications

# File lib/bel_parser/language.rb, line 87
def self.specifications
  versions.map { |version| specification(version) }
end
versions() click to toggle source

Returns all version strings that are defined and supported by this project.

@return [Array<String>] BEL language versions

# File lib/bel_parser/language.rb, line 46
def self.versions
  Dir[
    File.join(
      File.expand_path('..', __FILE__),
      'language',
      'version*.rb'
    )
  ].map do |path|
    file_name = File.basename(path)
    file_name.scan(/[0-9]+_[0-9]+/).first.sub('_', '.')
  end.sort
end

Private Class Methods

create_specification(version) click to toggle source

Create the {Specification} for a BEL version.

@param [String] version the BEL version string (e.g. +“2.0”+) @return [Specification] the BEL specification @raise [ArgumentError] if the version string is malformed or is not

supported

@see {.defines?}

# File lib/bel_parser/language.rb, line 119
def self.create_specification(version)
  major, minor = version.split('.')
  version_file = "language/version#{major}_#{minor}"

  begin
    require_relative version_file
    version_const  = :"Version#{major}_#{minor}"
    version_module = BELParser::Language.const_get(version_const)
    version_module::Specification.new
  rescue LoadError
    raise ArgumentError, "Version #{version} is not supported."
  end
end