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, Yaml

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
58
# 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 = "/yahoo/mail/assets"
  @assets_url = "http://#{servers[@domain]['application_id']}.yom.mail.yahoo.net#{@assets_directory}"
  @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



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

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

#buildObject

Compile this view unless it is a partial.



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

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


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

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)


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

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

#process_erb(template) ⇒ Object

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



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

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

#processed_templateObject

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



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

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

#server_pathObject

Path to the servers directory for the current domain.



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

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

#servers_pathObject



219
220
221
# File 'lib/ymdp/compiler/template.rb', line 219

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.



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

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

#verbose(message) ⇒ Object

Outputs a message if @verbose is on.



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

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

#verbose?Boolean

Is the verbose setting on?

Returns:

  • (Boolean)


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

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.



214
215
216
217
# File 'lib/ymdp/compiler/template.rb', line 214

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.



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/ymdp/compiler/template.rb', line 182

def write_template_with_layout(result)
  # puts "Base write_template_with_layout"
  @content = result
  layout = result
  
  view_layout = "#{paths[:base_path]}\/app\/views\/layouts\/#{@view}.html"
  application_layout = "#{paths[:base_path]}\/app\/views\/layouts\/application.html"
  
  haml_view_layout = view_layout + ".haml"
  haml_application_layout = application_layout + ".haml"
  
  if File.exists?(haml_view_layout)
    layout_name = haml_view_layout
  else
    layout_name = haml_application_layout
  end

  if File.exists?(layout_name)
    template = File.read(layout_name)
    layout = process_haml(template, layout_name) 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.



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

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