Sha256: 78b401900400c192e6c913c6430c8349497ec3e1a02c61e41fa9368bc2e62a3b

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

module Rad
  module AbstractRouter    
    
    protected
      def parse_url_root options
        url_root = options.delete(:url_root)      
        url_root.must =~ /^\/.+/ if url_root
        url_root
      end
      
      def parse_prefix options        
        prefix = options.delete(:prefix)
        if prefix
          prefix.must_not_be.empty
          prefix.must_be.a Array
          prefix = prefix.collect do |item|
            item = Array(item)                      
            item.size.must_be.in 1..2
            item << item.first.to_s if item.size < 2
            item.first.must_be.a Symbol
            item.last.must_be.a String
            item
          end
        end
        prefix
      end
    
      def encode_prefix_params! path, params, meta
        if prefix = meta[:prefix]
          parts = []
          prefix.each do |name, als|            
            parts << als
            parts << (params.delete(name) || raise("not provided :#{name} prefix!")) # TODO 3 add url_escape
          end
          "/#{parts.join('/')}#{path}"
        else
          path
        end
      end
      
      def encode_url_root! path, meta
        if url_root = meta[:url_root]
          "#{url_root}#{path}" 
        else
          path
        end
      end
      
      def decode_prefix_params! parts, params, meta
        if prefix = meta[:prefix]
          prefix.reverse.each do |name, als|            
            params[name] = parts.pop || raise("not provided :#{name} prefix!")
            raise("incorrect url for :#{name} prefix!") unless parts.pop == als
          end          
        end
      end
      
      def decode_url_root! parts, meta
        raise "invalid :url_root!" if meta[:url_root] and meta[:url_root] != "/#{parts.join('/')}"
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rad_core-0.0.13 lib/rad/router/routers/abstract_router.rb