class SamFront::Boostraper

Public Class Methods

new(arr) click to toggle source
# File lib/jenga/boostraper.rb, line 15
def initialize arr
  @hash = arr
  not_a_hash if not arr.is_a? Array
end

Private Class Methods

not_a_hash() click to toggle source
# File lib/jenga/boostraper.rb, line 26
def self.not_a_hash
  msg = "Boostraper received #{@hash.class}, expected Hash"
  raise msg
end

Public Instance Methods

get_first() click to toggle source
# File lib/jenga/boostraper.rb, line 20
def get_first
  set_chain
end

Private Instance Methods

set_chain() click to toggle source

check: stackoverflow.com/questions/5924495

# File lib/jenga/boostraper.rb, line 34
def set_chain
  components = []
  #load each component
  @hash.each do |c|       
    # Get component class
    clazz = nil
    if c[:class].include? "::"
      clazz = c[:class].split('::').inject(Object) {|o,c| o.const_get c}
    else
      clazz = Object.const_get(c[:class])
    end

    # Instantiate components
    comp = clazz.new
    comp.set_config c[:config]

    # Add component to list
    components << comp
  end

  # Set chain sequence
  last = components.length - 1
  components.each_with_index do |c, idx|
    c.forward components[idx+1] unless idx == last
  end
  # return the first on the chain
  return components.first
end