Class: Protocol::Caldav::Path
- Inherits:
-
Object
- Object
- Protocol::Caldav::Path
- Defined in:
- lib/protocol/caldav/path.rb
Instance Attribute Summary collapse
-
#storage_class ⇒ Object
readonly
Returns the value of attribute storage_class.
-
#to_s ⇒ Object
readonly
Returns the value of attribute to_s.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #build_propfind(xml) ⇒ Object
- #build_xml(xml) ⇒ Object
- #child_of?(other) ⇒ Boolean
- #depth ⇒ Object
- #ensure_trailing_slash ⇒ Object
-
#initialize(raw, storage_class: nil) ⇒ Path
constructor
A new instance of Path.
- #parent ⇒ Object
- #parent_exists? ⇒ Boolean
- #start_with?(prefix) ⇒ Boolean
- #to_propfind_xml ⇒ Object
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_class ⇒ Object (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_s ⇒ Object (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
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 |
#depth ⇒ Object
29 30 31 |
# File 'lib/protocol/caldav/path.rb', line 29 def depth @to_s.chomp('/').split('/').reject(&:empty?).length end |
#ensure_trailing_slash ⇒ Object
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 |
#parent ⇒ Object
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
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
62 63 64 |
# File 'lib/protocol/caldav/path.rb', line 62 def start_with?(prefix) @to_s.start_with?(prefix) end |
#to_propfind_xml ⇒ Object
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 |