Sha256: 0d4d0588162fa7e30f29f087a247244243b65cdd8a48f058b9c01cce8e81ca1b

Contents?: true

Size: 1.09 KB

Versions: 5

Compression:

Stored size: 1.09 KB

Contents

class Usher
  class Route
    class Variable
      attr_reader :type, :name, :validator, :regex_matcher
      attr_accessor :look_ahead, :globs_capture_separators, :default_value
      
      def initialize(type, name, validator = nil, regex_matcher = nil, globs_capture_separators = false)
        @type = type
        @name = :"#{name}"
        @validator = validator
        @regex_matcher = regex_matcher
        @globs_capture_separators = globs_capture_separators
      end

      def to_s
        "#{type}#{name}"
      end
      
      def valid!(val)
        case @validator
        when Proc
          begin
            @validator.call(val)
          rescue Exception => e
            raise ValidationException.new("#{val} does not conform to #{@validator}, root cause #{e.inspect}")
          end
        else
          @validator === val or raise(ValidationException.new("#{val} does not conform to #{@validator}, root cause #{e.inspect}"))
        end if @validator
      end
      
      def ==(o)
        o && (o.type == @type && o.name == @name && o.validator == @validator)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
joshbuddy-usher-0.4.5 lib/usher/route/variable.rb
joshbuddy-usher-0.4.6 lib/usher/route/variable.rb
joshbuddy-usher-0.4.7 lib/usher/route/variable.rb
joshbuddy-usher-0.4.8 lib/usher/route/variable.rb
usher-0.4.8 lib/usher/route/variable.rb