Class: Cadenza::BlockHierarchy
- Inherits:
-
Object
- Object
- Cadenza::BlockHierarchy
- Defined in:
- lib/cadenza/block_hierarchy.rb
Overview
This class is used to help implement the “super” magic variable which is available when rendering blocks using the TextRenderer. It is essentially a glorified hash table with some special rules for merging.
Please treat this class as private to Cadenza, it is not meant to be used outside of this gem.
Instance Method Summary collapse
-
#fetch(block_name) ⇒ Array
(also: #[])
The inheritance chain for the given block name.
-
#initialize(data = nil) ⇒ BlockHierarchy
constructor
creates a new BlockHierarchy with the initial block hash.
-
#merge(hash) ⇒ Object
merges the given hash of blocks (name -> block) onto the end of each inheritance chain.
-
#push(block) ⇒ Object
(also: #<<)
appends the given block to the inheritance chain of it’s name.
Constructor Details
#initialize(data = nil) ⇒ BlockHierarchy
creates a new Cadenza::BlockHierarchy with the initial block hash
13 14 15 16 17 |
# File 'lib/cadenza/block_hierarchy.rb', line 13 def initialize(data=nil) @names = Hash.new merge(data) if data end |
Instance Method Details
#fetch(block_name) ⇒ Array Also known as: []
Returns the inheritance chain for the given block name.
20 21 22 |
# File 'lib/cadenza/block_hierarchy.rb', line 20 def fetch(block_name) @names[block_name.to_s] || [] end |
#merge(hash) ⇒ Object
merges the given hash of blocks (name -> block) onto the end of each inheritance chain
38 39 40 |
# File 'lib/cadenza/block_hierarchy.rb', line 38 def merge(hash) hash.each {|k,v| self << v } end |
#push(block) ⇒ Object Also known as: <<
appends the given block to the inheritance chain of it’s name
28 29 30 31 |
# File 'lib/cadenza/block_hierarchy.rb', line 28 def push(block) @names[block.name.to_s] ||= [] @names[block.name.to_s] << block end |