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

#repath, #unpath

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 (readonly)

Returns the value of attribute segments.



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

def segments
  @segments
end

Instance Method Details

#eachObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/valise/search-root.rb', line 18

def 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



40
41
42
# File 'lib/valise/search-root.rb', line 40

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

#get_from(item) ⇒ Object



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

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

#insert(item) ⇒ Object



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

def insert(item)
  if(File::exists?(item.full_path))
    raise Errors::WouldClobber.new(Item.new(item.stack.segments, self, nil))
  else
    write(item)
    return item
  end
end

#inspectObject



36
37
38
# File 'lib/valise/search-root.rb', line 36

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

#present?(segments) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/valise/search-root.rb', line 58

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

#writable?(segments) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#write(item) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/valise/search-root.rb', line 44

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