Module: Pairtree

Defined in:
lib/pairtree.rb,
lib/pairtree/obj.rb,
lib/pairtree/path.rb,
lib/pairtree/root.rb,
lib/pairtree/identifier.rb

Defined Under Namespace

Classes: Identifier, IdentifierError, Obj, Path, PathError, Root, VersionMismatch

Constant Summary collapse

SPEC_VERSION =
0.1

Class Method Summary collapse

Class Method Details

.at(path, args = {}) ⇒ Object

Instantiate a pairtree at a given path location

Parameters:

  • path (String)

    The path in which the pairtree resides

  • 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

  • :create (Boolean) — default: false

    if true, create the pairtree and its directory structure if it doesn’t already exist



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pairtree.rb', line 22

def self.at path, args = {}
  args = { :prefix => nil, :version => nil, :create => false }.merge(args)
  args[:version] ||= SPEC_VERSION
  args[:version] = args[:version].to_f
  
  root_path = File.join(path, 'pairtree_root')
  prefix_file = File.join(path, 'pairtree_prefix')
  version_file = File.join(path, pairtree_version_filename(args[:version]))
  existing_version_file = Dir[File.join(path, "pairtree_version*")].sort.last
  
  if args.delete(:create)
    if File.exists?(path) and not File.directory?(path)
      raise PathError, "#{path} exists, but is not a valid pairtree root"
    end
    FileUtils.mkdir_p(root_path)

    unless File.exists? prefix_file
      File.open(prefix_file, 'w') { |f| f.write(args[:prefix].to_s) }
    end
    
    if existing_version_file
      if existing_version_file != version_file
        stored_version = existing_version_file.scan(/([0-9]+)_([0-9]+)/).flatten.join('.').to_f
        raise VersionMismatch, "Version #{args[:version]} specified, but #{stored_version} found."
      end
    else
      args[:version] ||= SPEC_VERSION
      version_file = File.join(path, pairtree_version_filename(args[:version]))
      File.open(version_file, 'w') { |f| f.write %{This directory conforms to Pairtree Version #{args[:version]}. Updated spec: http://www.cdlib.org/inside/diglib/pairtree/pairtreespec.html} }
      existing_version_file = version_file
    end
  else
    unless File.directory? root_path
      raise PathError, "#{path} does not point to an existing pairtree"
    end
  end

  stored_prefix = File.read(prefix_file)
  unless args[:prefix].nil? or args[:prefix].to_s == stored_prefix
    raise IdentifierError, "Specified prefix #{args[:prefix].inspect} does not match stored prefix #{stored_prefix.inspect}"
  end
  args[:prefix] = stored_prefix

  stored_version = existing_version_file.scan(/([0-9]+)_([0-9]+)/).flatten.join('.').to_f
  args[:version] ||= stored_version
  unless args[:version] == stored_version
    raise VersionMismatch, "Version #{args[:version]} specified, but #{stored_version} found."
  end
      
  Pairtree::Root.new(File.join(path, 'pairtree_root'), args)
end