Class: Bluenode::Module

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, id, parent = nil) ⇒ Module

Returns a new instance of Module.



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

def initialize(context, id, parent = nil)
  @context  = context
  @id       = id
  @exports  = context.new_object
  @parent   = parent

  parent.children << self if parent

  @filename = nil
  @loaded   = false
  @children = []
  @paths    = []
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



10
11
12
# File 'lib/bluenode/module.rb', line 10

def children
  @children
end

#exportsObject

Returns the value of attribute exports.



9
10
11
# File 'lib/bluenode/module.rb', line 9

def exports
  @exports
end

#filenameObject

Returns the value of attribute filename.



9
10
11
# File 'lib/bluenode/module.rb', line 9

def filename
  @filename
end

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/bluenode/module.rb', line 9

def id
  @id
end

#loadedObject Also known as: loaded?

Returns the value of attribute loaded.



9
10
11
# File 'lib/bluenode/module.rb', line 9

def loaded
  @loaded
end

#parentObject (readonly)

Returns the value of attribute parent.



10
11
12
# File 'lib/bluenode/module.rb', line 10

def parent
  @parent
end

#pathsObject

Returns the value of attribute paths.



9
10
11
# File 'lib/bluenode/module.rb', line 9

def paths
  @paths
end

Class Method Details

.wrap(script) ⇒ Object



4
5
6
# File 'lib/bluenode/module.rb', line 4

def wrap(script)
  "(function(exports, require, module, __filename, __dirname) {\n#{script}\n})"
end

Instance Method Details

#load(filename) ⇒ Object

Raises:

  • (LoadError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bluenode/module.rb', line 27

def load(filename)
  raise LoadError, 'module already loaded' if loaded?

  @filename = filename
  @paths = @context.class.node_module_paths(File.dirname(filename))

  extension = File.extname(filename)
  extension = '.js' if extension.nil? || extension == ''
  extension = '.js' unless @context.extensions.key?(extension)

  @context.extensions[extension].call self, filename

  @loaded = true
end

#require(request) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bluenode/module.rb', line 42

def require(request)
  filename  = resolve(request)
  cached    = @context.modules[filename]

  return cached.exports if cached

  return NativeModule.require(@context, filename) if NativeModule.exist?(filename)

  mod = self.class.new(@context, filename, self)

  @context.modules[filename] = mod

  had_exception = true

  begin
    mod.load filename
    had_exception = false
  ensure
    @context.modules.delete(filename) if had_exception
  end

  mod.exports
end