class Apipie::ParamDescription

method parameter description

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

Attributes

allow_nil[R]
desc[R]
name[R]
parent[RW]
required[R]
validator[R]

Public Class Methods

new(name, validator, desc_or_options = nil, options = {}, &block) click to toggle source
# File lib/apipie/param_description.rb, line 15
def initialize(name, validator, desc_or_options = nil, options = {}, &block)

  if desc_or_options.is_a?(Hash) && options.empty?
    options = desc_or_options
  elsif desc_or_options.is_a?(String)
    options[:desc] = desc_or_options
  elsif !desc_or_options.nil?
    raise ArgumentError.new("param description: expected description or options as 3rd parameter")
  end

  options.symbolize_keys!

  @name = name
  @desc = Apipie.markup_to_html(options[:desc] || '')
  @required = if options.has_key? :required
    options[:required]
  else
    Apipie.configuration.required_by_default?
  end
  @allow_nil = options[:allow_nil] || false

  @validator = nil
  unless validator.nil?
    @validator =
      Validator::BaseValidator.find(self, validator, options, block)
    raise "Validator not found." unless validator
  end
end

Public Instance Methods

full_name() click to toggle source
# File lib/apipie/param_description.rb, line 53
def full_name
  name_parts = parents_and_self.map(&:name)
  return ([name_parts.first] + name_parts[1..-1].map { |n| "[#{n}]" }).join("")
end
parents_and_self() click to toggle source

returns an array of all the parents: starting with the root parent ending with itself

# File lib/apipie/param_description.rb, line 60
def parents_and_self
  ret = []
  if self.parent
    ret.concat(self.parent.parents_and_self)
  end
  ret << self
  ret
end
to_json() click to toggle source
# File lib/apipie/param_description.rb, line 69
def to_json
  if validator.is_a? Apipie::Validator::HashValidator
    {
      :name => name.to_s,
      :full_name => full_name,
      :description => desc,
      :required => required,
      :allow_nil => allow_nil,
      :validator => validator.to_s,
      :expected_type => validator.expected_type,
      :params => validator.hash_params_ordered.map(&:to_json)
    }
  else
    {
      :name => name.to_s,
      :full_name => full_name,
      :description => desc,
      :required => required,
      :allow_nil => allow_nil,
      :validator => validator.to_s,
      :expected_type => validator.expected_type
    }
  end
end
validate(value) click to toggle source
# File lib/apipie/param_description.rb, line 44
def validate(value)
  return true if @allow_nil && value.nil?
  unless @validator.valid?(value)
    error = @validator.error
    error = ParamError.new(error) unless error.is_a? Exception
    raise error
  end
end