Class: OroGen::Spec::ConfigurationObject

Inherits:
Object
  • Object
show all
Defined in:
lib/orogen/spec/configuration_object.rb

Overview

Representation of a task's attribute or property

Direct Known Subclasses

Attribute, Property

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task, name, type, default_value) ⇒ ConfigurationObject

Create a new property with the given name, type and default value


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/orogen/spec/configuration_object.rb', line 39

def initialize(task, name, type, default_value)
    name = name.to_s
    if name !~ /^\w+$/
        raise ArgumentError, "property names need to be valid C++ identifiers, i.e. contain only alphanumeric characters and _ (got #{name})"
    end

    type = task.project.find_interface_type(type)
    OroGen.validate_toplevel_type(type)
    @dynamic = false
    @task, @name, @type, @default_value = task, name, type, default_value
    @setter_operation = nil
    @doc = nil
end

Instance Attribute Details

#default_valueObject (readonly)

The property's default value


36
37
38
# File 'lib/orogen/spec/configuration_object.rb', line 36

def default_value
  @default_value
end

#nameObject (readonly)

The property name


11
12
13
# File 'lib/orogen/spec/configuration_object.rb', line 11

def name
  @name
end

#setter_operationSpec::Operation?

An operation that can be used to set the property. This is non-nil only for dynamic properties.


24
25
26
# File 'lib/orogen/spec/configuration_object.rb', line 24

def setter_operation
  @setter_operation
end

#taskObject

The task on which this property is attached


9
10
11
# File 'lib/orogen/spec/configuration_object.rb', line 9

def task
  @task
end

#typeObject (readonly)

The property type, as a Typelib::Type object from the underlying project's type registry


15
16
17
# File 'lib/orogen/spec/configuration_object.rb', line 15

def type
  @type
end

Instance Method Details

#dynamicObject


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/orogen/spec/configuration_object.rb', line 53

def dynamic
    @setter_operation = task.find_operation("__orogen_set#{name.capitalize}")
    if !@setter_operation
        @setter_operation = task.operation("__orogen_set#{name.capitalize}").
            returns("bool").
            argument("value", type_name).
            doc("Dynamic Property setter of #{name}")
    end

    self
end

#dynamic?Boolean

If this property coudl be set Dynamic this returns true


18
# File 'lib/orogen/spec/configuration_object.rb', line 18

def dynamic?; !!@setter_operation end

#each_interface_type {|type| ... } ⇒ Object

Yields:


82
83
84
85
# File 'lib/orogen/spec/configuration_object.rb', line 82

def each_interface_type
    return enum_for(__method__) if !block_given?
    yield type
end

#orocos_type_nameObject

The type name as registered on RTT


31
32
33
# File 'lib/orogen/spec/configuration_object.rb', line 31

def orocos_type_name
    type.name
end

#pretty_print(pp) ⇒ Object


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/orogen/spec/configuration_object.rb', line 65

def pretty_print(pp)
    default = if value = self.default_value
                  ", default: #{value}"
              end

    if doc
        first_line = true
        doc.split("\n").each do |line|
            pp.breakable if !first_line
            first_line = false
            pp.text "# #{line}"
        end
        pp.breakable
    end
    pp.text "#{name}:#{type.name}#{default}"
end

#to_hHash

Converts this model into a representation that can be fed to e.g. a JSON dump, that is a hash with pure ruby key / values.

The generated hash has the following keys:

name: the attribute name
type: the type (as marshalled with Typelib::Type#to_h)
dynamic: boolean value indicating whether this can be set
  dynamically or not
doc: the documentation string
default: the default value. Not present if there is none.

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/orogen/spec/configuration_object.rb', line 107

def to_h
    result = Hash[
        name: name,
        type: type.to_h,
        dynamic: !!dynamic?,
        doc: (doc || "")]
    if value = self.default_value
        if value.respond_to?(:to_simple_value)
            result[:default] = value.to_simple_value
        else
            result[:default] = value
        end
    end
    result
end

#type_nameObject

The name of the type this property is using, for consistency with the type attribute


28
# File 'lib/orogen/spec/configuration_object.rb', line 28

def type_name; type.name end