Class: Pebbles::Uid

Inherits:
Object
  • Object
show all
Defined in:
lib/pebbles-uid.rb,
lib/pebbles-uid/oid.rb,
lib/pebbles-uid/query.rb,
lib/pebbles-uid/roots.rb,
lib/pebbles-uid/labels.rb,
lib/pebbles-uid/version.rb,
lib/pebbles-uid/wildcard.rb,
lib/pebbles-uid/conditions.rb,
lib/pebbles-uid/class_methods.rb

Defined Under Namespace

Modules: Wildcard Classes: Conditions, Labels, Oid, Query, Roots

Constant Summary collapse

VERSION =
"0.0.22"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ Uid

Returns a new instance of Uid.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pebbles-uid.rb', line 15

def initialize(s)
  @species, @path, @oid = self.class.parse(s)
  unless valid_species?
    raise ArgumentError.new("Invalid Uid: #{s}. Species is invalid.")
  end
  unless valid_path?
    raise ArgumentError.new("Invalid Uid: #{s}. Path is invalid.")
  end

  if oid == '*'
    raise ArgumentError.new("Invalid Uid: #{s}. Cannot have wildcard oid.")
  end

  if species.empty?
    raise ArgumentError.new("Invalid Uid: #{s}. Species is required.")
  end
  if path.empty?
    raise ArgumentError.new("Invalid Uid: #{s}. Realm is required.")
  end

end

Instance Attribute Details

#oidObject (readonly)

Returns the value of attribute oid.



14
15
16
# File 'lib/pebbles-uid.rb', line 14

def oid
  @oid
end

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'lib/pebbles-uid.rb', line 14

def path
  @path
end

#speciesObject (readonly)

Returns the value of attribute species.



14
15
16
# File 'lib/pebbles-uid.rb', line 14

def species
  @species
end

Class Method Details

.build(species, path, oid) ⇒ Object



14
15
16
17
18
# File 'lib/pebbles-uid/class_methods.rb', line 14

def build(species, path, oid)
  s = "#{species}:#{path}"
  s << "$#{oid}" if oid
  s
end

.cache_key(uid) ⇒ Object



24
25
26
27
# File 'lib/pebbles-uid/class_methods.rb', line 24

def cache_key(uid)
  species, path, oid = parse(uid)
  "#{species}:#{Labels.new(path).first}.*$#{oid}"
end

.epiteth(s) ⇒ Object



37
38
39
# File 'lib/pebbles-uid/class_methods.rb', line 37

def epiteth(s)
  parse(s)[0].split('.')[1..-1].join('.')
end

.genus(s) ⇒ Object



33
34
35
# File 'lib/pebbles-uid/class_methods.rb', line 33

def genus(s)
  parse(s)[0].split('.').first
end

.oid(s) ⇒ Object



45
46
47
# File 'lib/pebbles-uid/class_methods.rb', line 45

def oid(s)
  parse(s)[2]
end

.parse(s) ⇒ Object



9
10
11
12
# File 'lib/pebbles-uid/class_methods.rb', line 9

def parse(s)
  /^(?<species>[^:]*):(?<path>[^\$]*)\$?(?<oid>.*)$/ =~ s
  [species, path, oid && oid.empty? ? nil : oid]
end

.path(s) ⇒ Object



41
42
43
# File 'lib/pebbles-uid/class_methods.rb', line 41

def path(s)
  parse(s)[1]
end

.query(s, options = {}) ⇒ Object



20
21
22
# File 'lib/pebbles-uid/class_methods.rb', line 20

def query(s, options = {})
  Pebbles::Uid::Query.new(s, options)
end

.roots(uids) ⇒ Object



5
6
7
# File 'lib/pebbles-uid/class_methods.rb', line 5

def roots(uids)
  Pebbles::Uid::Roots.new(uids).unique
end

.species(s) ⇒ Object



29
30
31
# File 'lib/pebbles-uid/class_methods.rb', line 29

def species(s)
  parse(s)[0]
end

.valid_oid?(oid) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/pebbles-uid/class_methods.rb', line 59

def valid_oid?(oid)
  return true if !oid || oid.empty?
  !!(oid =~ /^[^,|]+$/)
end

.valid_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/pebbles-uid/class_methods.rb', line 49

def valid_path?(path)
  return false if path.empty?
  Labels.new(path).valid_with?(/^[a-z0-9_-]+$/)
end

.valid_species?(species) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
# File 'lib/pebbles-uid/class_methods.rb', line 54

def valid_species?(species)
  return false if species.empty?
  Labels.new(species).valid_with?(/^[a-z0-9_-]+$/)
end

Instance Method Details

#cache_keyObject



95
96
97
# File 'lib/pebbles-uid.rb', line 95

def cache_key
  "#{species}:#{realm}.*$#{oid}"
end

#child_pathObject



103
104
105
# File 'lib/pebbles-uid.rb', line 103

def child_path
  "#{path}.#{oid}"
end

#children(child_species = '*') ⇒ Object



107
108
109
# File 'lib/pebbles-uid.rb', line 107

def children(child_species = '*')
  "#{child_species}:#{child_path}"
end

#epitethObject



37
38
39
# File 'lib/pebbles-uid.rb', line 37

def epiteth
  @epiteth ||= species.split('.')[1..-1].join('.')
end

#genusObject



41
42
43
# File 'lib/pebbles-uid.rb', line 41

def genus
  @genus ||= species.split('.').first
end

#oid?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/pebbles-uid.rb', line 77

def oid?
  !!oid
end

#parent(parent_species = species) ⇒ Object



99
100
101
# File 'lib/pebbles-uid.rb', line 99

def parent(parent_species = species)
  "#{parent_species}:#{path_labels.values[0..-2].join('.')}$#{path_labels.values.last}"
end

#parsedObject



85
86
87
# File 'lib/pebbles-uid.rb', line 85

def parsed
  [species, path, oid].compact
end

#path_labelsObject



53
54
55
# File 'lib/pebbles-uid.rb', line 53

def path_labels
  @path_labels ||= Labels.new(path, :name => 'path')
end

#realmObject



45
46
47
# File 'lib/pebbles-uid.rb', line 45

def realm
  @realm ||= path.split('.').first
end

#species_labelsObject



57
58
59
# File 'lib/pebbles-uid.rb', line 57

def species_labels
  @species_labels ||= Labels.new(species, :name => 'species')
end

#to_hashObject



89
90
91
92
93
# File 'lib/pebbles-uid.rb', line 89

def to_hash
  hash = species_labels.to_hash.merge(path_labels.to_hash)
  hash = hash.merge(:oid => oid) if oid?
  hash
end

#to_sObject



81
82
83
# File 'lib/pebbles-uid.rb', line 81

def to_s
  Pebbles::Uid.build species, path, oid
end

#valid?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/pebbles-uid.rb', line 61

def valid?
  valid_species? && valid_path? && valid_oid?
end

#valid_oid?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/pebbles-uid.rb', line 73

def valid_oid?
  self.class.valid_oid?(oid)
end

#valid_path?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/pebbles-uid.rb', line 69

def valid_path?
  self.class.valid_path?(path)
end

#valid_species?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/pebbles-uid.rb', line 65

def valid_species?
  self.class.valid_species?(species)
end