Class: PebblePath::Positions

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pebble_path/positions.rb

Constant Summary collapse

MAX_DEPTH =
10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(positions) ⇒ Positions

Returns a new instance of Positions.



10
11
12
13
# File 'lib/pebble_path/positions.rb', line 10

def initialize(positions)
  @all = resolve positions
  @labels = all.compact
end

Instance Attribute Details

#allObject (readonly)

Returns the value of attribute all.



9
10
11
# File 'lib/pebble_path/positions.rb', line 9

def all
  @all
end

#labelsObject (readonly)

Returns the value of attribute labels.



9
10
11
# File 'lib/pebble_path/positions.rb', line 9

def labels
  @labels
end

Class Method Details

.to_conditions(path) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pebble_path/positions.rb', line 50

def to_conditions(path)
  unless Pebblebed::Uid.valid_path?(path)
    raise ArgumentError.new("Wildcards terminate the path. Invalid path: #{path}")
  end

  labels = path.split('.')
  # In a Pebblebed::Uid::WildcardPath, anything after '^' is optional.
  optional_part = false

  labels.map! do |label|
    if label =~ /^\^/
      label.gsub!(/^\^/, '')
      optional_part = true
    end

    result = label.include?('|') ? label.split('|') : label
    result = [label, nil].flatten if optional_part
    result
  end

  result = {}
  (0...MAX_DEPTH).map do |index|
    break if labels[index] == '*'
    result[:"label_#{index}"] = labels[index]
    break if labels[index].nil?
  end
  result
end

Instance Method Details

#[](index) ⇒ Object



19
20
21
# File 'lib/pebble_path/positions.rb', line 19

def [](index)
  labels[index]
end

#[]=(index, value) ⇒ Object



23
24
25
# File 'lib/pebble_path/positions.rb', line 23

def []=(index, value)
  labels[index] = value
end

#eachObject



15
16
17
# File 'lib/pebble_path/positions.rb', line 15

def each
  labels.each {|label| yield label}
end

#resolve(positions) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/pebble_path/positions.rb', line 31

def resolve(positions)
  positions = positions.split('.') if positions.is_a?(String)
  positions = truncate_invalid(positions)
  (0...MAX_DEPTH).map do |i|
    positions[i]
  end
end

#to_sObject



27
28
29
# File 'lib/pebble_path/positions.rb', line 27

def to_s
  labels.join('.')
end

#truncate_invalid(positions) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/pebble_path/positions.rb', line 39

def truncate_invalid(positions)
  labels = []
  (0...MAX_DEPTH).each do |i|
    break if positions[i].nil?
    labels << positions[i]
  end
  labels
end