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.



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

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

Instance Attribute Details

#storage_classObject (readonly)

Returns the value of attribute storage_class.



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

def storage_class
  @storage_class
end

#to_sObject (readonly)

Returns the value of attribute to_s.



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

def to_s
  @to_s
end

Instance Method Details

#==(other) ⇒ Object



70
71
72
# File 'lib/protocol/caldav/path.rb', line 70

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

#build_propfind(xml) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/protocol/caldav/path.rb', line 74

def build_propfind(xml)
  XmlBuilder.response(xml, href: @to_s) do
    XmlBuilder.propstat_ok(xml) do
      xml.tag!("d:resourcetype") { xml.tag!("d:collection") }
    end
  end
end

#build_xml(xml) ⇒ Object



82
83
84
# File 'lib/protocol/caldav/path.rb', line 82

def build_xml(xml)
  build_propfind(xml)
end

#child_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def child_of?(other)
  parent_str = other.to_s
  unless parent_str.end_with?('/')
    parent_str = "#{parent_str}/"
  end
  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



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

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

#ensure_trailing_slashObject



58
59
60
61
62
63
64
# File 'lib/protocol/caldav/path.rb', line 58

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

#parentObject



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

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)


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/protocol/caldav/path.rb', line 46

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

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

#start_with?(prefix) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#to_propfind_xmlObject



86
87
88
89
90
# File 'lib/protocol/caldav/path.rb', line 86

def to_propfind_xml
  x = Builder::XmlMarkup.new
  build_propfind(x)
  x.target!
end