Class: Colore::DocKey

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

Overview

This is the unique identifier for a document, a composite of application name (app) and document identifier (doc_id). Doc_id is generated by the application, so we can only guarantee uniqueness by combining with app name.

Constant Summary collapse

STORAGE_SUBDIRECTORY_LENGTH =
2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, doc_id) ⇒ DocKey

Constructor. The app and doc_id may only be comprised of alphanumeric letters and the special characters “_” and “-”.


23
24
25
26
27
28
# File 'lib/doc_key.rb', line 23

def initialize app, doc_id
  validate(app)
  validate(doc_id)
  @app = app
  @doc_id = doc_id
end

Instance Attribute Details

#appObject

Returns the value of attribute app


11
12
13
# File 'lib/doc_key.rb', line 11

def app
  @app
end

#doc_idObject

Returns the value of attribute doc_id


12
13
14
# File 'lib/doc_key.rb', line 12

def doc_id
  @doc_id
end

Class Method Details

.parse(doc_key_str) ⇒ Object

Constructs [DocKey] from the input string, which should be the output from [#to_s].


15
16
17
# File 'lib/doc_key.rb', line 15

def self.parse doc_key_str
  self.new *(doc_key_str.split '/')
end

Instance Method Details

#pathObject

Doc key as path


31
32
33
# File 'lib/doc_key.rb', line 31

def path
  Pathname.new(app) + subdirectory + doc_id
end

#subdirectoryObject

Returns the first 2 characters of a SHA256 hash for this doc, suitable for subdirectory names into which the docs can be distributed, rather than clumping them into a single base directory (which will not scale well)


43
44
45
# File 'lib/doc_key.rb', line 43

def subdirectory
  Digest::MD5.hexdigest(@doc_id)[0..(STORAGE_SUBDIRECTORY_LENGTH-1)]
end

#to_sObject

Doc key as string


36
37
38
# File 'lib/doc_key.rb', line 36

def to_s
  "#{@app}/#{@doc_id}"
end