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.



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

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.



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

def storage_class
  @storage_class
end

#to_sObject (readonly)

Returns the value of attribute to_s.



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

def to_s
  @to_s
end

Instance Method Details

#==(other) ⇒ Object



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

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

#build_propfind(xml) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/protocol/caldav/path.rb', line 70

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



78
79
80
# File 'lib/protocol/caldav/path.rb', line 78

def build_xml(xml)
  build_propfind(xml)
end

#child_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

#ensure_trailing_slashObject



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

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)

Raises:

  • (ArgumentError)


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

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)


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

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

#to_propfind_xmlObject



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

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