Class: Valise::SearchRoot

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Unpath
Defined in:
lib/valise/search-root.rb

Direct Known Subclasses

ReadOnlySearchRoot, StemDecorator

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Unpath

#collapse, file_from_backtrace, #file_from_backtrace, #from_here, from_here, repath, string_to_segments, unpath, up_to, #up_to

Constructor Details

#initialize(path) ⇒ SearchRoot

Returns a new instance of SearchRoot.



12
13
14
# File 'lib/valise/search-root.rb', line 12

def initialize(path)
  @segments = unpath(path)
end

Instance Attribute Details

#segmentsObject

Returns the value of attribute segments.



16
17
18
# File 'lib/valise/search-root.rb', line 16

def segments
  @segments
end

Instance Method Details

#each(pathmatch = nil) ⇒ Object

ALL_FILES = PathMatcher.build(‘**’)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/valise/search-root.rb', line 19

def each(pathmatch = nil)
#      pathmatch ||= ALL_FILES
#      files = pathmatch.fnmatchers(@segments).inject([]) do |list, fnmatch|
#        list + Dir.glob(fnmatch).find_all{|path| File::file?(path)}
#      end
#      pathmatch.sort(files).each
#
#
  paths = [[]]
  until paths.empty?
    rel = paths.shift
    path = repath(@segments + rel)
    #symlinks?
    if(File::directory?(path))
      Dir.entries(path).each do |entry|
        next if entry == "."
        next if entry == ".."
        paths.push(rel + [entry])
      end
    elsif(File::file?(path))
      yield(unpath(rel))
    end
  end
end

#full_path(segments) ⇒ Object



52
53
54
# File 'lib/valise/search-root.rb', line 52

def full_path(segments)
  repath(@segments + segments)
end

#get_from(item) ⇒ Object



74
75
76
# File 'lib/valise/search-root.rb', line 74

def get_from(item)
  File::read(item.full_path)
end

#inspectObject



44
45
46
# File 'lib/valise/search-root.rb', line 44

def inspect
  "#{self.class.name.split(":").last}:#{[*@segments].join("/")}"
end

#present?(segments) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/valise/search-root.rb', line 70

def present?(segments)
  return File::exists?(full_path(segments))
end

#to_sObject



48
49
50
# File 'lib/valise/search-root.rb', line 48

def to_s
  "#{[*@segments].join("/")}"
end

#writable?(segments) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/valise/search-root.rb', line 65

def writable?(segments)
  path = full_path(segments)
  return (!File::exists?(path) or File::writable?(path))
end

#write(item) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/valise/search-root.rb', line 56

def write(item)
  return if item.contents.nil?
  FileUtils::mkdir_p(::File::dirname(item.full_path))
  File::open(item.full_path, "w") do |file|
    file.write(item.dump_contents)
  end
  item
end