Class: Bunch::EJSNode

Inherits:
FileNode show all
Defined in:
lib/bunch/ejs_node.rb

Instance Attribute Summary

Attributes inherited from AbstractNode

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FileNode

#inspect

Methods inherited from AbstractNode

#write_to_dir, #write_to_file

Constructor Details

#initialize(fn) ⇒ EJSNode

Returns a new instance of EJSNode.



3
4
5
6
# File 'lib/bunch/ejs_node.rb', line 3

def initialize(fn)
  EJSNode.require_ejs
  @filename = fn
end

Class Method Details

.require_ejsObject



41
42
43
44
45
46
47
48
# File 'lib/bunch/ejs_node.rb', line 41

def require_ejs
  unless @required
    require 'ejs'
    @required = true
  end
rescue LoadError
  raise "'gem install ejs' to compile .ejs files."
end

Instance Method Details

#contentObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/bunch/ejs_node.rb', line 8

def content
  @content ||= fetch(@filename) {
    <<-JAVASCRIPT
      (function() {
        this.JST || (this.JST = {});
        this.JST['#{template_name}'] = #{EJS.compile(File.read(@filename))};
      })();
    JAVASCRIPT
  }
rescue => e
  raise CompileError.new(e, @filename)
end

#nameObject



21
22
23
# File 'lib/bunch/ejs_node.rb', line 21

def name
  File.basename(@filename, '.ejs')
end

#target_extensionObject



35
36
37
# File 'lib/bunch/ejs_node.rb', line 35

def target_extension
  '.js'
end

#template_nameObject



25
26
27
28
29
30
31
32
33
# File 'lib/bunch/ejs_node.rb', line 25

def template_name
  name = @filename.sub(/\.jst\.ejs$/, '')

  if @options[:root]
    name.sub(/^#{Regexp.escape(@options[:root].to_s)}\//, '')
  else
    name
  end
end