Class: Origen::Generator::Stage

Inherits:
Object
  • Object
show all
Defined in:
lib/origen/generator/stage.rb

Overview

The stage provides a way to store objects in named banks for later retrieval. This is typically used during pattern generation to generate header, body and footer elements of a pattern in non-sequential order, allowing them to be combined at the end into the logical order.

Instance Method Summary collapse

Constructor Details

#initializeStage

Returns a new instance of Stage


8
9
10
# File 'lib/origen/generator/stage.rb', line 8

def initialize
  @vault = {}
end

Instance Method Details

#bank(name = @bank) ⇒ Object

Returns the entire bank, an array


71
72
73
# File 'lib/origen/generator/stage.rb', line 71

def bank(name = @bank)
  @vault[name] || []
end

#bank=(name) ⇒ Object

Set the current bank


66
67
68
# File 'lib/origen/generator/stage.rb', line 66

def bank=(name)
  @bank = name
end

#current_bankObject


75
76
77
78
# File 'lib/origen/generator/stage.rb', line 75

def current_bank
  return @vault[@bank] if @vault[@bank]
  @vault[@bank] = []
end

#insert_from_end(obj, x) ⇒ Object

Insert a new object into the current bank X places from the end


43
44
45
46
47
48
# File 'lib/origen/generator/stage.rb', line 43

def insert_from_end(obj, x)
  # Ruby insert is a bit un-intuative in that insert(1) will insert something 1 place in from the
  # start, whereas insert(-1) will insert it at the end (0 places in from the end).
  # So the subtraction of 1 here aligns the behavior when inserting from the start or the end.
  current_bank.insert((x * -1) - 1, obj)
end

#insert_from_start(obj, x) ⇒ Object

Insert a new object into the current bank X places from the start


51
52
53
# File 'lib/origen/generator/stage.rb', line 51

def insert_from_start(obj, x)
  current_bank.insert(x, obj)
end

#last_object(offset = 0) ⇒ Object

Same as last_vector except it returns the last objects of any type, not just vectors


32
33
34
35
# File 'lib/origen/generator/stage.rb', line 32

def last_object(offset = 0)
  i = current_bank.size - 1 - offset
  current_bank[i]
end

#last_vector(offset = 0) ⇒ Object

Returns vectors from the end of the bank


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/origen/generator/stage.rb', line 17

def last_vector(offset = 0)
  offset = offset.abs
  i = current_bank.size - 1
  while offset >= 0
    return nil if i < 0
    unless current_bank[i].is_a?(String)
      return current_bank[i] if offset == 0
      offset -= 1
    end
    i -= 1
  end
end

#newestObject

Pull the last item added to the current bank


56
57
58
# File 'lib/origen/generator/stage.rb', line 56

def newest
  current_bank.pop
end

#oldestObject

Pull the oldest item added to the current bank


61
62
63
# File 'lib/origen/generator/stage.rb', line 61

def oldest
  current_bank.shift
end

#reset!Object


12
13
14
# File 'lib/origen/generator/stage.rb', line 12

def reset!
  @vault = {}
end

#store(obj) ⇒ Object

Store a new value in the current bank


38
39
40
# File 'lib/origen/generator/stage.rb', line 38

def store(obj)
  current_bank.push(obj)
end

#with_bank(bank) ⇒ Object

Temporarily switches to the given bank


81
82
83
84
85
86
# File 'lib/origen/generator/stage.rb', line 81

def with_bank(bank)
  orig_bank = @bank
  @bank = bank
  yield
  @bank = orig_bank
end