Class: RawQ::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/rawq/generator.rb,
lib/rawq/generator/options.rb,
lib/rawq/generator/application.rb

Defined Under Namespace

Classes: Application, Options

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = []) ⇒ Generator

Returns a new instance of Generator.



14
15
16
17
18
19
20
# File 'lib/rawq/generator.rb', line 14

def initialize(options = [])
  self.options = options
  self.path, self.application_name = File.split(options[:application_name])
  self.music_dir = options[:music_dir] || File.join(self.path, "media/music")
  self.username = options[:username] || self.application_name
  self.password = (0...8).map{('a'..'z').to_a[rand(26)]}.join
end

Instance Attribute Details

#application_nameObject

Returns the value of attribute application_name.



12
13
14
# File 'lib/rawq/generator.rb', line 12

def application_name
  @application_name
end

#music_dirObject

Returns the value of attribute music_dir.



12
13
14
# File 'lib/rawq/generator.rb', line 12

def music_dir
  @music_dir
end

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/rawq/generator.rb', line 12

def options
  @options
end

#passwordObject

Returns the value of attribute password.



12
13
14
# File 'lib/rawq/generator.rb', line 12

def password
  @password
end

#pathObject

Returns the value of attribute path.



12
13
14
# File 'lib/rawq/generator.rb', line 12

def path
  @path
end

#usernameObject

Returns the value of attribute username.



12
13
14
# File 'lib/rawq/generator.rb', line 12

def username
  @username
end

Instance Method Details

#create_dir(dir) ⇒ Object



42
43
44
45
# File 'lib/rawq/generator.rb', line 42

def create_dir(dir)
  $stdout.puts "\tmkdir\t#{dir}"
  Dir.mkdir dir
end

#create_dirsObject



35
36
37
38
39
40
# File 'lib/rawq/generator.rb', line 35

def create_dirs
  if File.exists?(target_dir)
    raise FileExists, "A directory or file at #{self.path} already exists. Aborted."
  end
  create_dir target_dir
end

#create_filesObject



47
48
49
50
51
52
# File 'lib/rawq/generator.rb', line 47

def create_files
  find_templates(".").each do |template|
    next create_dir File.join(target_dir, template) if File.directory?(File.join(template_dir, template))
    create_template template
  end
end

#create_template(source) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rawq/generator.rb', line 70

def create_template(source)
  target = File.join(target_dir, source)
  if File.extname(source) == ".erb"
    template_result = render_template(source)
    target = File.join(File.dirname(target), File.basename(target, ".erb"))
  else
    template_result = File.read(File.join(template_dir, source))
  end

  $stdout.puts "\tcreate\t#{target}"
  File.open(target, "w") { |file| file.write(template_result) }
end

#find_templates(pwd) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/rawq/generator.rb', line 54

def find_templates(pwd)
  templates = []
  Dir.new(File.join(template_dir, pwd)).each do |filename|
    next if [".", ".."].include?(filename)
    templates << File.join(pwd, filename)
    templates = templates + find_templates(File.join(pwd, filename)) if File.directory?(File.join(template_dir, pwd, filename))
  end
  templates
end

#render_template(source) ⇒ Object



64
65
66
67
68
# File 'lib/rawq/generator.rb', line 64

def render_template(source)
  contents = File.read(File.join(template_dir, source))
  template = ERB.new(contents, nil, "<>")
  template.result(binding)
end

#runObject



22
23
24
25
# File 'lib/rawq/generator.rb', line 22

def run
  create_dirs
  create_files
end

#target_dirObject



27
28
29
# File 'lib/rawq/generator.rb', line 27

def target_dir
  File.join(self.path, self.application_name)
end

#template_dirObject



31
32
33
# File 'lib/rawq/generator.rb', line 31

def template_dir
  File.join(File.dirname(__FILE__), 'templates')
end