Sha256: 95ece74cb8fcd1988fff9a7a76f3a0de6205ef47b421ab77c5942e1ac520f607

Contents?: true

Size: 1.47 KB

Versions: 26

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 / lval.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

26 entries across 26 versions & 2 rubygems

Version Path
joshbuddy-usher-0.5.7 lib/usher/route/util.rb
usher-0.8.3 lib/usher/route/util.rb
usher-0.8.2 lib/usher/route/util.rb
usher-0.8.1 lib/usher/route/util.rb
usher-0.8.0 lib/usher/route/util.rb
usher-0.7.5 lib/usher/route/util.rb
usher-0.7.4 lib/usher/route/util.rb
usher-0.7.3 lib/usher/route/util.rb
usher-0.7.2 lib/usher/route/util.rb
usher-0.7.1 lib/usher/route/util.rb
usher-0.7.0 lib/usher/route/util.rb
usher-0.6.8 lib/usher/route/util.rb
usher-0.6.7 lib/usher/route/util.rb
usher-0.6.6 lib/usher/route/util.rb
usher-0.6.5 lib/usher/route/util.rb
usher-0.6.4 lib/usher/route/util.rb
usher-0.6.3 lib/usher/route/util.rb
usher-0.6.2 lib/usher/route/util.rb
usher-0.6.1 lib/usher/route/util.rb
usher-0.6.0 lib/usher/route/util.rb