module HDLRuby::Hdecorator

Gives a decorator the HDLRuby object.

Attributes

hdr_id[R]

The id

Private Class Methods

decorate_parent_id() click to toggle source

Some predefined properties to set.

# 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 = "") click to toggle source

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

# 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) click to toggle source

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.

# 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) click to toggle source

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.

# 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) click to toggle source

Get an object by id.

# File lib/HDLRuby/hruby_decorator.rb, line 42
def self.get(id)
    return @@id_map[id]
end
included(base) click to toggle source

Ensures the ID is generated when object is initialized

# 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) click to toggle source

Loads properties to key for all objects from source.

# 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

Private Instance Methods

properties() click to toggle source

Access the set of properties

# 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