Class: MetaRuby::YARD::InheritedAttributeHandler

Inherits:
YARD::Handlers::Ruby::AttributeHandler
  • Object
show all
Defined in:
lib/yard-metaruby.rb

Overview

Instance Method Summary collapse

Instance Method Details

#processObject

Callback handled by YARD



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/yard-metaruby.rb', line 19

def process
    name = statement.parameters[0].jump(:tstring_content, :ident).source
    if statement.parameters.size == 4
        attr_name = statement.parameters[1].jump(:tstring_content, :ident).source
    else
        attr_name = name
    end
    options = statement.parameters.jump(:assoc)

    is_map = false
    if options != statement.parameters
        key = options[0].jump(:ident).source
        value = options[1].jump(:ident).source
        if key == "map" && value == "true"
            is_map = true
        end
    end

    key_type, value_type = nil

    object = YARD::CodeObjects::MethodObject.new(namespace, attr_name, scope) do |o|
        o.dynamic = true
        o.aliases << "self_#{name}"
    end
    register(object)
    key_name ||=
        if object.docstring.has_tag?('key_name')
            object.docstring.tag('key_name').text
        else
            'key'
        end
    return_type ||=
        if object.docstring.has_tag?('return')
            object.docstring.tag('return').types.first
        elsif is_map
            'Hash<Object,Object>'
        else
            'Array<Object>'
        end
    if return_type =~ /^\w+\<(.*)\>$/
        if is_map
            key_type, value_type = $1.split(',')
        else
            value_type = $1
        end
    else
        key_type = "Object"
        value_type = "Object"
    end

    object = YARD::CodeObjects::MethodObject.new(namespace, "all_#{name}", scope)
    object.dynamic = true 
    register(object)
    object.docstring.replace("The union, along the class hierarchy, of all the values stored in #{name}\n@return [Array<#{value_type}>]")

    if is_map
        object = YARD::CodeObjects::MethodObject.new(namespace, "find_#{name}", scope)
        object.dynamic = true 
        register(object)
        object.parameters << [key_name]
        object.docstring.replace("
Looks for objects registered in #{name} under the given key, and returns the first one in the ancestor chain
(i.e. the one tha thas been registered in the most specialized class)
                                 
@return [#{value_type},nil] the found object, or nil if none is registered under that key")

        object = YARD::CodeObjects::MethodObject.new(namespace, "has_#{name}?", scope)
        object.dynamic = true 
        register(object)
        object.parameters << [key_name]
        object.docstring.replace("Returns true if an object is registered in #{name} anywhere in the class hierarchy\n@return [Boolean]")
        object.signature = "def has_#{name}?(key)"

        object = YARD::CodeObjects::MethodObject.new(namespace, "each_#{name}", scope)
        object.dynamic = true 
        register(object)
        object.parameters << [key_name, "nil"] << ["uniq", "true"]
        object.docstring.replace("
@overload each_#{name}(#{key_name}, uniq = true)
  Enumerates all objects registered in #{name} under the given key
  @yield [element]
  @yieldparam [#{value_type}] element
@overload each_#{name}(nil, uniq = true)
  Enumerates all objects registered in #{name}
  @yield [#{key_name}, element]
  @yieldparam [#{key_type}] #{key_name}
  @yieldparam [#{value_type}] element
        ")
    else
        object = YARD::CodeObjects::MethodObject.new(namespace, "each_#{name}", scope)
        object.dynamic = true 
        register(object)
        object.docstring.replace("Enumerates all objects registered in #{name}\n@return []\n@yield [element]\n@yieldparam [#{value_type}] element")
    end

    if is_map
        return key_type, value_type
    else
        return value_type
    end
end