Class: Restapi::ParamDescription

Inherits:
Object
  • Object
show all
Defined in:
lib/restapi/param_description.rb

Overview

method parameter description

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *args, &block) ⇒ ParamDescription

Returns a new instance of ParamDescription.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/restapi/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 = Restapi.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

Instance Attribute Details

#allow_nilObject (readonly)

Returns the value of attribute allow_nil.



11
12
13
# File 'lib/restapi/param_description.rb', line 11

def allow_nil
  @allow_nil
end

#descObject (readonly)

Returns the value of attribute desc.



11
12
13
# File 'lib/restapi/param_description.rb', line 11

def desc
  @desc
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/restapi/param_description.rb', line 11

def name
  @name
end

#parentObject

Returns the value of attribute parent.



13
14
15
# File 'lib/restapi/param_description.rb', line 13

def parent
  @parent
end

#requiredObject (readonly)

Returns the value of attribute required.



11
12
13
# File 'lib/restapi/param_description.rb', line 11

def required
  @required
end

#validatorObject (readonly)

Returns the value of attribute validator.



11
12
13
# File 'lib/restapi/param_description.rb', line 11

def validator
  @validator
end

Instance Method Details

#full_nameObject



43
44
45
46
# File 'lib/restapi/param_description.rb', line 43

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_selfObject

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



50
51
52
53
54
55
56
57
# File 'lib/restapi/param_description.rb', line 50

def parents_and_self
  ret = []
  if self.parent
    ret.concat(self.parent.parents_and_self)
  end
  ret << self
  ret
end

#to_jsonObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/restapi/param_description.rb', line 59

def to_json
  if validator.is_a? Restapi::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) ⇒ Object



36
37
38
39
40
41
# File 'lib/restapi/param_description.rb', line 36

def validate(value)
  return true if @allow_nil && value.nil?
  unless @validator.valid?(value)
    raise ArgumentError.new(@validator.error)
  end
end