Class: Pairtree::Root
- Inherits:
-
Object
- Object
- Pairtree::Root
- Defined in:
- lib/pairtree/root.rb
Constant Summary collapse
- SHORTY_LENGTH =
2
Instance Attribute Summary collapse
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
-
#exists?(id) ⇒ Boolean
Determine if a given identifier exists within the pairtree.
-
#get(id) ⇒ Pairtree::Obj
(also: #[])
Get an existing ppath.
-
#initialize(root, args = {}) ⇒ Root
constructor
A new instance of Root.
-
#list ⇒ Array
Get a list of valid existing identifiers within the pairtree.
-
#mk(id) ⇒ Pairtree::Obj
Create a new ppath.
-
#pairtree_version ⇒ String
Get the version of the pairtree spec that this pairtree conforms to.
-
#path ⇒ String
Get the path containing the pairtree_root.
-
#path_for(id) ⇒ String
Get the full path for a given identifier (whether it exists or not).
-
#purge!(id) ⇒ Boolean
Delete a ppath.
Constructor Details
#initialize(root, args = {}) ⇒ Root
Returns a new instance of Root.
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
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
6 7 8 |
# File 'lib/pairtree/root.rb', line 6 def prefix @prefix end |
#root ⇒ Object (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
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
70 71 72 |
# File 'lib/pairtree/root.rb', line 70 def get id Pairtree::Obj.new path_for(id) end |
#list ⇒ Array
Get a list of valid existing identifiers within the pairtree
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
79 80 81 82 |
# File 'lib/pairtree/root.rb', line 79 def mk id FileUtils.mkdir_p path_for(id) get(id) end |
#pairtree_version ⇒ String
Get the version of the pairtree spec that this pairtree conforms to
98 99 100 |
# File 'lib/pairtree/root.rb', line 98 def pairtree_version @options[:version] end |
#path ⇒ String
Get the path containing the pairtree_root
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)
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 |