Class: Microstation::Dir

Inherits:
Object
  • Object
show all
Includes:
FileUtils::Verbose, PdfSupport
Defined in:
lib/microstation/dir.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PdfSupport

#file_exists?, #mtime, #needs_pdf?, #pdf_exists?, #pdf_name, #pdf_older?

Constructor Details

#initialize(dir, pdf_path = nil) ⇒ Dir

Returns a new instance of Dir.



12
13
14
15
# File 'lib/microstation/dir.rb', line 12

def initialize(dir,pdf_path=nil)
  @dir = Pathname(dir).expand_path
  @relative_pdf_path = set_relative_pdf_path(pdf_path)
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



10
11
12
# File 'lib/microstation/dir.rb', line 10

def dir
  @dir
end

#relative_pdf_pathObject

Returns the value of attribute relative_pdf_path.



10
11
12
# File 'lib/microstation/dir.rb', line 10

def relative_pdf_path
  @relative_pdf_path
end

Class Method Details

.Dir(path) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/microstation/dir.rb', line 17

def self.Dir(path)
  case path
  when Microstation::Dir
    path
  else
    new(Pathname(path))
  end
end

Instance Method Details

#+(other) ⇒ Object



55
56
57
# File 'lib/microstation/dir.rb', line 55

def +(other)
  self.class.new( self.path + other)
end

#==(other) ⇒ Object



26
27
28
# File 'lib/microstation/dir.rb', line 26

def ==(other)
  self.dir == other.dir && self.relative_pdf_path == other.relative_pdf_path
end

#concat_pdfs(files, name) ⇒ Object



108
109
110
# File 'lib/microstation/dir.rb', line 108

def concat_pdfs(files)
  Pdftk.concat(files)
end

#copy(drawing, dir) ⇒ Object



60
61
62
# File 'lib/microstation/dir.rb', line 60

def copy(drawing,dir)
  cp drawing, dir
end

#directory?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/microstation/dir.rb', line 38

def directory?
  path.directory?
end

#drawing_filesObject



88
89
90
91
92
# File 'lib/microstation/dir.rb', line 88

def drawing_files
  return @drawing_files if @drawing_files
  set_drawing_files(drawings.map{|dwg| Microstation::Drawing::File.new(dwg)} )
  @drawing_files
end

#drawing_files_needing_pdf(dir = pdf_dirname) ⇒ Object



112
113
114
# File 'lib/microstation/dir.rb', line 112

def drawing_files_needing_pdf(dir = pdf_dirname)
  drawing_files.select{|d| d.needs_pdf?(dir) }
end

#drawingsObject



75
76
77
# File 'lib/microstation/dir.rb', line 75

def drawings
  Pathname.glob(@dir + "*.d{gn,wg}")
end

#exist?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/microstation/dir.rb', line 42

def exist?
  path.exist?
end

#find_by_name(re) ⇒ Object



70
71
72
73
# File 'lib/microstation/dir.rb', line 70

def find_by_name(re)
  re = Regexp.new(re)
  drawing_files.find{|n| n.to_path =~ re}
end

#find_pdftk(dirs = nil) ⇒ Object



127
128
129
130
# File 'lib/microstation/dir.rb', line 127

def find_pdftk(dirs = nil)
  require 'pdf_forms'
  @pdftk = PdfForms.new('e:/tools/pdftk-1.44/bin/pdftk.exe')
end

#generate_pdfs(dir = pdf_dirname) ⇒ Object



122
123
124
125
# File 'lib/microstation/dir.rb', line 122

def generate_pdfs(dir = pdf_dirname)
  print_pdfs(dir)
  concat_pdfs(pdf_files(dir), dir + "dir.combined.drawings.pdf")
end

#get_meta_for_drawingsObject



144
145
146
147
148
149
150
151
# File 'lib/microstation/dir.rb', line 144

def get_meta_for_drawings
  @drawing_files = nil
  files = []
  with_drawing_files( drawings) do |drawing|
    files << Microstation::Drawing::File.from_drawing(drawing)
  end
  set_drawing_files(files)
end

#mkdirObject



47
48
49
# File 'lib/microstation/dir.rb', line 47

def mkdir
  @dir.mkdir
end

#mkpathObject



51
52
53
# File 'lib/microstation/dir.rb', line 51

def mkpath
  @dir.mkpath
end

#pathObject



34
35
36
# File 'lib/microstation/dir.rb', line 34

def path
  @dir
end

#pdf_dirnameObject



157
158
159
160
161
162
163
# File 'lib/microstation/dir.rb', line 157

def pdf_dirname
  if relative_pdf_path.absolute?
    relative_pdf_path
  else
    dir + relative_pdf_path
  end
end

#pdf_files(dir = pdf_dirname) ⇒ Object



104
105
106
# File 'lib/microstation/dir.rb', line 104

def pdf_files(dir = pdf_dirname)
  sort(drawing_files).map{|p| p.pdf_name(dir)}
end

#pdf_generation_complete?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/microstation/dir.rb', line 99

def pdf_generation_complete?
  drawing_files.all?{|d| not d.needs_pdf?}
end

#pdftkObject



132
133
134
# File 'lib/microstation/dir.rb', line 132

def pdftk
  @pdftk ||= find_pdftk(dirs= nil)
end


116
117
118
119
120
# File 'lib/microstation/dir.rb', line 116

def print_pdfs(dir = pdf_dirname)
  with_drawing_files( drawing_files_needing_pdf) do |drawing|
    drawing.save_as_pdf(:dir => dir)
  end
end

#select_by_name(re) ⇒ Object



64
65
66
67
# File 'lib/microstation/dir.rb', line 64

def select_by_name( re)
  re = Regexp.new(re)
  drawing_files.select{|n| n.to_path =~ re}
end

#set_drawing_files(dfiles) ⇒ Object



94
95
96
# File 'lib/microstation/dir.rb', line 94

def set_drawing_files(dfiles)
  @drawing_files = sort(dfiles)
end

#set_relative_pdf_path(path) ⇒ Object



165
166
167
168
# File 'lib/microstation/dir.rb', line 165

def set_relative_pdf_path(path)
  rel_path = path.nil? ? "." : path
  Pathname(rel_path)
end

#sort(array_of_files) ⇒ Object



83
84
85
86
# File 'lib/microstation/dir.rb', line 83

def sort(array_of_files)
  sort_lambda = @sort_by || lambda{|f| f.path.to_s}
  array_of_files.sort_by(&sort_lambda)
end

#sort_by(&block) ⇒ Object



79
80
81
# File 'lib/microstation/dir.rb', line 79

def sort_by(&block)
  @sort_by = block
end

#to_pathObject



30
31
32
# File 'lib/microstation/dir.rb', line 30

def to_path
  @dir.to_path
end

#with_drawing_files(dwgs = drawing_files, &block) ⇒ Object



153
154
155
# File 'lib/microstation/dir.rb', line 153

def with_drawing_files(dwgs = drawing_files, &block)
  Microstation.with_drawings(dwgs,&block)
end