Sha256: b719a4d6fe7e0498b7557e7683b625a7e0079a286939a970c78ef09d97a5831a

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

class Usher
  class Route
    class Variable
      attr_reader :type, :name, :validator, :transformer
      
      def initialize(type, name, opts = {})
        @type = type
        @name = :"#{name}"
        @validator = opts[:validator]
        @transformer = opts[:transformer]
      end

      def to_s
        "#{type}#{name}"
      end
      
      def transform!(val)
        return val unless @transformer

        case @transformer
        when Proc
          @transformer.call(val)
        when Symbol
          val.send(@transformer)
        end
      rescue Exception => e
        raise ValidationException.new("#{val} could not be successfully transformed by #{@transformer}, root cause #{e.inspect}")
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
joshbuddy-usher-0.2.1 lib/usher/route/variable.rb