class Pairtree::Root

Constants

SHORTY_LENGTH

Attributes

prefix[R]
root[R]

Public Class Methods

new(root, args = {}) click to toggle source

@param [String] root The pairtree_root directory within the pairtree home @param [Hash] args Pairtree options @option args [String] :prefix (nil) the identifier prefix used throughout the pairtree @option args [String] :version (Pairtree::SPEC_VERSION) the version of the pairtree spec that this tree conforms to

# File lib/pairtree/root.rb, line 13
def initialize root, args = {}
  @root = root
  
  @shorty_length = args.delete(:shorty_length) || SHORTY_LENGTH
  @prefix = args.delete(:prefix) || ''

  @options = args
end

Public Instance Methods

[](id)
Alias for: get
exists?(id) click to toggle source

Determine if a given identifier exists within the pairtree @param [String] id The full, prefixed identifier @return [Boolean]

# File lib/pairtree/root.rb, line 62
def exists? id
  File.directory?(path_for(id))
end
get(id) click to toggle source

Get an existing ppath @param [String] id The full, prefixed identifier @return [Pairtree::Obj] The object encapsulating the identifier’s ppath

# File lib/pairtree/root.rb, line 70
def get id
  Pairtree::Obj.new path_for(id)
end
Also aliased as: []
list() click to toggle source

Get a list of valid existing identifiers within the pairtree @return [Array]

# File lib/pairtree/root.rb, line 25
def list          
  objects = []
  return [] unless File.directory? @root

  Dir.chdir(@root) do
    possibles = Dir['**/?'] + Dir['**/??']
    possibles.each { |path|
      contents = Dir.entries(path).reject { |x| x =~ /^\./ }
      objects << path unless contents.all? { |f| f.length <= @shorty_length and File.directory?(File.join(path, f)) }
    }
  end
  objects.map { |x| @prefix + Pairtree::Path.path_to_id(x) }
end
mk(id) click to toggle source

Create a new ppath @param [String] id The full, prefixed identifier @return [Pairtree::Obj] The object encapsulating the newly created ppath

# File lib/pairtree/root.rb, line 79
def mk id
  FileUtils.mkdir_p path_for(id)
  get(id)
end
pairtree_version() click to toggle source

Get the version of the pairtree spec that this pairtree conforms to @return [String]

# File lib/pairtree/root.rb, line 98
def pairtree_version
  @options[:version]
end
path() click to toggle source

Get the path containing the pairtree_root @return [String]

# File lib/pairtree/root.rb, line 42
def path
  File.dirname(root)
end
path_for(id) click to toggle source

Get the full path for a given identifier (whether it exists or not) @param [String] id The full, prefixed identifier @return [String]

# File lib/pairtree/root.rb, line 50
def path_for id
  unless id.start_with? @prefix
    raise IdentifierError, "Identifier must start with #{@prefix}"
  end
  path_id = id[@prefix.length..-1]
  File.join(@root, Pairtree::Path.id_to_path(path_id))
end
purge!(id) click to toggle source

Delete a ppath @param [String] id The full, prefixed identifier @return [Boolean]

# File lib/pairtree/root.rb, line 88
def purge! id
  if exists?(id)
    Pairtree::Path.remove!(path_for(id))
  end
  not exists?(id)
end