Class: RbPlusPlus::RbModule

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

Overview

Class representation of a ruby Module to be exposed in the extension. A Module acts much in the same way as Extension in that it can contain classes, functions, enumerations, etc.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parser, &block) ⇒ RbModule

Registers a new module definition for this extension. Use Extension#module or RbModule#module instead of creating an instance of this class directly

The block parameter is optional, you can also grab the reference of the Module and work with it as you want:

module "Name" do |m|
  ...
end

or

m = module "Name"
...

Unlike Extension#new, no special processing is done in the block version, it’s just there for convenience.



38
39
40
41
42
43
44
45
46
47
# File 'lib/rbplusplus/module.rb', line 38

def initialize(name, parser, &block)
  @name = name
  @parser = parser
  @modules = []
  @wrapped_functions = []
  @wrapped_classes = []
  @wrapped_structs = []

  block.call(self) if block
end

Instance Attribute Details

#modulesObject

Modules can be nested



8
9
10
# File 'lib/rbplusplus/module.rb', line 8

def modules
  @modules
end

#nameObject

Modules have a name



11
12
13
# File 'lib/rbplusplus/module.rb', line 11

def name
  @name
end

#nodeObject (readonly)

Access to the underlying RbGCCXML parser



14
15
16
# File 'lib/rbplusplus/module.rb', line 14

def node
  @node
end

#parentObject

Parent module if this is nested



17
18
19
# File 'lib/rbplusplus/module.rb', line 17

def parent
  @parent
end

Instance Method Details

#classes(*args) ⇒ Object

As with #functions, add to node.classes any classes / structs that have been explicitly given to this module.



89
90
91
# File 'lib/rbplusplus/module.rb', line 89

def classes(*args)
  [node ? node.classes(*args) : [], @wrapped_classes].flatten
end

#enumerations(*args) ⇒ Object



98
99
100
# File 'lib/rbplusplus/module.rb', line 98

def enumerations(*args)
  node ? node.enumerations(*args) : []
end

#functions(*args) ⇒ Object

Make sure to add to the node.functions any functions specifically given to this module



83
84
85
# File 'lib/rbplusplus/module.rb', line 83

def functions(*args)
  [node ? node.functions(*args) : [], @wrapped_functions].flatten
end

#includes(node) ⇒ Object

Add an RbGCCXML::Node to this module. This Node can be a Function, Class or Struct and will get wrapped accordingly



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rbplusplus/module.rb', line 67

def includes(node)
  if node.is_a?(RbGCCXML::Function)
    @wrapped_functions << node
  elsif node.is_a?(RbGCCXML::Class)
    @wrapped_classes << node
  elsif node.is_a?(RbGCCXML::Struct)
    @wrapped_structs << node
  else
    raise "Cannot use #{self.class}#includes for type '#{node.class}'"
  end

  node.moved_to = self
end

#module(name, &block) ⇒ Object

Register another module to be defined inside of this module. Acts the same as Extension#module.



59
60
61
62
63
# File 'lib/rbplusplus/module.rb', line 59

def module(name, &block)
  m = RbModule.new(name, @parser, &block)
  m.parent = self
  @modules << m
end

#namespace(name) ⇒ Object

Specify a C++ namespace from which the contained code will be wrapped and exposed to Ruby under this Module.

Also see Extension#namespace



53
54
55
# File 'lib/rbplusplus/module.rb', line 53

def namespace(name)
  @node = @parser.namespaces.find(:all, :name => name)
end

#qualified_nameObject

Get the fully nested name of this module



103
104
105
106
107
108
109
# File 'lib/rbplusplus/module.rb', line 103

def qualified_name
  if parent
    "#{parent.qualified_name}::#{self.name}"
  else
    self.name
  end
end

#structs(*args) ⇒ Object

See #clases and #functions



94
95
96
# File 'lib/rbplusplus/module.rb', line 94

def structs(*args)
  [node ? node.structs(*args) : [], @wrapped_structs].flatten
end