Class: Nali::Generator

Inherits:
Object show all
Defined in:
lib/nali/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Generator

Returns a new instance of Generator.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/nali/generator.rb', line 5

def initialize( args )
  if args.first == 'new'
    if args[1] then create_application args[1]
    else puts 'Enter a name for the application' end
  elsif [ 'm', 'model' ].include?( args.first )
    if args[1] then create_model args[1]
    else puts 'Enter a name for the model' end
  elsif [ 'v', 'view' ].include?( args.first )
    if args[1] then create_view args[1]
    else puts 'Enter a name for the view' end
  end  
end

Instance Method Details

#clean_cacheObject



51
52
53
# File 'lib/nali/generator.rb', line 51

def clean_cache
  FileUtils.rm_rf File.join( Dir.pwd, 'tmp/cache' )
end

#create_application(name) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/nali/generator.rb', line 18

def create_application( name )
  source = File.join Nali.path, 'generator/application/.'
  target = File.join Dir.pwd, name
  FileUtils.cp_r source, target
  %w(
    db
    db/migrate
    lib
    lib/client
    lib/client/javascripts
    lib/client/stylesheets
    public
    public/client
    tmp
    vendor
    vendor/client
    vendor/client/javascripts
    vendor/client/stylesheets
    config/initializers
  ).each { |dir| unless Dir.exists?( path = File.join( target, dir ) ) then Dir.mkdir( path ) end }
  puts "Application #{ name } created"
end

#create_model(name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/nali/generator.rb', line 55

def create_model( name )
  if Dir.exists?( File.join( Dir.pwd, 'app' ) )
    if name.scan( '_' ).size > 0
      return puts 'Please don\'t use the underscore'
    end
    clean_cache
    filename  = name.downcase
    classname = name.camelize
    write "app/client/javascripts/models/#{ filename }.js.coffee", render( 'client_model', classname )
    write "app/client/javascripts/controllers/#{ filename }s.js.coffee", render( 'client_controller', classname )
    write "app/server/models/#{ filename }.rb", render( 'server_model', classname )
    write "app/server/controllers/#{ filename }s_controller.rb", render( 'server_controller', classname )
    write "app/server/models/access.yml", render( 'server_model_access', classname ), 'a'
  else puts 'Please go to the application folder' end
end

#create_view(name) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/nali/generator.rb', line 71

def create_view( name )
  if Dir.exists?( File.join( Dir.pwd, 'app' ) )
    dirname, *filename = name.underscore.split( '_' )
    filename  = filename.join( '_' )
    classname = name.underscore.camelize
    if not dirname.empty? and not filename.empty? and not classname.empty?
      clean_cache
      [
        "app/client/javascripts/views/#{ dirname }",
        "app/client/stylesheets/#{ dirname }",
        "app/client/templates/#{ dirname }"
      ].each { |dir| unless Dir.exists?( path = File.join( Dir.pwd, dir ) ) then Dir.mkdir( path ) end }
      write "app/client/javascripts/views/#{ dirname }/#{ filename }.js.coffee", render( 'client_view', classname )
      write "app/client/stylesheets/#{ dirname }/#{ filename }.css.sass", render( 'client_view_styles', classname )
      write "app/client/templates/#{ dirname }/#{ filename }.html", ''
    else puts 'Invalid view name' end
  else puts 'Please go to the application folder' end
end

#render(name, classname) ⇒ Object



41
42
43
44
# File 'lib/nali/generator.rb', line 41

def render( name, classname )
  require 'erb'
  ERB.new( File.read( File.join( Nali.path, 'generator/templates', "#{ name }.tpl" )  ) ).result binding
end

#write(path, content, mode = 'w') ⇒ Object



46
47
48
49
# File 'lib/nali/generator.rb', line 46

def write( path, content, mode = 'w' )
  File.open( File.join( Dir.pwd, path ), mode ) { |file| file.write( content ) }
  puts ( mode == 'a' ? 'Updated: ' : 'Created: ' ) + path
end