class Microstation::Changer

Attributes

template[R]
template_filename[R]
the_app[R]

Public Class Methods

new(template, output_dir: nil, app: nil, name: nil) click to toggle source
# File lib/microstation/changer.rb, line 9
def initialize(template, output_dir: nil, app: nil, name: nil)
  check_is_dgn(template)
  @template = template
  @template_filename = File.basename(template)
  @the_app = app
  @output_dir = output_dir || default_outdir(name)
  @name = name || default_name
end

Public Instance Methods

change_in_tempfile(app, &block) click to toggle source
# File lib/microstation/changer.rb, line 37
def change_in_tempfile(app, &block)
  path = Pathname(File.join(::Dir.tmpdir, "#{template_filename}_#{Time.now.to_i}_#{rand(1000)}"))
  app.new_drawing(path, seedfile: @template) do |drawing|
    if block_given?
      block.call(drawing)
    else
      default_block.call(drawing)
    end
  end
  path
rescue MultipleUpdateError => e
  puts "Error while #change_into_tempfile: #{e.message}"
  raise e
end
change_once_in_tempfile(&block) click to toggle source
# File lib/microstation/changer.rb, line 52
def change_once_in_tempfile(&block)
  ::Microstation.run do |app|
    change_in_tempfile(app, &block)
  end
end
default_block() click to toggle source
# File lib/microstation/changer.rb, line 58
def default_block
  ->(d) { d }
end
default_name() click to toggle source
# File lib/microstation/changer.rb, line 66
def default_name
  template_filename
end
default_outdir(_name = nil) click to toggle source
# File lib/microstation/changer.rb, line 62
def default_outdir(_name = nil)
  Pathname.getwd
end
ensure_output_path(odir) click to toggle source
# File lib/microstation/changer.rb, line 18
def ensure_output_path(odir)
  path = Pathname(odir)
  path.mkpath unless path.directory?
  path
end
run(name: nil, output_dir: nil, &block) click to toggle source
# File lib/microstation/changer.rb, line 24
def run(name: nil, output_dir: nil, &block)
  lname = name || @name
  loutput_dir = output_dir || @output_dir
  output_path = ensure_output_path(loutput_dir)
  newname = output_path + lname
  tmp_dgn = the_app ? change_in_tempfile(the_app, &block) : change_once_in_tempfile(&block)
  FileUtils.mv(tmp_dgn.to_s, newname.to_s)
  puts "Saved drawing #{newname}"
rescue StandardError => e
  puts 'error in changing file'
  raise e
end