Class: Pebbles::Path::Positions

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pebbles-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/pebbles-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/pebbles-path/positions.rb', line 9

def all
  @all
end

#labelsObject (readonly)

Returns the value of attribute labels.



9
10
11
# File 'lib/pebbles-path/positions.rb', line 9

def labels
  @labels
end

Class Method Details

.to_conditions(path) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/pebbles-path/positions.rb', line 50

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

  labels = Pebbles::Uid::Labels.new(path, :name => 'label', :max_depth => MAX_DEPTH, :stop => nil)
  labels.to_hash
end

Instance Method Details

#[](index) ⇒ Object



19
20
21
# File 'lib/pebbles-path/positions.rb', line 19

def [](index)
  labels[index]
end

#[]=(index, value) ⇒ Object



23
24
25
# File 'lib/pebbles-path/positions.rb', line 23

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

#eachObject



15
16
17
# File 'lib/pebbles-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/pebbles-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/pebbles-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/pebbles-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