Class: Ki::DirectoryBase

Inherits:
Object show all
Defined in:
lib/data_storage/dir_base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ DirectoryBase

Returns a new instance of DirectoryBase.



22
23
24
# File 'lib/data_storage/dir_base.rb', line 22

def initialize(path)
  init_from_path(path)
end

Class Method Details

.find!(path, *locations) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/data_storage/dir_base.rb', line 26

def self.find!(path, *locations)
  locations.each do |loc|
    dest = loc.go(path)
    if dest.exists?
      return dest
    end
  end
  raise "Could not find '#{path}' from '#{locations.map { |l| l.path }.join("', '")}'"
end

Instance Method Details

#child(name) ⇒ Object



100
101
102
# File 'lib/data_storage/dir_base.rb', line 100

def child(name)
  DirectoryBase.new(name)
end

#empty?(*sub_path) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/data_storage/dir_base.rb', line 104

def empty?(*sub_path)
  Dir.entries(go(*sub_path).path).size == 2
end

#exists?(*sub_path) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/data_storage/dir_base.rb', line 59

def exists?(*sub_path)
  File.exists?(go(*sub_path).path)
end

#go(*path) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/data_storage/dir_base.rb', line 45

def go(*path)
  if path.empty?
    self
  else
    path = File.join(path).split(File::Separator)
    child = child(path.delete_at(0)).parent(self)
    if path.empty?
      child
    else
      child.go(*path)
    end
  end
end

#init_from_path(path) ⇒ Object



37
38
39
# File 'lib/data_storage/dir_base.rb', line 37

def init_from_path(path)
  @path = path
end

#ki_path(*sub_paths) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/data_storage/dir_base.rb', line 91

def ki_path(*sub_paths)
  if defined? @parent
    paths = [@parent.ki_path, @path]
  else
    paths = ["/"]
  end
  File.join([paths, sub_paths].flatten)
end

#mkdir(*path) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/data_storage/dir_base.rb', line 63

def mkdir(*path)
  dest = go(*path)
  if !dest.exists?
    FileUtils.mkdir_p(dest.path)
  end
  dest
end

#nameObject



41
42
43
# File 'lib/data_storage/dir_base.rb', line 41

def name
  File.basename(@path)
end

#path(*sub_paths) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/data_storage/dir_base.rb', line 71

def path(*sub_paths)
  new_path = [@path, sub_paths].flatten
  if defined? @parent
    new_path.unshift(@parent.path)
  end
  File.join(new_path)
end

#rootObject



79
80
81
82
83
84
85
# File 'lib/data_storage/dir_base.rb', line 79

def root
  if defined? @parent
    @parent.root
  else
    self
  end
end

#root?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/data_storage/dir_base.rb', line 87

def root?
  !defined? @parent
end