Class: Cadenza::BlockHierarchy

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(data = nil) ⇒ BlockHierarchy

creates a new Cadenza::BlockHierarchy with the initial block hash

Parameters:

  • data (Hash) (defaults to: nil)

    the initial data to merge into the names 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

Returns:

  • (Array)

    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

Parameters:

  • hash (Hash)


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

Parameters:



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