Sha256: 882c24b2a618d8da344c353f2a499d97f30389b7317f4ad48ce0b269a7aef1a0

Contents?: true

Size: 1.47 KB

Versions: 11

Compression:

Stored size: 1.47 KB

Contents

class Usher
  class Route

    module Util

      class Group < Array
        attr_accessor :group_type
        attr_accessor :parent

        def inspect
          "#{group_type}->#{super}"
        end

        def initialize(group_type, parent)
          @group_type = group_type
          @parent = parent
        end
      end

      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 % rval.size])
          product << val
        end
        lval.replace(product)
      end

      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
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
joshbuddy-usher-0.5.1 lib/usher/route/util.rb
joshbuddy-usher-0.5.2 lib/usher/route/util.rb
joshbuddy-usher-0.5.3 lib/usher/route/util.rb
joshbuddy-usher-0.5.4 lib/usher/route/util.rb
joshbuddy-usher-0.5.6 lib/usher/route/util.rb
usher-0.5.6 lib/usher/route/util.rb
usher-0.5.5 lib/usher/route/util.rb
usher-0.5.4 lib/usher/route/util.rb
usher-0.5.3 lib/usher/route/util.rb
usher-0.5.2 lib/usher/route/util.rb
usher-0.5.1 lib/usher/route/util.rb