Class: Valise::SearchRoot
- Inherits:
-
Object
- Object
- Valise::SearchRoot
show all
- Includes:
- Enumerable, Unpath
- Defined in:
- lib/valise/search-root.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Unpath
#collapse, #file_from_backtrace, #from_here, from_here, #repath, #string_to_segments, #unpath, #up_to, up_to
Constructor Details
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
#segments ⇒ Object
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)
paths = [[]]
until paths.empty?
rel = paths.shift
path = repath(@segments + rel)
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
|
#inspect ⇒ Object
44
45
46
|
# File 'lib/valise/search-root.rb', line 44
def inspect
"#{self.class.name.split(":").last}:#{[*@segments].join("/")}"
end
|
#present?(segments) ⇒ Boolean
70
71
72
|
# File 'lib/valise/search-root.rb', line 70
def present?(segments)
return File::exists?(full_path(segments))
end
|
#to_s ⇒ Object
48
49
50
|
# File 'lib/valise/search-root.rb', line 48
def to_s
"#{[*@segments].join("/")}"
end
|
#writable?(segments) ⇒ 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
|