Class: Pebbles::Uid::Labels

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pebbles-uid/labels.rb

Direct Known Subclasses

Oid

Constant Summary collapse

NO_MARKER =
Class.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*values) ⇒ Labels

Returns a new instance of Labels.



9
10
11
12
13
14
15
16
17
# File 'lib/pebbles-uid/labels.rb', line 9

def initialize(*values)
  options = values.pop if values.last.is_a?(Hash)
  options ||= {}
  @values = values.flatten.compact.map {|v| v.split('.') }.flatten
  @name = options[:name]
  @suffix = options[:suffix]
  @stop = options.fetch(:stop) { NO_MARKER }
  @max_depth = options[:max_depth]
end

Instance Attribute Details

#max_depthObject (readonly)

Returns the value of attribute max_depth.



8
9
10
# File 'lib/pebbles-uid/labels.rb', line 8

def max_depth
  @max_depth
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/pebbles-uid/labels.rb', line 8

def name
  @name
end

#stopObject (readonly)

Returns the value of attribute stop.



8
9
10
# File 'lib/pebbles-uid/labels.rb', line 8

def stop
  @stop
end

#suffixObject (readonly)

Returns the value of attribute suffix.



8
9
10
# File 'lib/pebbles-uid/labels.rb', line 8

def suffix
  @suffix
end

#valuesObject (readonly)

Returns the value of attribute values.



8
9
10
# File 'lib/pebbles-uid/labels.rb', line 8

def values
  @values
end

Instance Method Details

#ambiguous?Boolean

Returns:

  • (Boolean)


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

def ambiguous?
  value == '*' || values.empty? || wildcard?
end

#child_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
# File 'lib/pebbles-uid/labels.rb', line 36

def child_of?(other)
  child = true
  other.values.each_with_index do |label, i|
    if label != values[i]
      child = false
    end
  end
  child && other.size != size
end

#conditionsObject



79
80
81
82
83
84
85
86
# File 'lib/pebbles-uid/labels.rb', line 79

def conditions
  unless @conditions
    options = {:name => name, :suffix => suffix}
    options.merge!(:stop => stop) if use_stop_marker?
    @conditions = Conditions.new(values, options)
  end
  @conditions
end

#each(&block) ⇒ Object



19
20
21
# File 'lib/pebbles-uid/labels.rb', line 19

def each(&block)
  values.each {|value| yield value}
end

#empty?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/pebbles-uid/labels.rb', line 63

def empty?
  values.empty?
end

#next_labelObject



75
76
77
# File 'lib/pebbles-uid/labels.rb', line 75

def next_label
  conditions.next
end

#parent_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
# File 'lib/pebbles-uid/labels.rb', line 27

def parent_of?(other)
  return false if values == other.values
  parent = true
  values.each_with_index do |value, i|
    parent = false unless value == other.values[i]
  end
  parent
end

#sizeObject



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

def size
  values.size
end

#tailObject



23
24
25
# File 'lib/pebbles-uid/labels.rb', line 23

def tail
  values[1..-1]
end

#to_hashObject



71
72
73
# File 'lib/pebbles-uid/labels.rb', line 71

def to_hash
  conditions.to_hash
end

#to_sObject Also known as: value



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

def to_s
  values.join('.')
end

#use_stop_marker?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/pebbles-uid/labels.rb', line 88

def use_stop_marker?
  stop != NO_MARKER && !(values.size == max_depth)
end

#valid_with?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


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

def valid_with?(pattern)
  !empty? && values.all? {|value| value[pattern] }
end

#wildcard?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/pebbles-uid/labels.rb', line 67

def wildcard?
  !!(value =~ /[\*\|\^]/)
end