Class: NodeJS::Module

Inherits:
Object
  • Object
show all
Defined in:
lib/nodejs/module.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environmnent, file) ⇒ Module

Returns a new instance of Module.



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

def initialize(environmnent, file)
  @environment = environmnent
  @file = file
  @exports = @environment.new_object
end

Instance Attribute Details

#exportsObject

Returns the value of attribute exports.



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

def exports
  @exports
end

Instance Method Details

#require_functionObject

Used to require both script files and modules Script path is relative from base path and defines where the require function will begin looking for require()‘d js-files script_path is ignored when requiring modules, i.e. require(“jade”)



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/nodejs/module.rb', line 14

def require_function
  lambda do |*args|
    this, module_id = *args

    module_id ||= this #backwards compatibility with TRR < 0.10

    return @environment.require(module_id) if NodeJS.builtins.keys.include?(module_id)

    #puts "requiring #{module_id} from #{CommonJS.resolve(@file.dirname, module_id).to_s}"
    @environment.require(NodeJS.resolve(@file.dirname, module_id).to_s)
  end
end