Class: SportDb::Pathspecs

Inherits:
Object
  • Object
show all
Defined in:
lib/fbtok/pathspec.rb

Overview

change to a module - why? why not?

Class Method Summary collapse

Class Method Details

.build(args, path: [], filepack: nil) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/fbtok/pathspec.rb', line 198

def self.build( args, path: [],
                      filepack: nil )
  recs = []

  ## check fo no args case  (and filepack present with default)
  if args.empty?
    if filepack && filepack.has_key?('default')
               recs << { 'path'      => '<default>',
                         'datafiles' => filepack['default'] }
    end
  else

    ## note - collect all "loose/standalone" files (NOT directories)
    ##             in single default pathspec node
    more = []

    args.each do |arg|
       ## note - ALWAYS give priority to existing directory matches (over filepack)
       ##             e.g. euro or such -  why? why not?
       ##
       ##  todo/check  if euro/ works too?
       ## check if directory
       if Dir.exist?( arg )
          recs << { 'path'       => arg,
                    'datafiles'  => Pathspec.find( arg ) }
       elsif filepack && filepack.has_key?( arg.downcase )
           recs << { 'path'      => "<#{arg.downcase}>",
                     'datafiles' => filepack[arg.downcase] }
       else   ## assume it's a file
           file = find_file( arg, path: path )
            ## check if file (exists) in any path lookup
            if file
              ## todo/fix:
              ##   add File.expand_path upstream in find_file !!!
              ##           and remove later here
              ##       also fix upstream Errorno::ENOENT to Errno::ENOENT !!!
              ##
              ## make sure path exists; raise error if not
               ## (auto-)expand path to normalize - yes why? why not?
              more << File.expand_path(file)
            else
              raise Errno::ENOENT, "No such file or directory - #{arg}"
            end
       end
    end

    if more.size > 0
      recs << { 'path'      => '<input>',
                'datafiles' =>  more }
    end
  end

  recs
end

.debug=(value) ⇒ Object



167
# File 'lib/fbtok/pathspec.rb', line 167

def self.debug=(value) @@debug = value; end

.debug?Boolean

note: default is FALSE

Returns:

  • (Boolean)


168
# File 'lib/fbtok/pathspec.rb', line 168

def self.debug?()      @@debug ||= false; end

.read(src) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/fbtok/pathspec.rb', line 171

def self.read( src )
    ## note: normalize src - use File.expand_path ??
    ##    change all backslash to slash for now (\ to /)
    fullsrc =  File.expand_path( src )

    recs = read_csv( fullsrc )
    pp recs     if debug?

    ##  note - make pathspecs relative to passed in file arg!!!
    basedir = File.dirname( fullsrc )

    recs.each do |rec|
        ##
        ## todo/check - check for season/seasons column - why? why not?
        path = rec['path']
        fullpath = File.expand_path( path, basedir )
        datafiles =   Pathspec.find( fullpath )

        ## auto-add (new) datafiles column (from expanded pathspec)
        rec['datafiles'] = datafiles
    end

    recs
end