Module: Path

Extended by:
MetaExtension
Defined in:
lib/scout/path.rb,
lib/scout/path/find.rb,
lib/scout/path/util.rb

Constant Summary collapse

SLASH =
"/"[0]
DOT =
"."[0]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MetaExtension

extended

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, prev = nil, *args, &block) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/scout/path.rb', line 61

def method_missing(name, prev = nil, *args, &block)
  if block_given? || name.to_s.start_with?('to_')
    super name, prev, *args, &block
  else
    join(name, prev)
  end
end

Class Method Details

.caller_lib_dir(file = nil, relative_to = ['lib', 'bin']) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/scout/path.rb', line 9

def self.caller_lib_dir(file = nil, relative_to = ['lib', 'bin'])
  
  if file.nil?
    caller_dup = caller.dup
    while file = caller_dup.shift
      break unless file =~ /scout\/(?:resource\.rb|workflow\.rb)/ or
        file =~ /scout\/path\.rb/ or
        file =~ /scout\/persist.rb/
    end
    file = file.sub(/\.rb[^\w].*/,'.rb')
  end

  relative_to = [relative_to] unless Array === relative_to
  file = File.expand_path(file)
  return Path.setup(file) if relative_to.select{|d| File.exist? File.join(file, d)}.any?

  while file != '/'
    dir = File.dirname file

    return dir if relative_to.select{|d| File.exist? File.join(dir, d)}.any?

    file = File.dirname file
  end

  return nil
end

.default_pkgdirObject



36
37
38
# File 'lib/scout/path.rb', line 36

def self.default_pkgdir
  @@default_pkgdir = 'scout'
end

.follow(path, map) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/scout/path/find.rb', line 14

def self.follow(path, map)
  map.sub('{PKGDIR}', path.pkgdir || Path.default_pkgdir).
    sub('{RESOURCE}', path.to_s).
    sub('{PWD}', FileUtils.pwd).
    sub('{TOPLEVEL}', path._toplevel).
    sub('{SUBPATH}', path._subpath).
    sub('{BASENAME}', File.basename(path)).
    sub('{PATH}', path).
    sub('{LIBDIR}', path.libdir || Path.caller_lib_dir).
    sub('{REMOVE}/', '').
    sub('{REMOVE}', '').gsub(/\/+/,'/')
end

.map_searchObject



43
44
45
# File 'lib/scout/path/find.rb', line 43

def self.map_search
  @@map_search ||= %w(current workflow user local global lib fast cache bulk)
end

.path_mapsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/scout/path/find.rb', line 27

def self.path_maps
  @@path_maps ||= IndiferentHash.setup({
    :current => File.join("{PWD}", "{TOPLEVEL}", "{SUBPATH}"),
    :user    => File.join(ENV['HOME'], ".{PKGDIR}", "{TOPLEVEL}", "{SUBPATH}"),
    :global  => File.join('/', "{TOPLEVEL}", "{PKGDIR}", "{SUBPATH}"),
    :usr     => File.join('/usr/', "{TOPLEVEL}", "{PKGDIR}", "{SUBPATH}"),
    :local   => File.join('/usr/local', "{TOPLEVEL}", "{PKGDIR}", "{SUBPATH}"),
    :fast    => File.join('/fast', "{TOPLEVEL}", "{PKGDIR}", "{SUBPATH}"),
    :cache   => File.join('/cache', "{TOPLEVEL}", "{PKGDIR}", "{SUBPATH}"),
    :bulk    => File.join('/bulk', "{TOPLEVEL}", "{PKGDIR}", "{SUBPATH}"),
    :lib     => File.join('{LIBDIR}', "{TOPLEVEL}", "{SUBPATH}"),
    :base    => File.join(Path.caller_lib_dir(__FILE__), "{TOPLEVEL}", "{SUBPATH}"),
    :default => :user
  })
end

.search_orderObject



47
48
49
# File 'lib/scout/path/find.rb', line 47

def self.search_order
  @@search_order ||= (path_maps.keys & map_search) + (path_maps.keys - map_search)
end

Instance Method Details

#_partsObject



2
3
4
# File 'lib/scout/path/find.rb', line 2

def _parts
  @_parts ||= self.split("/")
end

#_subpathObject



6
7
8
# File 'lib/scout/path/find.rb', line 6

def _subpath
  @subpath ||= _parts.length > 1 ? _parts[1..-1] * "/" : _parts[0]
end

#_toplevelObject



10
11
12
# File 'lib/scout/path/find.rb', line 10

def _toplevel
  @toplevel ||= _parts.length > 1 ? _parts[0] : nil
end

#annotate_found_where(found, where) ⇒ Object



58
59
60
61
62
63
# File 'lib/scout/path/find.rb', line 58

def annotate_found_where(found, where)
  self.annotate(found).tap{|p| 
    p.instance_variable_set("@where", where) 
    p.instance_variable_set("@original", self.dup) 
  }
end

#basenameObject



11
12
13
# File 'lib/scout/path/util.rb', line 11

def basename
  self.annotate(File.basename(self))
end

#directory?Boolean

Returns:

  • (Boolean)


2
3
4
5
# File 'lib/scout/path/util.rb', line 2

def directory?
  return nil unless self.exist?
  File.directory?(self.find)
end

#dirnameObject



7
8
9
# File 'lib/scout/path/util.rb', line 7

def dirname
  self.annotate(File.dirname(self))
end

#exist?Boolean Also known as: exists?

Returns:

  • (Boolean)


100
101
102
103
104
# File 'lib/scout/path/find.rb', line 100

def exist?
  # OPEN
  found = self.find
  File.exist?(found) || File.directory?(found)
end

#find(where = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/scout/path/find.rb', line 86

def find(where = nil)
  return self if located?
  return follow(where) if where


  Path.search_order.each do |map_name|
    found = follow(map_name, false)

    return annotate_found_where(found, map_name) if File.exist?(found) || File.directory?(real_path)
  end

  return follow(:default)
end

#find_all(caller_lib = nil, search_paths = nil) ⇒ Object



108
109
110
111
112
# File 'lib/scout/path/find.rb', line 108

def find_all(caller_lib = nil, search_paths = nil)
  Path.search_order
    .collect{|where| find(where) }
    .select{|file| file.exist? }.uniq
end

#follow(map_name = :default, annotate = true) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/scout/path/find.rb', line 73

def follow(map_name = :default, annotate = true)
  map = Path.path_maps[map_name]
  while Symbol === map
    map_name = map
    map = Path.path_maps[map_name]
  end
  found = Path.follow(self, map)

  annotate_found_where(found, map_name)  if annotate

  found
end

#glob(pattern = '*') ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/scout/path/util.rb', line 16

def glob(pattern = '*')
  if self.include? "*"
    self.glob_all
  else
    return [] unless self.exist? 
    found = self.find
    exp = File.join(found, pattern)
    paths = Dir.glob(exp).collect{|f| self.annotate(f) }

    paths.each do |p|
      p.original = File.join(found.original, p.sub(/^#{found}/, ''))
    end if found.original

    paths
  end
end

#glob_all(pattern = nil, caller_lib = nil, search_paths = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/scout/path/util.rb', line 33

def glob_all(pattern = nil, caller_lib = nil, search_paths = nil)
  search_paths ||= Path.search_paths
  search_paths = search_paths.dup

  location_paths = {}
  search_paths.keys.collect do |where| 
    found = find(where, Path.caller_lib_dir, search_paths)
    paths = pattern ? Dir.glob(File.join(found, pattern)) : Dir.glob(found) 

    paths = paths.collect{|p| self.annotate p }

    paths = paths.each do |p|
      p.original = File.join(found.original, p.sub(/^#{found}/, ''))
      p.where = where
    end if found.original and pattern

    location_paths[where] = paths
  end
end

#join(subpath, prevpath = nil) ⇒ Object Also known as: [], /



48
49
50
51
52
53
54
55
56
# File 'lib/scout/path.rb', line 48

def join(subpath, prevpath = nil)
  subpath = subpath.to_s if Symbol === subpath
  prevpath = prevpath.to_s if Symbol === prevpath

  subpath = File.join(prevpath.to_s, subpath) if prevpath
  new = File.join(self, subpath)
  self.annotate(new)
  new
end

#libdirObject



44
45
46
# File 'lib/scout/path.rb', line 44

def libdir
  @libdir ||= Path.caller_lib_dir
end

#located?Boolean

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/scout/path/find.rb', line 53

def located?
  # OPEN RESOURCE
  self.slice(0,1) == SLASH || (self.char(0,1) == DOT && self.char(1,2) == SLASH) # || (resource != Rbbt && (Open.remote?(self) || Open.ssh?(self)))
end

#originalObject



69
70
71
# File 'lib/scout/path/find.rb', line 69

def original
  @original
end

#pkgdirObject



40
41
42
# File 'lib/scout/path.rb', line 40

def pkgdir
  @pkgdir ||= Path.default_pkgdir
end

#whereObject



65
66
67
# File 'lib/scout/path/find.rb', line 65

def where
  @where
end