Class: NetMate::Generators::ControllerGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/net_mate/controller_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(controller, actions) ⇒ ControllerGenerator

Returns a new instance of ControllerGenerator.



8
9
10
11
# File 'lib/net_mate/controller_generator.rb', line 8

def initialize controller, actions
  @controller = controller.dup
  @actions = actions
end

Instance Method Details

#generate_controllerObject



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/net_mate/controller_generator.rb', line 13

def generate_controller       
  cur_dir = Dir.pwd
  flag = false
  while cur_dir != '/' do 
    if Dir.exist? 'app'              
      FileUtils.cd 'app'
      if Dir.exist? 'controllers'          
        FileUtils.cd 'controllers'
        controller_rb = "#{ActiveSupport::Inflector.tableize(@controller)}_controller.rb"
        File.open(controller_rb, 'w') do |file|  
          puts "Created app/controllers/#{controller_rb}"
          @controller =  ActiveSupport::Inflector.pluralize(@controller)                
          file.puts "class #{ActiveSupport::Inflector.camelize(@controller)}Controller < NetMate::Controller\n\n"
          @actions.each do |action|
            file.puts "  def #{action}\n  end\n\n"
          end
          file.puts 'end'
        end
        flag = true
        break
      else
        abort 'ERROR: app/controllers folder does not exist'
      end
    else
      cur_dir = File.expand_path '..', Dir.pwd
      FileUtils.cd cur_dir        
    end            
  end
  abort 'ERROR: please move to the application folder' unless flag
  unless @actions.empty?
    create_views
    create_routes
  end
end