Class: YMDP::Compiler::Template::Base

Inherits:
Base
  • Object
show all
Defined in:
lib/ymdp/compiler/template.rb

Overview

Process source files into usable code.

Source files can be HTML, Haml, ERB, JavaScript, or CSS files.

Files with an extension of “.haml” will be processed with Haml, all others will use ERB.

Examples

YMDP::Compiler::Template::View.new(params).build

YMDP::Compiler::Template::JavaScript.new(params).build

@template = YMDP::Compiler::Template::Base.new(params)

Options

verbose::     boolean value, output verbose notices,
domain::      string, indicates which domain the template is compiling to,
file::        filename of the template in question,
hash::        git hash of the latest commit,
message::     commit message of the latest commit.

Direct Known Subclasses

View, YRB

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#base_path, base_path, #configuration, configuration, configure, #content_variables, display_path, #display_path, #paths, #servers

Constructor Details

#initialize(params) ⇒ Base

Returns a new instance of Base.

Raises:

  • (StandardError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ymdp/compiler/template.rb', line 34

def initialize(params)
  @verbose = params[:verbose]
  @domain = params[:domain]
  @host = params[:host]
  
  server_settings = servers[@domain]
  if server_settings
    @server = server_settings["server"]
  else
    raise StandardError.new("Server settings are required.")
  end
  
  raise StandardError.new("Server name does not exist in server settings.") unless @server
  
  @file = params[:file]
  @assets_directory = "/om/assets/#{servers[@domain]['assets_id']}"
  @hash = params[:git_hash]
  @message = params[:message]
  
  set_content_variables
  
  @view = base_filename(@file.split("/").last)
  Application.current_view = @view
end

Instance Attribute Details

#assets_directoryObject

Returns the value of attribute assets_directory.



32
33
34
# File 'lib/ymdp/compiler/template.rb', line 32

def assets_directory
  @assets_directory
end

#domainObject

Returns the value of attribute domain.



32
33
34
# File 'lib/ymdp/compiler/template.rb', line 32

def domain
  @domain
end

#fileObject

Returns the value of attribute file.



32
33
34
# File 'lib/ymdp/compiler/template.rb', line 32

def file
  @file
end

#hashObject

Returns the value of attribute hash.



32
33
34
# File 'lib/ymdp/compiler/template.rb', line 32

def hash
  @hash
end

#hostObject

Returns the value of attribute host.



32
33
34
# File 'lib/ymdp/compiler/template.rb', line 32

def host
  @host
end

#messageObject

Returns the value of attribute message.



32
33
34
# File 'lib/ymdp/compiler/template.rb', line 32

def message
  @message
end

#serverObject

Returns the value of attribute server.



32
33
34
# File 'lib/ymdp/compiler/template.rb', line 32

def server
  @server
end

Instance Method Details

#base_filename(filename) ⇒ Object



107
108
109
# File 'lib/ymdp/compiler/template.rb', line 107

def base_filename(filename)
  raise "Define in child"
end

#buildObject

Compile this view unless it is a partial.



86
87
88
89
90
91
# File 'lib/ymdp/compiler/template.rb', line 86

def build
  # puts "Base build"
  unless partial?
    write_template(processed_template)
  end
end

#destination_pathObject

Produces the destination path of this template, in the servers directory for the given domain.

Examples

If the source file is:

app/views/authorize.html.haml

The destination file will be:

servers/staging/views/authorize.html.haml


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ymdp/compiler/template.rb', line 130

def destination_path
  # just the file, with no directory
  filename = File.basename(@file)

  # just the filename, with no extension
  filename = convert_filename(filename)

  # just the directory, with no file 
  directory = File.dirname(@file)

  # replace the app directory with the server directory
  relative_directory = directory.gsub!("#{paths[:base_path]}/app", server_path)

  # make the directory if it doesn't exist
  FileUtils.mkdir_p(relative_directory)

  "#{relative_directory}/#{filename}"
end

#partial?Boolean

If the filename begins with a _ it’s a partial.

Returns:

  • (Boolean)


80
81
82
# File 'lib/ymdp/compiler/template.rb', line 80

def partial?
  File.basename(@file) =~ /^_/
end

#process_template(template) ⇒ Object

Implemented in child classes, this defines what must be done to process a template.



113
114
115
# File 'lib/ymdp/compiler/template.rb', line 113

def process_template(template)
  raise "Define in child"
end

#processed_templateObject

Returns the compiled template code after its Haml or ERB has been processed.



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ymdp/compiler/template.rb', line 95

def processed_template
  # "Base processed_template"
  result = ""
  template = File.read(@file)
  if @file =~ /\.haml$/
    result = process_haml(template, @file)
  else
    result = process_template(template)
  end
  result
end

#server_pathObject

Path to the servers directory for the current domain.



151
152
153
# File 'lib/ymdp/compiler/template.rb', line 151

def server_path
  "#{servers_path}/#{@domain}"
end

#servers_pathObject



209
210
211
# File 'lib/ymdp/compiler/template.rb', line 209

def servers_path
  "#{paths[:base_path]}/servers"
end

#set_content_variablesObject

Parsed from the file ‘content.yml’ each of its keys is added to the environment as an instance variable, so they will be available inside the template.



68
69
70
71
72
73
74
75
76
# File 'lib/ymdp/compiler/template.rb', line 68

def set_content_variables
  content_variables.to_a.each do |key, value|
    attribute = "@#{key}"
    instance_variable_set(attribute, value) unless instance_variable_defined?(attribute)
    class_eval %(
      attr_accessor :#{key}
    )
  end
end

#verbose(message) ⇒ Object

Outputs a message if @verbose is on.



157
158
159
# File 'lib/ymdp/compiler/template.rb', line 157

def verbose(message)
  $stdout.puts(message) if @verbose
end

#verbose?Boolean

Is the verbose setting on?

Returns:

  • (Boolean)


61
62
63
# File 'lib/ymdp/compiler/template.rb', line 61

def verbose?
  @verbose
end

#write_template(result) ⇒ Object

Write this processed template to its destination file.

Overwrite this method in child class to define whether the class uses a template or not.



204
205
206
207
# File 'lib/ymdp/compiler/template.rb', line 204

def write_template(result)
  # puts "Base write_template"
  write_template_with_layout(result)
end

#write_template_with_layout(result) ⇒ Object

Writes the input string to the destination file, passing it through the application template.

The application layout can be either Haml or ERB.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/ymdp/compiler/template.rb', line 181

def write_template_with_layout(result)
  # puts "Base write_template_with_layout"
  @content = result
  layout = result
  
  application_layout = "#{paths[:base_path]}\/app\/views\/layouts\/application.html"
  haml_layout = application_layout + ".haml"

  if File.exists?(haml_layout)
    template = File.read(haml_layout)
    layout = process_haml(template, haml_layout) do
      @content
    end
  end

  write_template_without_layout(layout)
end

#write_template_without_layout(result) ⇒ Object

Writes the input string to the destination file without adding any layout.



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/ymdp/compiler/template.rb', line 163

def write_template_without_layout(result)
  path = destination_path
  
  # puts "\n\n\nBase write_template_without_layout: #{result}, #{path}"
  
  File.open(path, "w") do |f|
    f.write(result)
  end
  verbose "Finished writing #{path}.\n"
  
  result
end