public
Description: Plug-and-play data persistence created for small Ruby web applications.
Homepage: http://stone.rubyforge.org
Clone URL: git://github.com/ndemonner/stone.git
stone / bin / stone-gen
100644 70 lines (55 sloc) 1.392 kb
1
2
3
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env ruby
 
require 'fileutils'
require 'rubygems'
require 'facets'
 
def usage
  puts <<-EOS
stone-gen model
 
 
EOS
end
 
def model_usage
  puts <<-EOS
stone-gen model ModelName field:type field:type ...
 
e.g. stone-gen model Author name:string street_number:fixnum
 
Works just like all the other model generators out there,
just remember that Stone only accepts Ruby primitives for
field types (String, Fixnum, etc).
 
 
EOS
end
 
def gen_model(args)
  model_name = args.first.camelcase
  file_name = model_name.snakecase
  args.shift
  fields = Hash[*(args.map{|a| a.split(":") }.flatten)]
  model_str = "class #{model_name}\n include Stone::Resource\n\n"
  for field in fields
    unless field.last == "datetime"
      model_str << " field :#{field.first}, #{field.last.capitalize}\n"
    else
      model_str << " field :#{field.first}, DateTime\n"
    end
  end
  model_str << "end"
  FileUtils.mkdir(File.join(Dir.pwd, "app/models")) \
    unless File.exists?(File.join(Dir.pwd, "app/models"))
  File.open(File.join(Dir.pwd, "app/models/#{file_name}.rb"), "w") do |file|
    file << model_str
  end
  puts "Model: #{model_name} created."
end
 
if ARGV.empty?
  usage
else
  args = ARGV
  case args.first
  when "model"
    args.shift
    if args.empty?
      model_usage
    else
      gen_model(args)
    end
  else
    usage
  end
end