Class: Dryml::DRYMLBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/dryml/dryml_builder.rb

Defined Under Namespace

Classes: Erubis

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ DRYMLBuilder

Returns a new instance of DRYMLBuilder.



7
8
9
10
11
# File 'lib/dryml/dryml_builder.rb', line 7

def initialize(template)
  @template = template
  @build_instructions = nil # set to [] on the first add_build_instruction
  @part_names = []
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



13
14
15
# File 'lib/dryml/dryml_builder.rb', line 13

def environment
  @environment
end

#templateObject (readonly)

Returns the value of attribute template.



13
14
15
# File 'lib/dryml/dryml_builder.rb', line 13

def template
  @template
end

Instance Method Details

#<<(params) ⇒ Object



48
49
50
# File 'lib/dryml/dryml_builder.rb', line 48

def <<(params)
  @build_instructions << params
end

#add_build_instruction(type, params) ⇒ Object



36
37
38
# File 'lib/dryml/dryml_builder.rb', line 36

def add_build_instruction(type, params)
  @build_instructions << params.merge(:type => type)
end

#add_part(name, src, line_num) ⇒ Object

Raises:



41
42
43
44
45
# File 'lib/dryml/dryml_builder.rb', line 41

def add_part(name, src, line_num)
  raise DrymlException.new("duplicate part: #{name}", template_path, line_num) if name.in?(@part_names)
  add_build_instruction(:def, :src => src, :line_num => line_num)
  @part_names << name
end

#build(local_names, auto_taglibs, src_mtime) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/dryml/dryml_builder.rb', line 112

def build(local_names, auto_taglibs, src_mtime)

  auto_taglibs.each { |t| import_taglib(t) }

  @build_instructions._?.each do |instruction|
    name = instruction[:name]
    case instruction[:type]
    when :eval
      @environment.class_eval(instruction[:src], template_path, instruction[:line_num])

    when :def
      src = erb_process(instruction[:src])
      @environment.class_eval(src, template_path, instruction[:line_num])

    when :render_page
      method_src = render_page_source(erb_process(instruction[:src]), local_names)
      @environment.compiled_local_names = local_names
      @environment.class_eval(method_src, template_path, instruction[:line_num])

    when :include
      import_taglib(instruction)

    when :module
      import_module(name.constantize, instruction[:as])

    when :alias_method
      @environment.send(:alias_method, instruction[:new], instruction[:old])

    else
      raise RuntimeError.new("DRYML: Unknown build instruction :#{instruction[:type]}, " +
                             "building #{template_path}")
    end
  end
  @last_build_mtime = src_mtime
end

#erb_process(erb_src) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/dryml/dryml_builder.rb', line 102

def erb_process(erb_src)
  trim_mode = ActionView::Template::Handlers::ERB.erb_trim_mode
  erb = Erubis.new(erb_src, :trim_mode => trim_mode)
  res = erb.src
  if res.respond_to? :force_encoding
    res.force_encoding(erb_src.encoding)
  end
  res
end

#import_module(mod, as = nil) ⇒ Object

Raises:

  • (NotImplementedError)


163
164
165
166
# File 'lib/dryml/dryml_builder.rb', line 163

def import_module(mod, as=nil)
  raise NotImplementedError if as
  @environment.send(:include, mod)
end

#import_taglib(options) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/dryml/dryml_builder.rb', line 149

def import_taglib(options)
  if options[:module]
    import_module(options[:module].constantize, options[:as])
  else
    template_dir = File.dirname(template_path)
    options = options.merge(:template_dir => template_dir, :source_template => template_path)

    Taglib.get(options).each do |taglib|
      taglib.import_into(@environment, options[:as])
    end
  end
end

#ready?(mtime, d = false) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/dryml/dryml_builder.rb', line 25

def ready?(mtime, d=false)
  @build_instructions && @last_build_mtime && @last_build_mtime >= mtime
end

#render_page_source(src, local_names) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/dryml/dryml_builder.rb', line 53

def render_page_source(src, local_names)
  locals = local_names.map{|l| "#{l} = __local_assigns__[:#{l}];"}.join(' ')

  ("def render_page(__page_this__, __local_assigns__); " +
        "#{locals} new_object_context(__page_this__) do " +
        src +
       "; output_buffer; end; end")
end

#set_environment(environment) ⇒ Object



20
21
22
# File 'lib/dryml/dryml_builder.rb', line 20

def set_environment(environment)
  @environment = environment
end

#startObject



30
31
32
33
# File 'lib/dryml/dryml_builder.rb', line 30

def start
  @part_names.clear
  @build_instructions = []
end

#template_pathObject



15
16
17
# File 'lib/dryml/dryml_builder.rb', line 15

def template_path
  template.template_path
end