Sha256: bed3cb17de3557a81c4de6f3e820b684a7d7ee390ee85097410d344524677c03

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 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.to_s 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

1 entries across 1 versions & 1 rubygems

Version Path
usher-0.7.2 lib/usher/route/variable.rb