Class: Pairtree::Root

Inherits:
Object
  • Object
show all
Defined in:
lib/pairtree/root.rb

Constant Summary collapse

SHORTY_LENGTH =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, args = {}) ⇒ Root

Returns a new instance of Root.

Parameters:

  • root (String)

    The pairtree_root directory within the pairtree home

  • args (Hash) (defaults to: {})

    Pairtree options

Options Hash (args):

  • :prefix (String) — default: nil

    the identifier prefix used throughout the pairtree

  • :version (String) — default: Pairtree::SPEC_VERSION

    the version of the pairtree spec that this tree conforms to



13
14
15
16
17
18
19
20
# 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

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



6
7
8
# File 'lib/pairtree/root.rb', line 6

def prefix
  @prefix
end

#rootObject (readonly)

Returns the value of attribute root.



6
7
8
# File 'lib/pairtree/root.rb', line 6

def root
  @root
end

Instance Method Details

#exists?(id) ⇒ Boolean

Determine if a given identifier exists within the pairtree

Parameters:

  • id (String)

    The full, prefixed identifier

Returns:

  • (Boolean)


62
63
64
# File 'lib/pairtree/root.rb', line 62

def exists? id
  File.directory?(path_for(id))
end

#get(id) ⇒ Pairtree::Obj Also known as: []

Get an existing ppath

Parameters:

  • id (String)

    The full, prefixed identifier

Returns:

  • (Pairtree::Obj)

    The object encapsulating the identifier’s ppath



70
71
72
# File 'lib/pairtree/root.rb', line 70

def get id
  Pairtree::Obj.new path_for(id)
end

#listArray

Get a list of valid existing identifiers within the pairtree

Returns:

  • (Array)


25
26
27
28
29
30
31
32
33
34
35
36
37
# 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) ⇒ Pairtree::Obj

Create a new ppath

Parameters:

  • id (String)

    The full, prefixed identifier

Returns:

  • (Pairtree::Obj)

    The object encapsulating the newly created ppath



79
80
81
82
# File 'lib/pairtree/root.rb', line 79

def mk id
  FileUtils.mkdir_p path_for(id)
  get(id)
end

#pairtree_versionString

Get the version of the pairtree spec that this pairtree conforms to

Returns:

  • (String)


98
99
100
# File 'lib/pairtree/root.rb', line 98

def pairtree_version
  @options[:version]
end

#pathString

Get the path containing the pairtree_root

Returns:

  • (String)


42
43
44
# File 'lib/pairtree/root.rb', line 42

def path
  File.dirname(root)
end

#path_for(id) ⇒ String

Get the full path for a given identifier (whether it exists or not)

Parameters:

  • id (String)

    The full, prefixed identifier

Returns:

  • (String)


50
51
52
53
54
55
56
# 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) ⇒ Boolean

Delete a ppath

Parameters:

  • id (String)

    The full, prefixed identifier

Returns:

  • (Boolean)


88
89
90
91
92
93
# File 'lib/pairtree/root.rb', line 88

def purge! id
  if exists?(id)
    Pairtree::Path.remove!(path_for(id))
  end
  not exists?(id)
end