Class: YARD::Handlers::Ruby::BitStructHandler

Inherits:
AttributeHandler
  • Object
show all
Defined in:
lib/yard/handlers/ruby/bit_struct_handler.rb

Instance Method Summary collapse

Instance Method Details

#processObject



7
8
9
10
11
12
13
14
15
16
17
18
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
# File 'lib/yard/handlers/ruby/bit_struct_handler.rb', line 7

def process
  name      = statement.parameters[0].jump(:tstring_content, :ident).source
  bitlength = statement.parameters[1].jump(:tstring_content, :ident).source

  type = case statement.method_name(true)
         when :unsigned, :signed
           "Fixnum"
         when :string
           "String"
         else
           "Object"
         end

  reader = YARD::CodeObjects::MethodObject.new(namespace, name)
  reader.dynamic = true

  # Grabs the comment and parses it out into MethodObject#docstring
  register(reader)

  if reader.docstring.blank?
    # Use the default if there wasn't a field-specific docstring
    doc = "Value of the `#{name}` field\n"
    reader.docstring.replace(doc)
  end

  # Must have a writer method so this won't show up as read-only, but the
  # reader's stuff takes precedence, so none of its properties seem to
  # matter as long as there is a reader
  writer = reader.dup
  writer.name = "#{name}="

  unless reader.docstring.has_tag?(:return)
    reader.docstring.add_tag(
      YARD::Tags::Tag.new(:return, "#{bitlength}-bit #{statement.method_name(true)} value", type)
    )
  end

  namespace.attributes[scope][name] = SymbolHash[:read => reader, :write => writer]
end