Sha256: c3982139ba6615a66bb7cc0c3fca365fe1ccfc420229231dd756e5fa7778ee77

Contents?: true

Size: 1.61 KB

Versions: 16

Compression:

Stored size: 1.61 KB

Contents

class AppDrone::Template
  def initialize; @drones = {}; @directives = {} end

  def add(ref,*params)
    klass = ref.is_a?(Class)? ref : ('AppDrone::' + ref.to_s.classify).constantize
    @drones[klass] = klass.new(self, params.first) # no idea why `.first` is required..
  end

  def drone_objects; @drones.values end
  def drone_classes; @drones.keys end
  def hook?(klass); !@drones[klass].nil? end
  def hook(klass)
    raise "No such drone: #{klass}" unless i_klass = @drones[klass]
    return i_klass
  end
  
  def leftover_directives; @directives[:leftovers] || [] end
  def overridden_generator_methods; @directives.keys - [:leftovers] end

  def do!(d,drone);
    generator_method = drone.class.generator_method || :leftovers
    (@directives[generator_method] ||= []) << d
  end
  
  def check_dependencies
    drone_classes.each { |drone_class|
      drone_class.dependencies.each { |d|
        dependency = d.to_s.classify
        raise "#{drone_class} depends on #{dependency}, but it is not included in the template." unless drone_classes.include?(d)
      }
    }
  end

  def render!
    return if @rendered
    check_dependencies
    drone_objects.map(&:align)
    drone_objects.map(&:execute)
    @rendered = true
  end

  def render_with_wrapper
    render!
    template_path = '/template.erb'
    full_path = File.dirname(__FILE__) + template_path
    snippet = ERB.new File.read(full_path)
    return snippet.result(binding)
  end

  def render_to_screen
    render!
    puts render_with_wrapper
  end

  def render_to_file
    render!
    File.open('out.rb','w+') do |f|
      f.write(render_with_wrapper)
    end
  end
  
end

Version data entries

16 entries across 8 versions & 1 rubygems

Version Path
app_drone-0.5.2 lib/app_drone/template.rb~
app_drone-0.5.2 lib/app_drone/template.rb
app_drone-0.5.1 lib/app_drone/template.rb~
app_drone-0.5.1 lib/app_drone/template.rb
app_drone-0.5.0 lib/app_drone/template.rb
app_drone-0.5.0 lib/app_drone/template.rb~
app_drone-0.4.0 lib/app_drone/template.rb
app_drone-0.4.0 lib/app_drone/template.rb~
app_drone-0.3.4 lib/app_drone/template.rb
app_drone-0.3.4 lib/app_drone/template.rb~
app_drone-0.3.3 lib/app_drone/template.rb
app_drone-0.3.3 lib/app_drone/template.rb~
app_drone-0.3.2 lib/app_drone/template.rb~
app_drone-0.3.2 lib/app_drone/template.rb
app_drone-0.3.1 lib/app_drone/template.rb
app_drone-0.3.1 lib/app_drone/template.rb~