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.



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

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.



8
9
10
# File 'lib/microstation/dir.rb', line 8

def dir
  @dir
end

#relative_pdf_pathObject

Returns the value of attribute relative_pdf_path.



8
9
10
# File 'lib/microstation/dir.rb', line 8

def relative_pdf_path
  @relative_pdf_path
end

Class Method Details

.Dir(path) ⇒ Object



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

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

Instance Method Details

#+(other) ⇒ Object



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

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

#==(other) ⇒ Object



24
25
26
# File 'lib/microstation/dir.rb', line 24

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

#concat_pdfs(files, name) ⇒ Object



101
102
103
# File 'lib/microstation/dir.rb', line 101

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

#copy(drawing, dir) ⇒ Object



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

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

#directory?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/microstation/dir.rb', line 36

def directory?
  path.directory?
end

#drawing_filesObject



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

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



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

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

#drawingsObject



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

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

#exist?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/microstation/dir.rb', line 40

def exist?
  path.exist?
end

#find_by_name(re) ⇒ Object



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

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

#find_pdftk(dirs = nil) ⇒ Object



120
121
122
123
# File 'lib/microstation/dir.rb', line 120

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



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

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

#get_meta_for_drawingsObject



134
135
136
137
138
139
140
141
# File 'lib/microstation/dir.rb', line 134

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



44
45
46
# File 'lib/microstation/dir.rb', line 44

def mkdir
  @dir.mkdir
end

#mkpathObject



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

def mkpath
  @dir.mkpath
end

#pathObject



32
33
34
# File 'lib/microstation/dir.rb', line 32

def path
  @dir
end

#pdf_dirnameObject



147
148
149
150
151
152
153
# File 'lib/microstation/dir.rb', line 147

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

#pdf_files(dir = pdf_dirname) ⇒ Object



97
98
99
# File 'lib/microstation/dir.rb', line 97

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

#pdf_generation_complete?Boolean

Returns:

  • (Boolean)


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

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

#pdftkObject



125
126
127
# File 'lib/microstation/dir.rb', line 125

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


109
110
111
112
113
# File 'lib/microstation/dir.rb', line 109

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



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

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

#set_drawing_files(dfiles) ⇒ Object



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

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

#set_relative_pdf_path(path) ⇒ Object



155
156
157
158
# File 'lib/microstation/dir.rb', line 155

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

#sort(array_of_files) ⇒ Object



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

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



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

def sort_by(&block)
  @sort_by = block
end

#to_pathObject



28
29
30
# File 'lib/microstation/dir.rb', line 28

def to_path
  @dir.to_path
end

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



143
144
145
# File 'lib/microstation/dir.rb', line 143

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