Sha256: 5dce3d28d6cde46acae41009b752e39af34150036dac87eb5ad6e5effdaa86e4

Contents?: true

Size: 1.48 KB

Versions: 8

Compression:

Stored size: 1.48 KB

Contents

class Usher
  class Route
    class Variable

      module ProcValidator
        def valid!(val)
          begin
            @validator.call(val)
          rescue Exception => e
            raise ValidationException.new("#{val} does not conform to #{@validator}, root cause #{e.inspect}")
          end
        end
      end

      module CaseEqualsValidator
        def valid!(val)
          @validator === val or raise(ValidationException.new("#{val} does not conform to #{@validator}"))
        end
      end
      
      attr_reader :type, :name, :validator, :regex_matcher
      attr_accessor :look_ahead, :default_value, :look_ahead_priority
      
      def initialize(name, regex_matcher = nil, validator = nil)
        @name = name.to_s.to_sym
        @validator = validator || regex_matcher
        @regex_matcher = regex_matcher
        
        case @validator
        when nil
          # do nothing
        when Proc
          extend(ProcValidator)
        else
          extend(CaseEqualsValidator)
        end
      end
      private :initialize

      def valid!(val)
      end
      
      def ==(o)
        o && (o.class == self.class && o.name == @name && o.validator == @validator)
      end
      
      class Single < Variable
        def to_s
          ":#{name}"
        end
      end

      class Glob < Variable
        def to_s
          "*#{name}"
        end
      end

      class Greedy < Variable
        def to_s
          "!#{name}"
        end
      end
      
    end
    
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
usher-0.7.1 lib/usher/route/variable.rb
usher-0.7.0 lib/usher/route/variable.rb
usher-0.6.8 lib/usher/route/variable.rb
usher-0.6.7 lib/usher/route/variable.rb
usher-0.6.6 lib/usher/route/variable.rb
usher-0.6.5 lib/usher/route/variable.rb
usher-0.6.4 lib/usher/route/variable.rb
usher-0.6.3 lib/usher/route/variable.rb