Class: Origen::Generator::Stage
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
-
#bank(name = @bank) ⇒ Object
Returns the entire bank, an array.
-
#bank=(name) ⇒ Object
Set the current bank.
- #current_bank ⇒ Object
-
#initialize ⇒ Stage
constructor
A new instance of Stage.
-
#insert_from_end(obj, x) ⇒ Object
Insert a new object into the current bank X places from the end.
-
#insert_from_start(obj, x) ⇒ Object
Insert a new object into the current bank X places from the start.
-
#last_object(offset = 0) ⇒ Object
Same as last_vector except it returns the last objects of any type, not just vectors.
-
#last_vector(offset = 0) ⇒ Object
Returns vectors from the end of the bank.
-
#newest ⇒ Object
Pull the last item added to the current bank.
-
#oldest ⇒ Object
Pull the oldest item added to the current bank.
- #reset! ⇒ Object
-
#store(obj) ⇒ Object
Store a new value in the current bank.
-
#with_bank(bank) ⇒ Object
Temporarily switches to the given bank.
Constructor Details
#initialize ⇒ Stage
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
73 74 75 |
# File 'lib/origen/generator/stage.rb', line 73 def bank(name = @bank) @vault[name] || [] end |
#bank=(name) ⇒ Object
Set the current bank
68 69 70 |
# File 'lib/origen/generator/stage.rb', line 68 def bank=(name) @bank = name end |
#current_bank ⇒ Object
77 78 79 80 81 |
# File 'lib/origen/generator/stage.rb', line 77 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
45 46 47 48 49 50 |
# File 'lib/origen/generator/stage.rb', line 45 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
53 54 55 |
# File 'lib/origen/generator/stage.rb', line 53 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
34 35 36 37 |
# File 'lib/origen/generator/stage.rb', line 34 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 29 30 |
# 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 |
#newest ⇒ Object
Pull the last item added to the current bank
58 59 60 |
# File 'lib/origen/generator/stage.rb', line 58 def newest current_bank.pop end |
#oldest ⇒ Object
Pull the oldest item added to the current bank
63 64 65 |
# File 'lib/origen/generator/stage.rb', line 63 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
40 41 42 |
# File 'lib/origen/generator/stage.rb', line 40 def store(obj) current_bank.push(obj) end |
#with_bank(bank) ⇒ Object
Temporarily switches to the given bank
84 85 86 87 88 89 |
# File 'lib/origen/generator/stage.rb', line 84 def with_bank(bank) orig_bank = @bank @bank = bank yield @bank = orig_bank end |