Class: Microstation::Changer
- Inherits:
-
Object
- Object
- Microstation::Changer
show all
- Includes:
- FileTests
- Defined in:
- lib/microstation/changer.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from FileTests
#check_is_dgn, #check_is_drawing, #drawing?, #drawing_type?, #microstation_drawing?
Constructor Details
#initialize(template, output_dir: nil, app: nil, name: nil) ⇒ Changer
Returns a new instance of Changer.
9
10
11
12
13
14
15
16
|
# 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
|
Instance Attribute Details
#template ⇒ Object
Returns the value of attribute template.
7
8
9
|
# File 'lib/microstation/changer.rb', line 7
def template
@template
end
|
#template_filename ⇒ Object
Returns the value of attribute template_filename.
7
8
9
|
# File 'lib/microstation/changer.rb', line 7
def template_filename
@template_filename
end
|
#the_app ⇒ Object
Returns the value of attribute the_app.
7
8
9
|
# File 'lib/microstation/changer.rb', line 7
def the_app
@the_app
end
|
Instance Method Details
#change_in_tempfile(app, &block) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# 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) ⇒ Object
52
53
54
55
56
|
# 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 ⇒ Object
58
59
60
|
# File 'lib/microstation/changer.rb', line 58
def default_block
->(d) { d }
end
|
#default_name ⇒ Object
66
67
68
|
# File 'lib/microstation/changer.rb', line 66
def default_name
template_filename
end
|
#default_outdir(_name = nil) ⇒ Object
62
63
64
|
# File 'lib/microstation/changer.rb', line 62
def default_outdir(_name = nil)
Pathname.getwd
end
|
#ensure_output_path(odir) ⇒ Object
18
19
20
21
22
|
# 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) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
|
# 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
|