Class: Ninjs::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/ninjs/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Generator

Returns a new instance of Generator.



6
7
8
9
10
11
12
13
14
15
# File 'lib/ninjs/generator.rb', line 6

def initialize(config)
  @type = config[:type]
  @project = config[:project]
  @name = config[:name]
  @module_name = config[:name].gsub(/^_/, '')
  @alias = config[:alias].nil? ? false : true
  @app_name = config[:alias] || @project.config.name
  @dest = config[:dest] || @project.config.src_dir.is_a?(String) ? @project.config.src_dir : @project.config.src_dir.first
  @dependencies = config[:dependencies] || { elements: false, model: false }
end

Instance Attribute Details

#alias=(value) ⇒ Object (writeonly)

Sets the attribute alias

Parameters:

  • value

    the value to set the attribute alias to.



4
5
6
# File 'lib/ninjs/generator.rb', line 4

def alias=(value)
  @alias = value
end

#app_name=(value) ⇒ Object (writeonly)

Sets the attribute app_name

Parameters:

  • value

    the value to set the attribute app_name to.



4
5
6
# File 'lib/ninjs/generator.rb', line 4

def app_name=(value)
  @app_name = value
end

Instance Method Details

#generateObject



17
18
19
20
21
# File 'lib/ninjs/generator.rb', line 17

def generate
  generate_module_file if @type === 'module'
  generate_elements_file if @type === 'elements' || @dependencies[:elements]
  generate_model_file if @type === 'model' || @dependencies[:model]
end

#generate_elements_fileObject



42
43
44
45
46
47
48
49
# File 'lib/ninjs/generator.rb', line 42

def generate_elements_file
  File.open("#{@project.root}/elements/#{@module_name}" + ".elements.js", "w") do |file|
    file << %Q{\t#{@project.config.module_alias}.dom.ready(function() {\n\t\tmod.elements({\n\t\t\t\n\t\t});\n\t});\n}
    puts Ninjs::Notification.added "created #{@module_name}.elements.js"
  end unless File.exists? "#{@project.root}/elements/#{@module_name}.elements.js"
  
  self
end

#generate_model_fileObject



51
52
53
54
55
56
57
58
# File 'lib/ninjs/generator.rb', line 51

def generate_model_file
  File.open "#{@project.root}/models/#{@module_name}.model.js", "w" do |file|
    file << %Q{\t#{@project.config.module_alias}.set_data({\n\t\t\n\t});\n}
    puts Ninjs::Notification.added "created #{@module_name}.model.js"
  end unless File.exists? "#{@project.root}/models/#{@module_name}.model.js"
  
  self
end

#generate_module_fileObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ninjs/generator.rb', line 23

def generate_module_file
  module_content = Array.new
  module_content << "(function(#{@app_name if @alias}) {\n"
  module_content << "\tvar #{@project.config.module_alias} = #{@app_name}.add_module('#{@name}');\n\n"
  module_content << %Q{\t//= require "../elements/#{@name.downcase}.elements"\n} if @dependencies[:elements] || @type === 'elements'
  module_content << %Q{\t//= require "../models/#{@name.downcase}.model"\n\n} if @dependencies[:model] || @type === 'model'
  module_content << "\t#{@project.config.module_alias}.actions = function() {\n\t\t\n\t};\n\n"
  module_content << "\t#{@project.config.module_alias}.run();\n"
  module_content << "\n})(#{@project.config.name if @alias});"
  
  
  File.open "#{@project.root}/#{@dest}/#{@name}.module.js", "w" do |file|
    file << module_content.join('')
    puts Ninjs::Notification.added "created #{@name.downcase}.module.js"
  end unless File.exists? "#{@project.root}/#{@dest}/#{@name}.module.js"
  
  self
end