Class: Protocol::Caldav::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/caldav/path.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, storage_class: nil) ⇒ Path

Returns a new instance of Path.



12
13
14
15
16
17
# File 'lib/protocol/caldav/path.rb', line 12

def initialize(raw, storage_class: nil)
  p = raw.to_s.gsub(%r{/+}, '/')
  p = "/#{p}" unless p.start_with?('/')
  @to_s = p
  @storage_class = storage_class
end

Instance Attribute Details

#storage_classObject (readonly)

Returns the value of attribute storage_class.



10
11
12
# File 'lib/protocol/caldav/path.rb', line 10

def storage_class
  @storage_class
end

#to_sObject (readonly)

Returns the value of attribute to_s.



10
11
12
# File 'lib/protocol/caldav/path.rb', line 10

def to_s
  @to_s
end

Instance Method Details

#==(other) ⇒ Object



65
66
67
# File 'lib/protocol/caldav/path.rb', line 65

def ==(other)
  to_s == other.to_s
end

#child_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
# File 'lib/protocol/caldav/path.rb', line 32

def child_of?(other)
  parent_str = other.to_s
  parent_str = "#{parent_str}/" unless parent_str.end_with?('/')
  if @to_s.start_with?(parent_str)
    remainder = @to_s[parent_str.length..]
    remainder.chomp('/').count('/').zero? && !remainder.chomp('/').empty?
  else
    false
  end
end

#depthObject



28
29
30
# File 'lib/protocol/caldav/path.rb', line 28

def depth
  @to_s.chomp('/').split('/').reject(&:empty?).length
end

#ensure_trailing_slashObject



53
54
55
56
57
58
59
# File 'lib/protocol/caldav/path.rb', line 53

def ensure_trailing_slash
  if @to_s.end_with?('/')
    self
  else
    self.class.new("#{@to_s}/", storage_class: @storage_class)
  end
end

#parentObject



19
20
21
22
23
24
25
26
# File 'lib/protocol/caldav/path.rb', line 19

def parent
  parts = @to_s.chomp('/').split('/')
  if parts.length <= 1
    self.class.new('/', storage_class: @storage_class)
  else
    self.class.new("#{parts[0..-2].join('/')}/", storage_class: @storage_class)
  end
end

#parent_exists?Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
# File 'lib/protocol/caldav/path.rb', line 43

def parent_exists?
  raise ArgumentError, "storage_class required for parent_exists?" unless @storage_class

  if parent.depth <= 2
    true
  else
    @storage_class.collection_exists?(parent.to_s)
  end
end

#start_with?(prefix) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/protocol/caldav/path.rb', line 61

def start_with?(prefix)
  @to_s.start_with?(prefix)
end

#to_propfind_xmlObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/protocol/caldav/path.rb', line 69

def to_propfind_xml
  <<~XML
    <d:response>
      <d:href>#{Xml.escape(@to_s)}</d:href>
      <d:propstat>
        <d:prop>
          <d:resourcetype><d:collection/></d:resourcetype>
        </d:prop>
        <d:status>HTTP/1.1 200 OK</d:status>
      </d:propstat>
    </d:response>
  XML
end