Class: Generator

Inherits:
WpScaffold::Base show all
Defined in:
lib/wp_scaffold/models/generator.rb

Direct Known Subclasses

CustomPostType, Setting, Theme

Constant Summary

Constants inherited from WpScaffold::Base

WpScaffold::Base::TEMPLATE_BASE, WpScaffold::Base::USER_DIRECTORY

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#destinationObject

Returns the value of attribute destination.



3
4
5
# File 'lib/wp_scaffold/models/generator.rb', line 3

def destination
  @destination
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/wp_scaffold/models/generator.rb', line 3

def name
  @name
end

#relative_to_cwdObject

Returns the value of attribute relative_to_cwd.



3
4
5
# File 'lib/wp_scaffold/models/generator.rb', line 3

def relative_to_cwd
  @relative_to_cwd
end

#template_pathObject

Returns the value of attribute template_path.



3
4
5
# File 'lib/wp_scaffold/models/generator.rb', line 3

def template_path
  @template_path
end

Instance Method Details

#generateObject



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
# File 'lib/wp_scaffold/models/generator.rb', line 5

def generate
	# Path to our template folder
	source = TEMPLATE_BASE + self.template_path

	# For each file in the template source directory
	Dir.glob("#{source}/**/*").each do |file|
		unless File.directory?(file)

			@generator = self

			# template file, plus template directories
			file_name = file.split(source)[1]

			# full path to where the file will be written, including file name
			# Should this be relative to the current working directory?
			if @generator.relative_to_cwd == true
				full_destination = USER_DIRECTORY + self.destination + "/" + file_name
			else
				full_destination = self.destination + "/" + file_name
			end

			# full path to where the file will be written, without file name
			file_directory =  File.dirname(full_destination)

			# Create the directory, if needed
			unless File.directory?(file_directory)
			  FileUtils.mkdir_p(file_directory)
			end

			# complile the erb template, remove the .erb extention, and write to a new file
			File.open(full_destination.gsub('.erb', ''), 'w') do |f|
			  f.write ERB.new(open(file).read).result(binding)
			end

		end
	end # each
end