Module: Orocos::Namespace

Overview

The Namespace mixin provides collection classes with several methods for namespace handling.

Constant Summary collapse

DELIMATOR =

The Delimator between the namespace and the basename.

"/"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.split_name(name) ⇒ Array<String, String>

Splits the given name into its namespace name and basename.

Parameters:

  • name (String)

    the name which shall be split

Returns:

  • (Array<String, String>)

    its namespace and basename


95
96
97
98
99
100
101
# File 'lib/orocos/namespace.rb', line 95

def self.split_name(name)
    if(nil != (name =~ Regexp.new("^(.*)#{DELIMATOR}(.*)$")))
       [$1, $2]
    else
       [nil, name]
    end
end

.validate_namespace_name(name) ⇒ Object

Validates that the given name can be used as a namespace name

The only constraint so far is that namespace names cannot contain the namespace-to-name separation character DELIMATOR

Parameters:

  • name (String)

    the namespace name that should be validated

Raises:

  • (ArgumentError)

    if name is not a valid namespace name


110
111
112
113
114
# File 'lib/orocos/namespace.rb', line 110

def self.validate_namespace_name(name)
    if name =~ /#{DELIMATOR}/
        raise ArgumentError, "namespace #{name} contains #{DELIMATOR} which is not support."
    end
end

Instance Method Details

#basename(name) ⇒ String

Returns the basename of the given name.

Parameters:

  • name (String)

    the name

Returns:

  • (String)

    the name without its namespace


78
79
80
81
# File 'lib/orocos/namespace.rb', line 78

def basename(name)
    _,name = split_name(name)
    name
end

#map_to_namespace(names) ⇒ String, Array

Maps all given names to this namespace by adding the name of this namespace to the beginning of each given name if name is in none or in a different namespace.

Parameters:

  • names (String, Array)

    the names which shall be mapped

Returns:

  • (String, Array)

    the mapped names


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/orocos/namespace.rb', line 30

def map_to_namespace(names)
    if !namespace
        return names
    end

    ary = Array(names)
    ary.map! do |n|
        _,name = split_name(n)
        "#{namespace}#{DELIMATOR}#{name}"
    end
    if names.is_a? Array
        ary 
    else
        ary.first
    end
end

#namespaceString

Returns the name of the used namespace.

Returns:

  • (String)

    the used namespace name


20
21
22
# File 'lib/orocos/namespace.rb', line 20

def namespace
    @namespace
end

#namespace=(name) ⇒ Object

Sets the namespace name.

Parameters:

  • name (String)

    the new namespace name


12
13
14
15
# File 'lib/orocos/namespace.rb', line 12

def namespace=(name)
    Namespace.validate_namespace_name(name)
    @namespace = name
end

#same_namespace?(name) ⇒ Boolean

Checks if the given name is in the same namespace.

Parameters:

  • name (String)

    the name which shall be checked

Returns:

  • (Boolean)

52
53
54
55
56
57
58
59
# File 'lib/orocos/namespace.rb', line 52

def same_namespace?(name)
    ns,_ = split_name(name)
    if(!ns || !namespace || ns == namespace)
       true
    else
       false
    end
end

#split_name(name) ⇒ Array<String, String>

Splits the given name into its namespace name and basename.

Parameters:

  • name (String)

    the name which shall be split

Returns:

  • (Array<String, String>)

    its namespace and basename


87
88
89
# File 'lib/orocos/namespace.rb', line 87

def split_name(name)
    Namespace.split_name name
end

#verify_same_namespace(name) ⇒ nil

Checks if the given name is in the same namespace and raises an ArgumentError if not.

Parameters:

  • name (String)

    the name which shall be checked

Returns:

  • (nil)

Raises:

  • (ArgumentError)

67
68
69
70
71
72
# File 'lib/orocos/namespace.rb', line 67

def verify_same_namespace(name)
    if !same_namespace?(name)
        ns,_ = split_name(name)
        raise ArgumentError, "namespace '#{namespace}' was expected but got '#{ns}' for name '#{name}'"
    end
end