Class: DirToXML

Inherits:
Object
  • Object
show all
Includes:
RXFReadWriteModule
Defined in:
lib/dir-to-xml.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj = '.', index: 'dir.json', recursive: false, verbose: true, debug: false) ⇒ DirToXML

Returns a new instance of DirToXML.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dir-to-xml.rb', line 38

def initialize(obj= '.', index: 'dir.json', recursive: false,
               verbose: true, debug: false)

  if verbose then
    puts
    puts 'DirToXML at your service!'.highlight
    puts
    puts
  end

  @index, @recursive, @verbose, @debug = index, recursive, verbose, debug

  if File.basename(obj) == index then

    #read the index file
    @path = File.dirname(obj)
    puts 'intialize() @path: ' + @path.inspect if @debug

    @dx = read(index)

  else
    @path = obj
    puts 'intialize() @path: ' + @path.inspect if @debug

    new_scan()
  end

end

Instance Attribute Details

#deleted_filesObject (readonly)

Returns the value of attribute deleted_files.



36
37
38
# File 'lib/dir-to-xml.rb', line 36

def deleted_files
  @deleted_files
end

#dxObject (readonly)

Returns the value of attribute dx.



36
37
38
# File 'lib/dir-to-xml.rb', line 36

def dx
  @dx
end

#latest_fileObject (readonly)

Returns the value of attribute latest_file.



36
37
38
# File 'lib/dir-to-xml.rb', line 36

def latest_file
  @latest_file
end

#latest_filesObject (readonly)

Returns the value of attribute latest_files.



36
37
38
# File 'lib/dir-to-xml.rb', line 36

def latest_files
  @latest_files
end

#new_filesObject (readonly)

Returns the value of attribute new_files.



36
37
38
# File 'lib/dir-to-xml.rb', line 36

def new_files
  @new_files
end

Instance Method Details

#activityObject Also known as: changes



67
68
69
70
71
72
73
# File 'lib/dir-to-xml.rb', line 67

def activity()
  {
    new: @new_files,
    deleted: @deleted_files,
    modified: @latest_files
  }
end

#directories(recursive: false) ⇒ Object

Returns a list of directory names

note: if recursive is true then it will return a 1-dimensional Array

object containing all directory paths including nested directories


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/dir-to-xml.rb', line 82

def directories(recursive: false)

  a = @dx.all
  puts 'inside directories() a: ' + a.inspect if @debug

  if recursive then

    directories.flat_map do |dir_name|

      #puts 'dir_name: ' + dir_name.inspect
      #puts 'path+dir: ' + File.join(@path, dir_name).inspect
      dtx = DirToXML.new(File.join(@path, dir_name), verbose: false, debug: false)
      list = dtx.directories(recursive: true)
      r = [File.join(@path, dir_name)]
      r.concat list if list.any?

      r
    end

  else
    a.select {|x| x.type == 'directory'}.map(&:name)
  end

end

#find_all_by_ext(s) ⇒ Object



107
108
109
# File 'lib/dir-to-xml.rb', line 107

def find_all_by_ext(s)
  @dx.find_all_by_ext(s)
end

#find_by_filename(s) ⇒ Object



111
112
113
# File 'lib/dir-to-xml.rb', line 111

def find_by_filename(s)
  @dx.find_by_filename(s)
end

#latestObject



115
116
117
118
119
120
121
# File 'lib/dir-to-xml.rb', line 115

def latest()

  if @latest_file then
    File.join(@latest_file[:path], @latest_file[:name])
  end

end

#new_scanObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/dir-to-xml.rb', line 123

def new_scan()

  t = Time.now
  puts '_new_scan() @path: ' + @path.inspect if @debug
  records = scan_dir @path
  puts '_new_scan() records: ' + records.inspect if @debug

  a = records.map {|x| x[:name]}

  if FileX.exist? File.join(@path, @index) then

    @dx = read()

    old_records = @dx.to_a
    a2 = old_records.map {|x| x[:name]}

    # delete any old files
    #
    @deleted_files = a2 - a

    if @deleted_files.any? then

      @deleted_files.each do |filename|
        record = @dx.find_by_name filename
        record.delete if record
      end

    end

    # Add any new files
    #
    @new_files = a - a2

    if @new_files.any? then
      @dx.import @new_files.map {|filename| getfile_info(filename) }
    end

    if (@deleted_files +  @new_files).any? then

      @dx.last_modified = Time.now.to_s
      @dx.save

      new_scan()

    else

      # check for newly modified files
      # compare the file date with index file last modified date
      #
      dtx_last_modified = Time.parse(@dx.last_modified)

      select_records = records.select do |file|

        file[:mtime] > dtx_last_modified or file[:type] == 'directory'

      end

      puts 'select_records: ' + select_records.inspect if @debug

      find_latest(select_records) if select_records.any?
    end


  else

    @dx = new_index(records)
    find_latest(records) if records.any?

  end

  t2 = Time.now - t
  puts ("directory scanned in %.2f seconds" % t2).info if @verbose

end

#read(index = @index) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/dir-to-xml.rb', line 198

def read(index=@index)

  t = Time.now
  puts 'read path: ' + File.join(@path, index).inspect if @debug

  dx = DxLite.new(File.join(@path, index), autosave: false)

  t2 = Time.now - t
  puts ("%s read in %.2f seconds" % [@index, t2]).info if @verbose

  return dx

end