Class: SwaggerYard::Property

Inherits:
Object
  • Object
show all
Includes:
Example
Defined in:
lib/swagger_yard/property.rb

Overview

Holds the name and type for a single model property

Constant Summary collapse

NAME_OPTIONS_REGEXP =
/[\(\)]/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Example

#example, #example=

Constructor Details

#initialize(name, types, description, options) ⇒ Property



59
60
61
62
63
64
# File 'lib/swagger_yard/property.rb', line 59

def initialize(name, types, description, options)
  @name, @description = name, description
  @required = options.include?('required')
  @nullable = options.include?('nullable')
  @type = Type.from_type_list(types)
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



8
9
10
# File 'lib/swagger_yard/property.rb', line 8

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/swagger_yard/property.rb', line 7

def name
  @name
end

#nullableObject (readonly)

Returns the value of attribute nullable.



7
8
9
# File 'lib/swagger_yard/property.rb', line 7

def nullable
  @nullable
end

#requiredObject (readonly)

Returns the value of attribute required.



7
8
9
# File 'lib/swagger_yard/property.rb', line 7

def required
  @required
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/swagger_yard/property.rb', line 7

def type
  @type
end

Class Method Details

.from_method(yard_method) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/swagger_yard/property.rb', line 22

def self.from_method(yard_method)
  return nil unless yard_method.explicit || yard_method.parameters.empty?
  tags = (yard_method.tags ||[]).dup
  prop_tag = tags.detect { |t| t.tag_name == 'property' }
  return nil unless prop_tag
  tags.reject { |t| t.tag_name == 'property' }
  from_tag(prop_tag).tap do |prop|
    ex = tags.detect { |t| t.tag_name == 'example' }
    prop.example = ex.text.empty? ? ex.name : ex.text if ex
    prop.description = yard_method.docstring unless prop.description
  end
end

.from_tag(tag) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/swagger_yard/property.rb', line 35

def self.from_tag(tag)
  tag = SwaggerYard.requires_type(tag)
  return nil unless tag

  name = tag_name(tag)
  return nil unless name

  text = tag.text

  if (options_src = (tag.name || '')) =~ NAME_OPTIONS_REGEXP
    _, options_string = options_src.split(NAME_OPTIONS_REGEXP)
  elsif tag.name && tag.object.is_a?(YARD::CodeObjects::MethodObject)
    if text
      text = tag.name + ' ' + text
    else
      text = tag.name
    end
  end

  options = options_string.to_s.split(',').map(&:strip)

  new(name, tag.types, text, options)
end

.tag_name(tag) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/swagger_yard/property.rb', line 12

def self.tag_name(tag)
  if tag.object.is_a?(YARD::CodeObjects::MethodObject)
    tag.object.name.to_s
  else
    tag = SwaggerYard.requires_name(tag)
    return nil unless tag
    tag.name.split(NAME_OPTIONS_REGEXP).first
  end
end

Instance Method Details

#required?Boolean



66
67
68
# File 'lib/swagger_yard/property.rb', line 66

def required?
  @required
end