Class: ModuleGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/module/module_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_module_fileObject

source_root File.expand_path(‘../templates’, __FILE__)



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/generators/module/module_generator.rb', line 4

def create_module_file
  
  name = file_name.downcase.gsub(/\s|-/, '_')
  param = class_path
  param.map! {|item| item.downcase.gsub(/\s|-/, '_')}
  
  path = File.join('app/modules', *param)
  
  scope = []
  text = ""
  param.map! {|item|
    item = item.camelcase
    scope << item
    text += "module #{scope.join('::')}; end\n"
    item
  }
  param << name.camelcase
  scope = param.join('::')
  
  
  create_file File.join(path, "#{name}.rb") do
    type = ask("What type of module (device, service, logic) will this be?")
    
    text += "\n\nclass \#{scope} < AutomateEm::\#{type.downcase.camelcase}\ndef on_load\nend\n\ndef on_unload\nend\n\ndef on_update\nend\nend\n\n    FILE\n    \n    text\n  end\n  \nend\n"