class Directory

Directory

Directory groups Items (passwords) for user interface purposes

Constants

VALID_DIRECTORY_REGEX

VALID_DIRECTORY_REGEX = /A+z/ # directory name can't contain slash and asterisk and newline

Public Class Methods

cd(path) click to toggle source

Find a directory, starting from root. Equivalent for cd(“/path”) instance method

Directory.cd('path/user') == directory.cd('/path/user')  #=> true
# File app/models/directory.rb, line 148
def self.cd(path)
  Directory.root.cd(path)
end
list() click to toggle source

Line list, but lists everything starting from root.

# File app/models/directory.rb, line 182
def self.list
  puts Directory.root.list
end
mkdir_p!(path, **options) click to toggle source

Creates a bunch of directories starting with root

Directory.mkdir_p '/Users/grych'
# File app/models/directory.rb, line 124
def self.mkdir_p!(path, **options)
  d = Directory.root.mkdir_p!(path, **options)
  Directory.root.reload
  d
end
root() click to toggle source

The root of all directories is the one without the parent

root = Directory.root
# File app/models/directory.rb, line 29
def self.root
  Directory.find_by(directories: nil) || (raise(Tarkin::DirectoryNotFound, "Root not found") unless Directory.count == 0)
end

Public Instance Methods

_list(depth=0) click to toggle source

See list

# File app/models/directory.rb, line 163
def _list(depth=0)
  ret = "  " * depth + self.name + "/" + " [" + self.groups.map{|g| g.name}.join(', ') + "]\n"
  self.directories.each {|child| ret += child._list(depth + 1)} 
  self.items.each { |item| ret += "  " * depth + "  " + item.username + "\n" }
  ret
end
cd(path) click to toggle source

Find a directory belongs to current directory. If the path starts with '/', it keeps searching from the root.

directory.cd('path/user')   #=> #<Directory:0x007f8ba57c9ce id: 15, name: "user">
directory.cd('/path/user')  #=> #<Directory:0x007f8ba57c9ce id: 15, name: "user">
# File app/models/directory.rb, line 135
def cd(path)
  return Directory.root if path.nil? || path.empty?
  r = if path.strip.starts_with?('/') then Directory.root else self end
  path.split('/').reject(&:empty?).each do |dir|
    r = r.directories.find_by(name: dir)
    raise Tarkin::DirectoryNotFound, "Directory #{dir} not found" unless r
  end
  r
end
groups(**options) click to toggle source
# File app/models/directory.rb, line 54
def groups(**options)
  if options[:user]
    options[:user].groups
  else
    if self.root?
      Group.all
    else
      self.groups_old
    end
  end
end
Also aliased as: groups_old
groups_old(**options)

Groups of current Directory. In the special case, when Directory is root, returns all groups OR all groups for a given user

Directory.root.groups.count                     #=> 15
Directory.root.groups(user: User.first).count   #=> 2
directory.groups.count                          #=> 2
Alias for: groups
inspect() click to toggle source

Shorter view

# File app/models/directory.rb, line 201
def inspect
  "#<Directory> '#{self.name}'  [id: #{self.id}, parent: #{self.directory_id}]"
end
list() click to toggle source

Prints all items and directories belongs to the current directory

d.list
#=>  dir0/ [group 0, group 1]
#=>    subdir/ []
#=>    username 0
# File app/models/directory.rb, line 176
def list
  self.reload
  puts _list 0
end
mkdir(name, **options) click to toggle source

Create the directory under current dir. If it already exists, just returns it. New directory inherits groups from the parent. If creating directory is under root, User can be given as an option (in this case it inherits only Users groups). It is also possible to specify list of groups which should be connected with the new directory by adding options

Directory.root.mkdir "new dir", description: "The brand new one", user: User.first
#=> #<Directory:0x007f8ba7c6063, id: 16, name: "new dir", description: "The brand new one">
# File app/models/directory.rb, line 74
def mkdir(name, **options)
  user = nil
  if options[:user]
    user = options[:user]
    options.delete :user
  end
  groups = nil
  if options[:groups]
    groups = options[:groups]
    options.delete :groups
  end
  d = Directory.new(name: name, directory: self, **options)
  if user || groups
    # inherits groups from user
    self.groups(user: user).each { |group| d.groups << group  } if user
    # take the groups from the list
    groups.each { |group| d.groups << group  } if groups
  else 
    # inherit all groups from parent
    self.groups.each do |group|
      d.groups << group
    end
  end
  d
end
mkdir!(name, **options) click to toggle source
# File app/models/directory.rb, line 100
def mkdir!(name, **options)
  d = Directory.find_by(name: name, directory: self)
  unless d
    d = self.mkdir(name, **options)
    d.save!
  end
  d
end
mkdir_p!(path, **options) click to toggle source

Create a bunch of directories (equivalent to Unix 'mkdir -p') separated by slash. Returns last of them.

d.mkdir_p 'usr/local/bin' #=> #<Directory:0x007f8ba57c9ce id: 15, name: "bin">
# File app/models/directory.rb, line 113
def mkdir_p!(path, **options)
  r = self
  path.split('/').reject(&:empty?).each do |dir|
    r = r.mkdir!(dir, **options)
  end
  r
end
parent() click to toggle source

Parent of current directory

Directory.root.parent  #=> nil
# File app/models/directory.rb, line 43
def parent
  self.directory
end
pwd() click to toggle source

Like pwd_r, but including root

# File app/models/directory.rb, line 196
def pwd
  [Directory.root] + pwd_r
end
pwd_r(array=[]) click to toggle source

Returns a list of ancestrors of current directory, without root

# File app/models/directory.rb, line 187
def pwd_r(array=[])
  if self.parent.nil?
    array.reverse
  else
    self.parent.pwd_r(array + [self])
  end
end
root?() click to toggle source

Is the current directory the root?

Directory.root.root?  #=> true
# File app/models/directory.rb, line 36
def root?
  self.directory.nil?
end
siblings() click to toggle source

Siblings are all directories in the same level except self

# File app/models/directory.rb, line 158
def siblings
  self.parent.directories.where.not(id: self.id)
end