Class: OSC::Machete::Location
- Inherits:
-
Object
- Object
- OSC::Machete::Location
- 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
-
#copy_to(dest) ⇒ Location
Copies the data in a Location to a destination path using rsync.
-
#initialize(path) ⇒ Location
constructor
A new instance of Location.
-
#render(params, options = {}) ⇒ self
Render each mustache template and rename the file, removing the extension that indicates it is a template file i.e.
-
#template_files ⇒ Array<String>
**This should be a private method**.
-
#to_s ⇒ String
The location path as String.
Constructor Details
#initialize(path) ⇒ Location
Returns a new instance of Location.
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.
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`.
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, = {}) # custom_delimiters = options['delimeters'] || nil replace_template_files = [:replace].nil? ? true : [: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_files ⇒ Array<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
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_s ⇒ String
Returns The location path as String.
28 29 30 |
# File 'lib/osc/machete/location.rb', line 28 def to_s @path.to_s end |