Module: NodeJS

Defined in:
lib/nodejs/builtins.rb,
lib/nodejs.rb,
lib/nodejs/module.rb,
lib/nodejs/environment.rb

Overview

Quick and dirty implementations of the node.js standard modules as required by Jade

Defined Under Namespace

Classes: Environment, Module

Class Method Summary collapse

Class Method Details

.builtinsObject



6
7
8
# File 'lib/nodejs.rb', line 6

def self.builtins
  @builtins
end

.register_builtin(sym, klass) ⇒ Object



3
4
5
# File 'lib/nodejs.rb', line 3

def self.register_builtin(sym, klass)
  @builtins[sym.to_s] = klass
end

.resolve(base_path, module_or_path) ⇒ Object



32
33
34
# File 'lib/nodejs/environment.rb', line 32

def self.resolve(base_path, module_or_path)
  NodeJS.send(module_or_path =~ /^(\.|\/)/ ? :resolve_file : :resolve_module, base_path, module_or_path)
end

.resolve_file(base_path, path) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/nodejs/environment.rb', line 36

def self.resolve_file(base_path, path)
  full_path = base_path.join(path)
  return NodeJS.resolve_file(base_path, full_path.join("index.js")) if full_path.directory?
  unless File.exists?(full_path) && full_path.extname =~ /\.js$/
    full_path = Pathname("#{full_path.to_s}.js")
  end
  fail LoadError, "Module '#{full_path}' not found" unless full_path.file?
  full_path
end

.resolve_module(base_path, module_name) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/nodejs/environment.rb', line 46

def self.resolve_module(base_path, module_name)
  module_dir = base_path.join("node_modules", module_name)
  package_json = module_dir.join("package.json")
  fail LoadError, "Module '#{module_name}' not found" unless package_json.file?
  module_def = JSON.parse(File.read(package_json))
  module_dir.join(module_def['main'])
end