Sha256: 5f20ff54c3c185bb7ca1509c48ed984b4f9e3a5d5d2db1304d812d02eda2fb27

Contents?: true

Size: 1.71 KB

Versions: 19

Compression:

Stored size: 1.71 KB

Contents

#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.  The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License.  You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations
# under the License.

module Sinatra
  module Rabbit
    class Param
      attr_reader :name, :klass, :required, :values, :description

      def initialize(*args)
        args.reverse!
        @name, @klass = args.pop, args.pop
        raise "DSL: You need to specify the name and param type (#{@name})" unless @name or @klass
        parse_params!(args)
        @description ||= "Description not available"
      end

      def required?; @required == true; end
      def optional?; !required?; end
      def enum?; !@values.nil?; end
      def number?; [:integer, :float, :number].include?(@klass); end
      def string?; @klass == :string; end

      def to_s
        "#{name}:#{klass}:#{required? ? 'required' : 'optional'}"
      end

      private

      def parse_params!(args)
        @values = args.pop if args.last.kind_of? Array
        @required = args.pop == :required if [:required, :optional].include? args.last
        @description = args.pop
      end

    end

  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
sinatra-rabbit-1.1.6 lib/sinatra/rabbit/param.rb
sinatra-rabbit-1.1.5 lib/sinatra/rabbit/param.rb
sinatra-rabbit-1.1.4 lib/sinatra/rabbit/param.rb
sinatra-rabbit-1.1.3 lib/sinatra/rabbit/param.rb
sinatra-rabbit-1.1.2 lib/sinatra/rabbit/param.rb
sinatra-rabbit-1.1.1 lib/sinatra/rabbit/param.rb
sinatra-rabbit-1.1.0 lib/sinatra/rabbit/param.rb
sinatra-rabbit-1.0.11 lib/sinatra/rabbit/param.rb
sinatra-rabbit-1.0.10 lib/sinatra/rabbit/param.rb
sinatra-rabbit-1.0.9 lib/sinatra/rabbit/param.rb
sinatra-rabbit-1.0.8 lib/sinatra/rabbit/param.rb
sinatra-rabbit-1.0.7 lib/sinatra/rabbit/param.rb
sinatra-rabbit-1.0.6 lib/sinatra/rabbit/param.rb
sinatra-rabbit-1.0.5 lib/sinatra/rabbit/param.rb
sinatra-rabbit-1.0.3 lib/sinatra/rabbit/param.rb
sinatra-rabbit-1.0.2 lib/sinatra/rabbit/param.rb
sinatra-rabbit-1.0.1 lib/sinatra/rabbit/param.rb
sinatra-rabbit-0.9.1 lib/sinatra/rabbit/param.rb
sinatra-rabbit-1.0 lib/sinatra/rabbit/param.rb