Class: Pebblebed::Uid
- Inherits:
-
Object
- Object
- Pebblebed::Uid
- Defined in:
- lib/pebblebed/uid.rb
Defined Under Namespace
Modules: WildcardPath
Instance Attribute Summary collapse
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#oid ⇒ Object
Returns the value of attribute oid.
-
#path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
- .parse(string) ⇒ Object
- .raw_parse(string) ⇒ Object
- .valid?(string) ⇒ Boolean
- .valid_klass?(value) ⇒ Boolean
- .valid_label?(value) ⇒ Boolean
- .valid_oid?(value) ⇒ Boolean
- .valid_path?(value) ⇒ Boolean
- .valid_wildcard_path?(value) ⇒ Boolean
- .wildcard_path?(value) ⇒ Boolean
Instance Method Summary collapse
- #==(other) ⇒ Object
- #eql?(other) ⇒ Boolean
-
#initialize(args) ⇒ Uid
constructor
A new instance of Uid.
- #inspect ⇒ Object
- #realm ⇒ Object
- #to_s ⇒ Object (also: #to_uid)
Constructor Details
#initialize(args) ⇒ Uid
Returns a new instance of Uid.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/pebblebed/uid.rb', line 6 def initialize(args) case args when String self.klass, self.path, self.oid = self.class.raw_parse(args) when Hash self.klass, self.path, self.oid = args[:klass], args[:path], args[:oid] else raise "Invalid argument" end raise InvalidUid, "Missing klass in uid" unless self.klass raise InvalidUid, "A valid uid must specify either path or oid" unless self.path || self.oid end |
Instance Attribute Details
#klass ⇒ Object
Returns the value of attribute klass.
18 19 20 |
# File 'lib/pebblebed/uid.rb', line 18 def klass @klass end |
#oid ⇒ Object
Returns the value of attribute oid.
18 19 20 |
# File 'lib/pebblebed/uid.rb', line 18 def oid @oid end |
#path ⇒ Object
Returns the value of attribute path.
18 19 20 |
# File 'lib/pebblebed/uid.rb', line 18 def path @path end |
Class Method Details
.parse(string) ⇒ Object
72 73 74 75 |
# File 'lib/pebblebed/uid.rb', line 72 def parse(string) uid = new(string) [uid.klass, uid.path, uid.oid] end |
.raw_parse(string) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/pebblebed/uid.rb', line 57 def raw_parse(string) if string =~ /\A([^:]+)\:([^\$]*)?\$?(.+)?\Z/ klass, path, oid = $1, $2, $3 [klass, path, oid] end end |
.valid?(string) ⇒ Boolean
64 65 66 67 68 69 70 |
# File 'lib/pebblebed/uid.rb', line 64 def valid?(string) begin true if new(string) rescue InvalidUid false end end |
.valid_klass?(value) ⇒ Boolean
81 82 83 84 85 86 87 88 |
# File 'lib/pebblebed/uid.rb', line 81 def valid_klass?(value) return false if value =~ /^\.+$/ return false if value == "" value.split('.').each do |label| return false unless self.valid_label?(label) end true end |
.valid_label?(value) ⇒ Boolean
77 78 79 |
# File 'lib/pebblebed/uid.rb', line 77 def valid_label?(value) !!(value =~ /^[a-zA-Z0-9_-]+$/) end |
.valid_oid?(value) ⇒ Boolean
99 100 101 |
# File 'lib/pebblebed/uid.rb', line 99 def valid_oid?(value) !value.nil? && !value.include?('/') end |
.valid_path?(value) ⇒ Boolean
90 91 92 93 94 95 96 97 |
# File 'lib/pebblebed/uid.rb', line 90 def valid_path?(value) # catches a stupid edge case in ruby where "..".split('.') == [] instead of ["", "", ""] return false if value =~ /^\.+$/ return true if valid_wildcard_path?(value) value.split('.').all? { |s| valid_label?(s) } end |
.valid_wildcard_path?(value) ⇒ Boolean
107 108 109 |
# File 'lib/pebblebed/uid.rb', line 107 def valid_wildcard_path?(value) wildcard_path?(value) && WildcardPath.valid?(value) end |
.wildcard_path?(value) ⇒ Boolean
103 104 105 |
# File 'lib/pebblebed/uid.rb', line 103 def wildcard_path?(value) value =~ /[\*\|\^]/ end |
Instance Method Details
#==(other) ⇒ Object
48 49 50 |
# File 'lib/pebblebed/uid.rb', line 48 def ==(other) self.to_uid == other.to_uid end |
#eql?(other) ⇒ Boolean
52 53 54 |
# File 'lib/pebblebed/uid.rb', line 52 def eql?(other) self == other end |
#inspect ⇒ Object
39 40 41 |
# File 'lib/pebblebed/uid.rb', line 39 def inspect "#<Pebblebed::Uid '#{to_s}'>" end |
#realm ⇒ Object
35 36 37 |
# File 'lib/pebblebed/uid.rb', line 35 def realm self.path.split(".").first if self.path end |
#to_s ⇒ Object Also known as: to_uid
43 44 45 |
# File 'lib/pebblebed/uid.rb', line 43 def to_s "#{@klass}:#{@path}$#{@oid}".chomp("$") end |