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, *args, &block) click to toggle source
# File lib/apipie/param_description.rb, line 15
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 = Apipie.markup_to_html(options[:desc] || '')
  @required = options[:required] || false
  @allow_nil = options[:allow_nil] || false

  @validator = nil
  unless validator_type.nil?
    @validator =
      Validator::BaseValidator.find(self, validator_type, 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 44
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 51
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 60
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 37
def validate(value)
  return true if @allow_nil && value.nil?
  unless @validator.valid?(value)
    raise ArgumentError.new(@validator.error)
  end
end