Class: Origen::NVM::BlockArray

Inherits:
Array show all
Defined in:
lib/origen/nvm/block_array.rb

Overview

A block array is a standard Ruby array that has been enhanced with additional methods related to the fact that it is intended to hold NVM block objects.

This for example allows a block select value to be automatically generated for whatever blocks are contained in the array by calling the bsel method.

Instance Method Summary collapse

Methods inherited from Array

#dups, #dups?, #dups_with_index, #ids

Instance Method Details

#[](ix) ⇒ Object

Return all single blocks wrapped in a block array



10
11
12
# File 'lib/origen/nvm/block_array.rb', line 10

def [](ix)
  BlockArray.new << super(ix)
end

#bselObject Also known as: block_select, block_select_value

Returns the block select value required to select all contained blocks, the block object must implement a method called bsel for this to work



53
54
55
# File 'lib/origen/nvm/block_array.rb', line 53

def bsel
  reduce(0) { |bsels, block| bsels | block.bsel }
end

#find(*ids) ⇒ Object

Extract a subset of blocks based on ids

$nvm.blocks.find(0,3)

An elegant way to implement this is via an accessor like this on your top-level object which owns the blocks:

def blocks(*args)
  if args.empty?
    @blocks
  else
    @blocks.find(*args)
  end
end
alias :block :blocks

This provides the following API:

$nvm.blocks        # Returns all blocks
$nvm.block(0)      # Returns block 0 wrapped in a block array
$nvm.blocks(0, 3)  # Returns blocks 0 and 3 wrapped in a block array


35
36
37
38
39
40
41
# File 'lib/origen/nvm/block_array.rb', line 35

def find(*ids)
  b = BlockArray.new
  ids.each do |id|
    b << self[id]
  end
  b
end

#size_in_bytesObject

Returns the sum of the size of all contained blocks in bytes, the block object must implement a method called size_in_kb for this to work



67
68
69
# File 'lib/origen/nvm/block_array.rb', line 67

def size_in_bytes
  size_in_kb * 1024
end

#size_in_kbObject

Returns the sum of the size of all contained blocks in KB, the block object must implement a method called size_in_kb for this to work



61
62
63
# File 'lib/origen/nvm/block_array.rb', line 61

def size_in_kb
  reduce(0) { |sum, block| sum + block.size_in_kb }
end