Class: Roger::Release::Processors::Mockup

Inherits:
Base
  • Object
show all
Defined in:
lib/roger/release/processors/mockup.rb

Overview

The Mockup processor that will process all templates

Constant Summary collapse

MIME_TYPES_TO_EXTENSION =
{
  "text/html" => "html",
  "text/css"  => "css",
  "application/javascript" => "js",
  "text/xml" => "xml",
  "application/xml" => "xml",
  "text/csv" => "csv",
  "application/json" => "json"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mockup

Returns a new instance of Mockup.



16
17
18
19
20
21
22
23
24
# File 'lib/roger/release/processors/mockup.rb', line 16

def initialize(options = {})
  @options = {
    env: {},
    match: ["**/*.{html,md,html.erb}"],
    skip: [/\Astylesheets/, /\Ajavascripts/]
  }

  @options.update(options) if options
end

Instance Attribute Details

#projectObject

Returns the value of attribute project.



4
5
6
# File 'lib/roger/release/processors/mockup.rb', line 4

def project
  @project
end

Instance Method Details

#call(release, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/roger/release/processors/mockup.rb', line 26

def call(release, options = {})
  self.project = release.project

  options = update_call_options(options)

  log_call(options)

  release.get_files(options[:match], options[:skip]).each do |file_path|
    release.log(self, "    Extract: #{file_path}", true)
    self.run_on_file!(file_path, options[:env])
  end
end

#extract_source_from_file(file_path, env = {}) ⇒ Object

Runs the extractor on a single file and return processed source.



56
57
58
59
60
61
62
# File 'lib/roger/release/processors/mockup.rb', line 56

def extract_source_from_file(file_path, env = {})
  Roger::Template.open(
    file_path,
    partials_path: project.partial_path,
    layouts_path: project.layouts_path
  ).render(env.dup)
end

#run_on_file!(file_path, env = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/roger/release/processors/mockup.rb', line 39

def run_on_file!(file_path, env = {})
  template = Roger::Template.open(
    file_path,
    partials_path: project.partial_path,
    layouts_path: project.layouts_path
  )

  # Clean up source file
  FileUtils.rm(file_path)

  # Write out new file
  File.open(target_path(file_path, template).to_s, "w") do |f|
    f.write(template.render(env.dup))
  end
end

#target_path(path, template) ⇒ Pathname

Determines the output path for a mockup path with a certain template

Returns:

  • (Pathname)


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/roger/release/processors/mockup.rb', line 67

def target_path(path, template)
  parts, dir = split_path(path)

  # Always return .html directly as it will cause too much trouble otherwise
  return Pathname.new(path) if parts.last == "html"

  # Strip last extension if we have a double extension
  return dir + parts[0..-2].join(".") if parts.size > 2

  dir + extension_based_on_mime_type(parts, template.template.class.default_mime_type)
end