Class: Ramix::Builder

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/ramix/builder.rb

Overview

Ramix::Builder be responsible for adding some useful methods on the top or the bottom of template.

Instance Method Summary collapse

Methods included from Helpers

#before_callbacks, #callback_functions, #callbacks, #preparation_methods, #rails_version

Constructor Details

#initialize(default_template_path = nil, &block) ⇒ Builder

Returns a new instance of Builder.



8
9
10
11
12
# File 'lib/ramix/builder.rb', line 8

def initialize(default_template_path = nil, &block)
  @template_path = default_template_path || tempfile_path
  @import        = []
  instance_eval(&block) if block_given?
end

Instance Method Details

#import(template, *args) ⇒ Object



14
15
16
# File 'lib/ramix/builder.rb', line 14

def import(template, *args)
  @import << [template.order, proc { template.output(*args) }]
end

#runObject

Write some useful methods and the content of recipe in the file. Return the path of the template.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ramix/builder.rb', line 20

def run
  begin
    File.open(@template_path, "a+") do |file|
      file.write preparation_methods
      file.write rails_version
      file.write callback_functions
      ordered_templates.each{ |template|  file.write template[1].call }
      file.write before_callbacks
      file.write callbacks
    end
  rescue Exception => e
    puts "Create template #{@template} error~~ #{e.message}"
  end
  @template_path
end