Class: Syskit::Properties

Inherits:
BasicObject
Defined in:
lib/syskit/properties.rb

Overview

A hash wrapper that gives access to properties in a more manageable way than using a hash or using TaskContext#property and Models::Component#method_missing

E.g.

task.properties.test = 10
task.properties.test # => 10

Instance Method Summary collapse

Constructor Details

#initialize(task, properties) ⇒ Properties

Returns a new instance of Properties



12
13
14
15
# File 'lib/syskit/properties.rb', line 12

def initialize(task, properties)
    @task = task
    @properties = properties
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/syskit/properties.rb', line 54

def method_missing(m, *args)
    if m =~ /=$/
        raw, p = __resolve_property($`.to_s)
        if raw
            return p.raw_write(*args)
        else
            return p.write(*args)
        end
    else
        raw, p = __resolve_property(m.to_s)

        if raw
            value = p.raw_read(*args)
            if ::Kernel.block_given?
                return p.raw_write(yield(value))
            else
                return value
            end
        else
            value = p.read(*args)
            if ::Kernel.block_given?
                return p.write(yield(value))
            else
                return value
            end
        end
    end
end

Instance Method Details

#[](name) ⇒ Property?

Returns a property by name

Returns:



35
36
37
# File 'lib/syskit/properties.rb', line 35

def [](name)
    @properties[name.to_str]
end

#__resolve_property(name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/syskit/properties.rb', line 39

def __resolve_property(name)
    if p = @properties[name.to_str]
        return false, p
    elsif name.start_with?("raw_")
        non_raw_name = name[4..-1]
        if p = @properties[non_raw_name]
            return true, p
        else
            ::Kernel.raise ::Orocos::NotFound, "neither #{non_raw_name} nor #{name} are a property of #{@task}"
        end
    else
        ::Kernel.raise ::Orocos::NotFound, "#{name} is not a property of #{@task}"
    end
end

#clear_valuesObject

Clear all written values



28
29
30
# File 'lib/syskit/properties.rb', line 28

def clear_values
    @properties.each_value { |p| p.clear_value }
end

#each(&block) ⇒ Object

Enumerate the properties



18
19
20
# File 'lib/syskit/properties.rb', line 18

def each(&block)
    @properties.each_value(&block)
end

#include?(name) ⇒ Boolean

Whether there is a property with this name

Returns:

  • (Boolean)


23
24
25
# File 'lib/syskit/properties.rb', line 23

def include?(name)
    @properties.has_key?(name.to_str)
end