Class: SmallCage::DocumentPath

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, path) ⇒ DocumentPath

Returns a new instance of DocumentPath.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/smallcage/document_path.rb', line 5

def initialize(root, path)
  @root = Pathname.new(root).realpath

  @path = Pathname.new(path)
  if @path.exist?
    @path = @path.realpath
  else
    @path = @path.cleanpath
  end

  fail "Illegal path: #{ path.to_s }" if @path.to_s[0...@root.to_s.length] != @root.to_s

  if @path == @root
    @uri = '/'
  else
    @uri = @path.to_s[@root.to_s.length .. -1]
  end
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/smallcage/document_path.rb', line 3

def path
  @path
end

#rootObject (readonly)

Returns the value of attribute root.



3
4
5
# File 'lib/smallcage/document_path.rb', line 3

def root
  @root
end

#uriObject (readonly)

Returns the value of attribute uri.



3
4
5
# File 'lib/smallcage/document_path.rb', line 3

def uri
  @uri
end

Class Method Details

.add_smc_method(obj, value) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/smallcage/document_path.rb', line 52

def self.add_smc_method(obj, value)
  obj.instance_eval do
    @__smallcage ||= {}
    @__smallcage[:smc] = value
  end

  def obj.smc
    @__smallcage.nil? ? nil : @__smallcage[:smc]
  end

  obj
end

.create_with_uri(root, uri, base = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/smallcage/document_path.rb', line 42

def self.create_with_uri(root, uri, base = nil)
  base ||= root
  if uri[0, 1] == '/'
    path = root + uri[1..-1] # absolute URI
  else
    path = base + uri # relative URI
  end
  new(root, path)
end

.to_uri(root, path) ⇒ Object



38
39
40
# File 'lib/smallcage/document_path.rb', line 38

def self.to_uri(root, path)
  new(root, path).uri
end

Instance Method Details

#outfileObject



28
29
30
31
# File 'lib/smallcage/document_path.rb', line 28

def outfile
  return nil unless smc?
  self.class.new(@root, @path.to_s[0 .. -5])
end

#outuriObject



33
34
35
36
# File 'lib/smallcage/document_path.rb', line 33

def outuri
  return nil unless smc?
  uri[0 .. -5]
end

#smc?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/smallcage/document_path.rb', line 24

def smc?
  @path.extname == '.smc'
end