Class: NeptuneCoffee::JavascriptGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/neptune_coffee/javascript_generator.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, dir) ⇒ JavascriptGenerator

Returns a new instance of JavascriptGenerator.



22
23
24
25
# File 'lib/neptune_coffee/javascript_generator.rb', line 22

def initialize root, dir
  @root = root
  @dir = dir
end

Class Attribute Details

.generatorsObject

Returns the value of attribute generators.



7
8
9
# File 'lib/neptune_coffee/javascript_generator.rb', line 7

def generators
  @generators
end

Instance Attribute Details

#class_filesObject

Returns the value of attribute class_files.



21
22
23
# File 'lib/neptune_coffee/javascript_generator.rb', line 21

def class_files
  @class_files
end

#dirObject

Returns the value of attribute dir.



21
22
23
# File 'lib/neptune_coffee/javascript_generator.rb', line 21

def dir
  @dir
end

#rootObject

Returns the value of attribute root.



21
22
23
# File 'lib/neptune_coffee/javascript_generator.rb', line 21

def root
  @root
end

#subdirsObject

Returns the value of attribute subdirs.



21
22
23
# File 'lib/neptune_coffee/javascript_generator.rb', line 21

def subdirs
  @subdirs
end

Class Method Details

.load_erbObject



8
9
10
11
12
13
14
15
16
17
# File 'lib/neptune_coffee/javascript_generator.rb', line 8

def load_erb
  generator_root = Pathname.new(__FILE__).parent + "generators"
  @generators = {}
  generator_root.children(false).each do |erb_file|
    name = erb_file.to_s.split(/\.erb$/)[0]
    path = generator_root + erb_file
    @generators[name] = ERB.new path.read, nil, "<>-"
    @generators[name].filename = erb_file
  end
end

Instance Method Details

#define_js(relative_file_paths, local_names) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/neptune_coffee/javascript_generator.rb', line 31

def define_js relative_file_paths, local_names
  files_js = if relative_file_paths.length == 0 then ""
  else
    "\n  " + relative_file_paths.map{|f| "'./#{f}'"}.join(",\n  ") + "\n"
  end

  old_buffer, @output_buffer = @output_buffer, ''
  begin
    content = yield
  ensure
    @output_buffer = old_buffer
  end

  @output_buffer << "define([#{files_js}], function(#{local_names.join ', '}) {#{content}});"
end

#files_relative_to(files, relative_to_path) ⇒ Object



27
28
29
# File 'lib/neptune_coffee/javascript_generator.rb', line 27

def files_relative_to files, relative_to_path
  files.map {|f| f.relative_path_from relative_to_path}
end

#module(subdirs, class_files) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/neptune_coffee/javascript_generator.rb', line 47

def module subdirs, class_files
  @output_buffer = ""
  @subdirs = subdirs
  @class_files = class_files
  JavascriptGenerator.generators["module.js"].result(binding)
  @output_buffer
end

#namespace(subdirs) ⇒ Object



59
60
61
62
63
64
# File 'lib/neptune_coffee/javascript_generator.rb', line 59

def namespace subdirs
  @output_buffer = ""
  @subdirs = subdirs
  JavascriptGenerator.generators["namespace.js"].result(binding)
  @output_buffer
end

#namespace_nameObject



55
56
57
# File 'lib/neptune_coffee/javascript_generator.rb', line 55

def namespace_name
  @namespace_name ||= (dir == root ? "Neptune" : dir.basename.to_s.camel_case)
end