Class: YMDP::Generator::Templates::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/view.rb

Overview

Basic processing of templates (ERB by default).

Direct Known Subclasses

JavaScript, Modifications, Stylesheet, Translation, View

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view, params = {}) ⇒ Base

View name should be a single word that’s valid as a filename.

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
# File 'lib/view.rb', line 36

def initialize(view, params={})
  @view = view
  @template_path = params[:template_path]
  @application_path = params[:application_path]
  
  raise ArgumentError.new("template_path is required") unless @template_path
  raise ArgumentError.new("application_path is required") unless @application_path
end

Instance Attribute Details

#application_pathObject

Returns the value of attribute application_path.



32
33
34
# File 'lib/view.rb', line 32

def application_path
  @application_path
end

#template_pathObject

Returns the value of attribute template_path.



32
33
34
# File 'lib/view.rb', line 32

def template_path
  @template_path
end

#viewObject

Returns the value of attribute view.



32
33
34
# File 'lib/view.rb', line 32

def view
  @view
end

Instance Method Details

#append_to_file(file, content) ⇒ Object

Append the content to a file and log that it’s happening.



89
90
91
92
93
94
# File 'lib/view.rb', line 89

def append_to_file(file, content)
  File.open(file, "a") do |f|
    $stdout.puts "  #{display_path(file)} writing . . ."
    f.puts content
  end
end

#confirm_overwrite(path) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/view.rb', line 135

def confirm_overwrite(path)
  if File.exists?(path)
    $stdout.puts "File exists: #{File.expand_path(path)}"
    $stdout.print "  overwrite? (y/n)"
    answer = $stdin.gets

    answer =~ /^y/i
  else
    true
  end          
end

#destination_dirObject

The destination directory based on the application_path and type of this object.



125
126
127
# File 'lib/view.rb', line 125

def destination_dir
  raise "Define in child"
end

#destination_filenameObject

The destination filename based on the view name of this object.



119
120
121
# File 'lib/view.rb', line 119

def destination_filename
  raise "Define in child"
end

#destination_pathObject

Generate the destination path for this file from its destination directory and its filename.



101
102
103
# File 'lib/view.rb', line 101

def destination_path
  "#{destination_dir}/#{destination_filename}"
end

#display_path(path) ⇒ Object

friendlier display of paths



130
131
132
133
# File 'lib/view.rb', line 130

def display_path(path)
  path = File.expand_path(path)
  path.gsub(BASE_PATH, "")
end

#generateObject

Write the template to its destination_path after it has been processed.



47
48
49
# File 'lib/view.rb', line 47

def generate
  write_processed_template
end

#process_template(content) ⇒ Object

Process this template. By default templates are processed with ERB.

Override this in a child to change this behavior.

content should be a string, which contains the unprocessed source code of the file.



60
61
62
# File 'lib/view.rb', line 60

def process_template(content)
  ERB.new(content, 0, "%<>").result(binding)
end

#processed_templateObject

Return the content of this file after it has been processed.

Read the unprocessed content from the source_path and pass it to process_template and return the result.



69
70
71
72
73
74
75
# File 'lib/view.rb', line 69

def processed_template
  unprocessed_content = ""
  File.open(source_path) do |f|
    unprocessed_content = f.read
  end
  process_template(unprocessed_content)
end

#source_filenameObject

The source filename for the blank template file.



113
114
115
# File 'lib/view.rb', line 113

def source_filename
  raise "Define in child"
end

#source_pathObject

Generate the source path from the template directory and the source filename.



107
108
109
# File 'lib/view.rb', line 107

def source_path
  "#{template_path}/#{source_filename}"
end

#write_processed_templateObject

Write the result of processed_template to the destination path.



81
82
83
84
85
# File 'lib/view.rb', line 81

def write_processed_template
  if confirm_overwrite(destination_path)
    append_to_file(destination_path, processed_template)
  end
end