Class: AppDrone::Template
- Inherits:
-
Object
- Object
- AppDrone::Template
- Defined in:
- lib/app_drone/template.rb
Instance Method Summary collapse
- #add(ref, *params) ⇒ Object
- #do!(d, drone) ⇒ Object
- #do_finally!(d, drone) ⇒ Object
- #drone_classes ⇒ Object
- #drone_notices ⇒ Object
- #drone_objects ⇒ Object
- #generator_methods ⇒ Object
- #hook(klass) ⇒ Object
- #hook?(klass) ⇒ Boolean
-
#initialize ⇒ Template
constructor
A new instance of Template.
- #leftover_directives ⇒ Object
- #notify!(notice, drone) ⇒ Object
- #overridable_generator_methods ⇒ Object
- #overridden_generator_method?(m) ⇒ Boolean
- #render! ⇒ Object
- #render_to_file ⇒ Object
- #render_to_screen ⇒ Object
- #render_with_wrapper ⇒ Object
Constructor Details
#initialize ⇒ Template
Returns a new instance of Template.
4 5 6 7 8 |
# File 'lib/app_drone/template.rb', line 4 def initialize @drones = Hash.new{|h,k| h[k] = []} @directives = Hash.new{|h,k| h[k] = []} @drone_notices = Hash.new{|h,k| h[k] = []} end |
Instance Method Details
#add(ref, *params) ⇒ Object
10 11 12 13 |
# File 'lib/app_drone/template.rb', line 10 def add(ref,*params) klass = ref.is_a?(Class)? ref : ref.to_sym.to_app_drone_class @drones[klass] = klass.new(self, params.first) # no idea why `.first` is required.. end |
#do!(d, drone) ⇒ Object
29 30 31 32 |
# File 'lib/app_drone/template.rb', line 29 def do!(d,drone) generator_method = drone.class.generator_method || :leftovers @directives[generator_method] << d end |
#do_finally!(d, drone) ⇒ Object
34 35 36 |
# File 'lib/app_drone/template.rb', line 34 def do_finally!(d,drone) @directives[:final] << d end |
#drone_classes ⇒ Object
16 |
# File 'lib/app_drone/template.rb', line 16 def drone_classes; @drones.keys end |
#drone_notices ⇒ Object
17 |
# File 'lib/app_drone/template.rb', line 17 def drone_notices; @drone_notices end |
#drone_objects ⇒ Object
15 |
# File 'lib/app_drone/template.rb', line 15 def drone_objects; @drones.values end |
#generator_methods ⇒ Object
25 |
# File 'lib/app_drone/template.rb', line 25 def generator_methods; @directives.keys - [:leftovers] end |
#hook(klass) ⇒ Object
19 20 21 22 |
# File 'lib/app_drone/template.rb', line 19 def hook(klass) raise "No such drone: #{klass}" unless i_klass = @drones[klass] return i_klass end |
#hook?(klass) ⇒ Boolean
18 |
# File 'lib/app_drone/template.rb', line 18 def hook?(klass); !(@drones[klass].nil? || @drones[klass] == []) end |
#leftover_directives ⇒ Object
24 |
# File 'lib/app_drone/template.rb', line 24 def leftover_directives; @directives[:leftovers] end |
#notify!(notice, drone) ⇒ Object
38 39 40 |
# File 'lib/app_drone/template.rb', line 38 def notify!(notice,drone) @drone_notices[drone.class] << notice.gsub("'","\\'").gsub('"','\\"') # escape quotes end |
#overridable_generator_methods ⇒ Object
26 |
# File 'lib/app_drone/template.rb', line 26 def overridable_generator_methods; [:gemfile] end |
#overridden_generator_method?(m) ⇒ Boolean
27 |
# File 'lib/app_drone/template.rb', line 27 def overridden_generator_method?(m); overridable_generator_methods.include?(m) end |
#render! ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/app_drone/template.rb', line 42 def render! return if @rendered DependencyChain.check_dependencies!(drone_classes) ordered_chain = AppDrone::DependencyChain.sort(drone_classes) ordered_chain.each { |klass| hook(klass).align } ordered_chain.each { |klass| hook(klass).execute } @rendered = true end |
#render_to_file ⇒ Object
67 68 69 70 71 72 |
# File 'lib/app_drone/template.rb', line 67 def render_to_file render! File.open('out.rb','w+') do |f| f.write(render_with_wrapper) end end |
#render_to_screen ⇒ Object
62 63 64 65 |
# File 'lib/app_drone/template.rb', line 62 def render_to_screen render! puts render_with_wrapper end |
#render_with_wrapper ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/app_drone/template.rb', line 54 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 |