Module: HDLRuby::Hdecorator

Defined in:
lib/HDLRuby/hruby_decorator.rb

Overview

Gives a decorator the HDLRuby object.

Constant Summary collapse

@@id_map =

The id to object table

{}
@@id_gen =

Generate the ID

0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hdr_idObject (readonly)

The id



18
19
20
# File 'lib/HDLRuby/hruby_decorator.rb', line 18

def hdr_id
  @hdr_id
end

Class Method Details

.decorate_parent_idObject

Some predefined properties to set.



133
134
135
136
137
138
139
140
141
142
# File 'lib/HDLRuby/hruby_decorator.rb', line 133

def self.decorate_parent_id
    @@id_map.each do |id, obj|
        parent = obj.parent
        if parent then
            obj.properties[:parent_id] = obj.parent.hdr_id
        else
            obj.properties[:parent_id] = -1
        end
    end
end

.dump(key, target = "") ⇒ Object

Saves properties +key+ of all the object associated with their id to +target+.



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/HDLRuby/hruby_decorator.rb', line 107

def self.dump(key, target = "")
    # Build the table to dump
    tbl = {}
    self.each do |id,obj|
        value = obj.properties[key]
        if value.any? then
            tbl[id] = value
        end
    end
    # Dump the table.
    target << YAML.dump(tbl)
    return target
end

.each(&ruby_block) ⇒ Object

Iterate over all the id with their object.

Returns an enumerator if no ruby block is given.

NOTE: converts the hash to an array to allow on-the-fly modification.



51
52
53
54
55
56
# File 'lib/HDLRuby/hruby_decorator.rb', line 51

def self.each(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each) unless ruby_block
    # A ruby block? Apply it on each object.
    @@id_map.to_a.each(&ruby_block)
end

.each_with_property(prop, top = nil, &ruby_block) ⇒ Object

Iterate over all the objects from +top+ with +prop+ property.

Returns an enumerator if no ruby block is given. NOTE: if +top+ is not given, iterate over all the objects.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/HDLRuby/hruby_decorator.rb', line 73

def self.each_with_property(prop, top = nil, &ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_with_property) unless ruby_block
    # A ruby block? Apply the ruby_block...
    if (top) then
        # A top... on each object from it.
        top.each_deep do |obj|
            if (obj.properties.key?(prop)) then
                ruby_block.call(obj, *obj.properties[prop])
            end
        end
    else
        # No top... on all the objects.
        self.each do |id,obj|
            if (obj.properties.key?(prop)) then
                ruby_block.call(obj, *obj.properties[prop])
            end
        end
    end
end

.get(id) ⇒ Object

Get an object by id.



42
43
44
# File 'lib/HDLRuby/hruby_decorator.rb', line 42

def self.get(id)
    return @@id_map[id]
end

.included(base) ⇒ Object

Ensures the ID is generated when object is initialized



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

def self.included(base) # built-in Ruby hook for modules
    base.class_eval do    
        original_method = instance_method(:initialize)
        define_method(:initialize) do |*args, &block|
            original_method.bind(self).call(*args, &block)
            # Generate the id.
            @hdr_id = @@id_gen
            @@id_gen += 1
            # Update the id to object table
            @@id_map[@hdr_id] = self
        end
    end
end

.load(source, key) ⇒ Object

Loads properties to +key+ for all objects from +source+.



122
123
124
125
126
127
128
129
# File 'lib/HDLRuby/hruby_decorator.rb', line 122

def self.load(source,key)
    # Load the id to property table.
    tbl = YAML.load(source)
    # Adds the property of each object according to tbl
    tbl.each do |id,value|
        @@id_map[id].properties[key] = value
    end
end

Instance Method Details

#propertiesObject

Access the set of properties



61
62
63
64
65
66
67
# File 'lib/HDLRuby/hruby_decorator.rb', line 61

def properties
    # Create the properties if not present.
    unless @properties then
        @properties = Properties.new(self)
    end
    return @properties
end