Class: Apipie::ResponseDescriptionAdapter::PropDesc

Inherits:
Object
  • Object
show all
Defined in:
lib/apipie/response_description_adapter.rb

Overview

A ResponseDescriptionAdapter::PropDesc object pretends to be an Apipie::Param in a ResponseDescription

To successfully masquerade as such, it needs to:

respond_to?('name') and/or ['name'] returning the name of the parameter
respond_to?('required') and/or ['required'] returning boolean
respond_to?('additional_properties') and/or ['additional_properties'] returning boolean
respond_to?('validator') and/or ['validator'] returning 'nil' (so type is 'string'), or an object that:
       1) describes a type.  currently type is inferred as follows:
             if validator.is_a? Apipie::Validator::EnumValidator -->  respond_to? 'values' (returns array).  Type is enum or boolean
             else: use v.expected_type().  This is expected to be the swagger type, or:
                 numeric ==> swagger type is 'number'
                 hash ==> swagger type is 'object' and validator should respond_to? 'params_ordered'
                 array ==> swagger type is array and validator (FUTURE) should indicate type of element

Defined Under Namespace

Classes: Validator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, expected_type, options = {}, sub_properties = []) ⇒ PropDesc

Returns a new instance of PropDesc.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/apipie/response_description_adapter.rb', line 87

def initialize(name, expected_type, options = {}, sub_properties = [])
  @name = name
  @required = true
  @required = false if options[:required] == false
  @expected_type = expected_type
  @additional_properties = false

  options[:desc] ||= options[:description]
  @description = options[:desc]
  @options = options
  @is_array = options[:is_array] || false
  @sub_properties = []
  for prop in sub_properties do
    add_sub_property(prop)
  end
end

Instance Attribute Details

#additional_propertiesObject

Returns the value of attribute additional_properties.



132
133
134
# File 'lib/apipie/response_description_adapter.rb', line 132

def additional_properties
  @additional_properties
end

#descriptionObject (readonly) Also known as: desc

Returns the value of attribute description.



131
132
133
# File 'lib/apipie/response_description_adapter.rb', line 131

def description
  @description
end

#expected_typeObject (readonly)

Returns the value of attribute expected_type.



131
132
133
# File 'lib/apipie/response_description_adapter.rb', line 131

def expected_type
  @expected_type
end

#nameObject (readonly)

Returns the value of attribute name.



131
132
133
# File 'lib/apipie/response_description_adapter.rb', line 131

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



131
132
133
# File 'lib/apipie/response_description_adapter.rb', line 131

def options
  @options
end

#requiredObject (readonly)

Returns the value of attribute required.



131
132
133
# File 'lib/apipie/response_description_adapter.rb', line 131

def required
  @required
end

Instance Method Details

#[](key) ⇒ Object



104
105
106
# File 'lib/apipie/response_description_adapter.rb', line 104

def [](key)
  self.send(key) if self.respond_to?(key.to_s)
end

#add_sub_property(prop_desc) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/apipie/response_description_adapter.rb', line 108

def add_sub_property(prop_desc)
  raise "Only properties with expected_type 'object' can have sub-properties" unless @expected_type == 'object'
  case prop_desc
  when PropDesc
    @sub_properties << prop_desc
  when Modifier
    prop_desc.apply(self)
  else
    raise "Unrecognized prop_desc type (#{prop_desc.class})"
  end
end

#is_array?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/apipie/response_description_adapter.rb', line 136

def is_array?
  @is_array
end

#to_json(lang) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/apipie/response_description_adapter.rb', line 120

def to_json(lang)
  {
      name: name,
      required: required,
      validator: validator,
      description: description,
      additional_properties: additional_properties,
      is_array: is_array?,
      options: options
  }
end

#to_sObject



49
50
51
# File 'lib/apipie/response_description_adapter.rb', line 49

def to_s
  "PropDesc -- name: #{@name}  type: #{@expected_type} required: #{@required} options: #{@options} subprop count: #{@sub_properties.length} additional properties: #{@additional_properties}"
end

#validatorObject



140
141
142
# File 'lib/apipie/response_description_adapter.rb', line 140

def validator
  Validator.new(@expected_type, options[:values], @sub_properties)
end