Module: Usher::Route::Util

Defined in:
lib/usher/route/util.rb

Defined Under Namespace

Classes: Group

Class Method Summary collapse

Class Method Details

.cartesian_product!(lval, rval) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/usher/route/util.rb', line 20

def self.cartesian_product!(lval, rval)
  product = []
  (lval.size * rval.size).times do |index|
    val = []
    val.push(*lval[index % lval.size])
    val.push(*rval[index / lval.size])
    product << val
  end
  lval.replace(product)
end

.expand_path(parts) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/usher/route/util.rb', line 31

def self.expand_path(parts)
  if parts.is_a?(Array)
    paths = [[]]
    
    unless parts.respond_to?(:group_type)
      new_parts = Group.new(:any, nil)
      parts.each{|p| new_parts << p}
      parts = new_parts
    end
    
    case parts.group_type
    when :all
      parts.each do |p|
        cartesian_product!(paths, expand_path(p))
      end
    when :any
      parts.each do |p|
        cartesian_product!(paths, expand_path(p))
      end
      paths.unshift([])
    when :one
      cartesian_product!(paths, parts.collect do |p|
        expand_path(p)
      end)
    end
    paths.each{|p| p.compact!; p.flatten! }
    paths
  else
    [[parts]]
  end

end