Class: Ninjs::Generator
- Inherits:
-
Object
- Object
- Ninjs::Generator
- Defined in:
- lib/ninjs/generator.rb
Instance Attribute Summary collapse
-
#alias ⇒ Object
writeonly
Sets the attribute alias.
-
#app_name ⇒ Object
writeonly
Sets the attribute app_name.
Instance Method Summary collapse
- #generate ⇒ Object
- #generate_elements_file ⇒ Object
- #generate_model_file ⇒ Object
- #generate_module_file ⇒ Object
-
#initialize(config) ⇒ Generator
constructor
A new instance of Generator.
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
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
4 5 6 |
# File 'lib/ninjs/generator.rb', line 4 def app_name=(value) @app_name = value end |
Instance Method Details
#generate ⇒ Object
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_file ⇒ Object
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_file ⇒ Object
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_file ⇒ Object
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 |