Class: Monorail::ControllerGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/monorail/congen.rb

Instance Method Summary collapse

Constructor Details

#initializeControllerGenerator

Returns a new instance of ControllerGenerator.



39
40
# File 'lib/monorail/congen.rb', line 39

def initialize
end

Instance Method Details

#generate_controllerObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/monorail/congen.rb', line 89

def generate_controller
  cont = @options.controller
  unless cont and cont.length > 0
    puts "No controller specified, exiting."
    puts "For help: monorail controller --help"
    exit
  end

  if cont =~ /[^\w\d]/
    puts "Illegal characters in controller name, exiting."
    puts "For help: monorail controller --help"
    exit
  end

  path = File.expand_path( @options.dir )
  puts "Generating controller: #{cont}"
  puts "         in directory: #{path}"

  target = File.join( path, "c", cont )

  if File.exist?(target) || File.exist?(target+".rb")
    r = Readline.readline( "*** Controller #{cont} already exists. Erase it [yN]? " )
    unless r =~ /\Ay/i
      puts "Controller not re-generated, exiting."
      exit
    end

    puts "Erasing existing controller and re-generating"
    FileUtils.rm( target + ".rb" )
    FileUtils.rm_rf( target )
  end

  File.open( target + ".rb", "w" ) do |f|
    f.write <<EOF
# Monorail controller #{cont}
# Generated #{Time.now}

module Controller_#{cont}

def index
  "---Monorail: #{cont}---"
end

end
EOF
  end

 FileUtils.mkdir target

  puts "Generated controller #{cont}"
end

#parse_argumentsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/monorail/congen.rb', line 49

def parse_arguments
  @options = OpenStruct.new
  @options.dir = '.'
  @options.erase = false
  @options.controller = nil

  opts = OptionParser.new {|opts|
    opts.banner = "Usage: monorail controller [options] [controller-name]"

    opts.separator ""
    opts.separator "Options summary:"

    opts.on("-d", "--dir [DIR]", "generate a monorail controller in directory",
          "     default: the current directory") do |d|
      @options.dir = d
    end

    # Options summary
    opts.on_tail("-h", "--help", "Show this message") {
      puts opts
      exit
    }

    # Version
    opts.on_tail("--version", "Show version") {
      puts Version
      exit
    }


  }

  opts.parse!(ARGV)
  @options.controller = ARGV.shift


end

#runObject



43
44
45
46
# File 'lib/monorail/congen.rb', line 43

def run
  parse_arguments
  generate_controller
end