File: object_path.rb

package info (click to toggle)
ruby-dbus 0.25.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 776 kB
  • sloc: ruby: 6,584; xml: 225; sh: 38; makefile: 8
file content (29 lines) | stat: -rw-r--r-- 902 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# frozen_string_literal: true

# This file is part of the ruby-dbus project
# Copyright (C) 2019 Martin Vidner
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License, version 2.1 as published by the Free Software Foundation.
# See the file "COPYING" for the exact licensing terms.

module DBus
  # A {::String} that validates at initialization time.
  # See also {DBus::Data::ObjectPath}
  # @see https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-marshaling-object-path
  class ObjectPath < String
    # @raise Error if not a valid object path
    def initialize(str)
      unless self.class.valid?(str)
        raise DBus::Error, "Invalid object path #{str.inspect}"
      end

      super
    end

    def self.valid?(str)
      str == "/" || str =~ %r{\A(/[A-Za-z0-9_]+)+\z}
    end
  end
end