Sha256: 7d43e40d4f949bf683682a06ca254dfd13634ccff9a482a506d496b5ecbbcc6d

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

module Restapi

  # method parameter description
  # 
  # name - method name (show)
  # desc - description
  # required - boolean if required
  # validator - Validator::BaseValidator subclass
  class ParamDescription

    attr_reader :name, :desc, :required, :validator
    
    def initialize(name, *args, &block)

      if args.size > 1 || !args.first.is_a?(Hash)
        validator_type = args.shift || nil
      else
        validator_type = nil
      end
      options = args.pop || {}
      
      @name = name
      @desc = Restapi.rdoc.convert((options[:desc] || '').strip_heredoc)
      @required = options[:required] || false
      
      @validator = Validator::BaseValidator.find(validator_type, options, block)
      raise "Validator not found." unless validator
      @validator.param_name = @name
    end

    def validate(value)
      unless @validator.valid?(value)
        raise ArgumentError.new(@validator.error)
      end
    end

    def to_json
      {
        :name => name,
        :description => desc,
        :required => required,
        :validator => validator.to_s
      }
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
restapi-0.0.2 lib/restapi/param_description.rb
restapi-0.0.1 lib/restapi/param_description.rb