Class: OSC::Machete::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/osc/machete/location.rb

Overview

A util class with methods used with staging a simulation template directory. Use it by wrapping a file path (either string or Pathname object). For example, if I have a template directory at “/nfs/05/efranz/template” I can recursivly copy the template directory:

target = "/nfs/05/efranz/simulations/1"
simulation = Location.new("/nfs/05/efranz/template").copy_to(target)

Then I can recursively render all the mustache templates in the copied directory, renaming each file from XXXX.mustache to XXXX:

simulation.render(iterations: 20, geometry: "/nfs/05/efranz/geos/fan.stl")

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Location

Returns a new instance of Location.

Parameters:

  • path

    either string, Pathname, or Machete::Location object



22
23
24
25
# File 'lib/osc/machete/location.rb', line 22

def initialize(path)
  @path = Pathname.new(path.to_s).cleanpath
  @template_ext = ".mustache"
end

Instance Method Details

#copy_to(dest) ⇒ Location

Copies the data in a Location to a destination path using rsync.

Parameters:

  • dest (String, Pathname)

    The target location path.

Returns:

  • (Location)

    The target location path wrapped by Location instance.



36
37
38
39
40
41
42
43
44
# File 'lib/osc/machete/location.rb', line 36

def copy_to(dest)
  # @path has / auto-dropped, so we add it to make sure we copy everything
  # in the old directory to the new
  destloc = self.class.new(dest)
  `rsync -r --exclude='.svn' --exclude='.git' --exclude='.gitignore' --filter=':- .gitignore' #{@path.to_s}/ #{destloc.to_s}`

  # return target location so we can chain method
  destloc
end

#render(params, options = {}) ⇒ self

Render each mustache template and rename the file, removing the extension that indicates it is a template file i.e. ‘.mustache`.

Parameters:

  • params (Hash)

    the “context” or “hash” for use when rendering mustache templates

  • options (Hash) (defaults to: {})

    to modify rendering behavior

Options Hash (options):

  • :replace (Boolean) — default: true

    if true will delete the template file after the rendered file is created

Returns:

  • (self)

    returns self for optional chaining



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/osc/machete/location.rb', line 67

def render(params, options = {})
  # custom_delimiters = options['delimeters'] || nil
  replace_template_files = options[:replace].nil? ? true : options[:replace]

  renderer = Mustache.new

  template_files.each do |template|
    rendered_file = template.chomp(@template_ext)

    rendered_string = nil
    File.open(template, 'r') do |f|
      rendered_string = renderer.render(f.read, params)
    end
    # boo...
    # rendered_string = renderer.render_file(template, params)

    File.open(rendered_file, 'w') { |f| f.write(rendered_string) }

    FileUtils.rm template if replace_template_files
  end

  # return self so this can be at the end of a chained method
  self
end

#template_filesArray<String>

**This should be a private method**

Get a list of template files in this Location, where a template file is a file with the extension .mustache

Returns:

  • (Array<String>)

    list of template files in directory (recursively searched)



52
53
54
55
56
57
58
# File 'lib/osc/machete/location.rb', line 52

def template_files
  if @path.directory?
    Dir.glob(File.join(@path, "**/*#{@template_ext}"))
  else
    @path.to_s.end_with? @template_ext ? [@path.to_s] : []
  end
end

#to_sString

Returns The location path as String.

Returns:

  • (String)

    The location path as String.



28
29
30
# File 'lib/osc/machete/location.rb', line 28

def to_s
  @path.to_s
end