Class: Clearwater::Roda::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/clearwater/roda/application.rb

Constant Summary collapse

DirectoryAlreadyExists =
Class.new(StandardError)
TEMPLATE_PATH =
File.expand_path('../../../../templates', __FILE__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Application

Returns a new instance of Application.



14
15
16
# File 'lib/clearwater/roda/application.rb', line 14

def initialize name
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/clearwater/roda/application.rb', line 12

def name
  @name
end

Instance Method Details

#buildObject



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
# File 'lib/clearwater/roda/application.rb', line 18

def build
  dir_must_not_exist!

  mkdir
  chdir
  write_files
  git_init

  puts
  puts <<-EOF
Thanks for using Clearwater::Roda!

To run your new app:

    $ cd #{dir_name}
    $ ./dev

Then point your browser to http://localhost:9292/

The Clearwater app lives in #{Dir.pwd}/#{dir_name}/assets/js/app.rb. To compile
your assets for production, simply run:

    $ rake assets:precompile

  EOF
end

#chdirObject



49
50
51
# File 'lib/clearwater/roda/application.rb', line 49

def chdir
  FileUtils.chdir dir_name
end

#class_nameObject



110
111
112
# File 'lib/clearwater/roda/application.rb', line 110

def class_name
  titleize name
end

#dir_must_not_exist!Object



104
105
106
107
108
# File 'lib/clearwater/roda/application.rb', line 104

def dir_must_not_exist!
  if File.exist? dir_name
    raise DirectoryAlreadyExists, "directory #{dir_name} already exists!"
  end
end

#dir_nameObject



114
115
116
# File 'lib/clearwater/roda/application.rb', line 114

def dir_name
  underscore name
end

#git_initObject



53
54
55
# File 'lib/clearwater/roda/application.rb', line 53

def git_init
  `git init`
end

#mkdirObject



45
46
47
# File 'lib/clearwater/roda/application.rb', line 45

def mkdir
  FileUtils.mkdir_p dir_name
end

#template_optionsObject



83
84
85
86
87
88
# File 'lib/clearwater/roda/application.rb', line 83

def template_options
  {
    underscored_name: dir_name,
    titleized_name: class_name,
  }
end

#template_path(name) ⇒ Object



90
91
92
# File 'lib/clearwater/roda/application.rb', line 90

def template_path(name)
  "#{TEMPLATE_PATH}/#{name}"
end

#titleize(name) ⇒ Object



98
99
100
101
102
# File 'lib/clearwater/roda/application.rb', line 98

def titleize name
  name = name.gsub(/[_-][a-z]/) { |match| match[1].upcase }
  name[0] = name[0].upcase
  name
end

#underscore(name) ⇒ Object



94
95
96
# File 'lib/clearwater/roda/application.rb', line 94

def underscore name
  name.gsub(/\w[A-Z]/) { |match| "#{match[0]}_#{match[1]}" }.downcase
end

#write_filesObject



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
# File 'lib/clearwater/roda/application.rb', line 57

def write_files
  files = Dir["#{TEMPLATE_PATH}/**/*"]
    .select { |file| File.file? file }
    .map { |file| file.sub("#{TEMPLATE_PATH}/", '') }
    .grep_v('app.rb')
  
  files.each do |template_name|
    puts "Writing #{template_name}..."
    Template.new(
      template_path(template_name),
      template_name,
      template_options,
    ).write
  end

  app_filename = "#{underscore(name)}.rb"
  puts "Writing #{app_filename}..."
  Template.new(
    template_path('app.rb'),
    app_filename,
    template_options,
  ).write

  FileUtils.chmod 0744, 'dev'
end