Sha256: 23ccdf7364792727b15e94466967538c51c0b426d62713216d9ca2ccbac3d93a

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

module GrapeMarkdown
  class Parameter
    attr_reader :route, :full_name, :name, :settings

    delegate :route_name, :route_namespace, to: :route
    delegate :requirement, :type, :documentation, :desc, to: :settings
    delegate :example, to: :documentation, allow_nil: true

    def initialize(route, name, options)
      @full_name = name
      @name      = name
      @name      = name.scan(/\[(.*)\]/).flatten.first if name.include?('[')
      @route     = route
      @settings  = parse_options(options)
    end

    def description
      "#{name} (#{requirement}, #{type}, `#{example}`) ... #{desc}"
    end

    private

    def parse_options(options)
      options = default_options(options) if options.blank?

      options[:requirement] = options[:required] ? 'required' : 'optional'

      Hashie::Mash.new(options)
    end

    def default_options(options)
      model = name.include?('_id') ? name.gsub('_id', '') : route.route_name

      {
        required:       true,
        requirement:    'required',
        type:           'uuid',
        desc:           "the `id` of the `#{model}`",
        documentation:  {
          example:      GrapeMarkdown::Configuration.generate_id
        }
      }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
grape-markdown-0.0.6 lib/grape-markdown/parameter.rb
grape-markdown-0.0.5 lib/grape-markdown/parameter.rb