module YARD::Handlers::Ruby::StructHandlerMethods

def add_reader_tags(klass, new_method, member)

Returns:
  • (String) - a docstring to be attached to the getter method for this member

Parameters:
  • member (String) -- the name of the member we're generating documentation for
  • klass (ClassObject) -- the class whose members we're working with
def add_reader_tags(klass, new_method, member)
  member_tag = member_tag_for_member(klass, member, :read)
  return_type = return_type_from_tag(member_tag)
  getter_doc_text = member_tag ? member_tag.text : "Returns the value of attribute #{member}"
  new_method.docstring.replace(getter_doc_text)
  new_method.add_tag YARD::Tags::Tag.new(:return, "the current value of #{member}", return_type)
end

def add_writer_tags(klass, new_method, member)

Returns:
  • (String) - a docstring to be attached to the setter method for this member

Parameters:
  • member (String) -- the name of the member we're generating documentation for
  • klass (ClassObject) -- the class whose members we're working with
def add_writer_tags(klass, new_method, member)
  member_tag = member_tag_for_member(klass, member, :write)
  return_type = return_type_from_tag(member_tag)
  setter_doc_text = member_tag ? member_tag.text : "Sets the attribute #{member}"
  new_method.docstring.replace(setter_doc_text)
  new_method.add_tag YARD::Tags::Tag.new(:param, "the value to set the attribute #{member} to.", return_type, "value")
  new_method.add_tag YARD::Tags::Tag.new(:return, "the newly set value", return_type)
end

def create_attributes(klass, members)

Parameters:
  • members (Array) -- a list of member names
  • klass (ClassObject) -- the class to generate attributes for
def create_attributes(klass, members)
  # For each parameter, add reader and writers
  members.each do |member|
    next if klass.attributes[:instance][member]
    klass.attributes[:instance][member] = SymbolHash[:read => nil, :write => nil]
    create_writer klass, member if create_member_method?(klass, member, :write)
    create_reader klass, member if create_member_method?(klass, member, :read)
  end
end

def create_class(classname, superclass)

Returns:
  • (ClassObject) - the class object for further processing/method attaching

Parameters:
  • superclass (String) -- the name of the superclass
  • classname (String) -- the name of the class
def create_class(classname, superclass)
  register ClassObject.new(namespace, classname) do |o|
    o.superclass = superclass if superclass
    o.superclass.type = :class if o.superclass.is_a?(Proxy)
  end
end

def create_member_method?(klass, member, type = :read)

Returns:
  • (Boolean) - should the attribute be created?

Parameters:
  • type (Symbol) -- (:read) reader method, or writer method?
  • member (String) -- the name of the struct member we need
  • klass (ClassObject) -- the class whose tags we're searching
def create_member_method?(klass, member, type = :read)
  return true if (klass.tags(:attr) + klass.tags(:attr_reader) + klass.tags(:attr_writer)).empty?
  return true if member_tag_for_member(klass, member, type)
  return !member_tag_for_member(klass, member, :write) if type == :read
  !member_tag_for_member(klass, member, :read)
end

def create_reader(klass, member)

Parameters:
  • member (String) -- the name of the member we're generating a method for
  • klass (ClassObject) -- the class to attach the method to
def create_reader(klass, member)
  new_meth = register MethodObject.new(klass, member, :instance) do |o|
    o.signature ||= "def #{member}"
    o.source ||= "#{o.signature}\n  @#{member}\nend"
  end
  add_reader_tags(klass, new_meth, member)
  klass.attributes[:instance][member][:read] = new_meth
end

def create_writer(klass, member)

Parameters:
  • member (String) -- the name of the member we're generating a method for
  • klass (ClassObject) -- the class to attach the method to
def create_writer(klass, member)
  # We want to convert these members into attributes just like
  # as if they were declared using attr_accessor.
  new_meth = register MethodObject.new(klass, "#{member}=", :instance) do |o|
    o.parameters = [['value', nil]]
    o.signature ||= "def #{member}=(value)"
    o.source ||= "#{o.signature}\n  @#{member} = value\nend"
  end
  add_writer_tags(klass, new_meth, member)
  klass.attributes[:instance][member][:write] = new_meth
end

def member_tag_for_member(klass, member, type = :read)

Returns:
  • (Tags::Tag, nil) - the tag matching the request, or nil if not found

Parameters:
  • type (Symbol) -- reader method, or writer method?
  • member (String) -- the name of the struct member we need
  • klass (ClassObject) -- the class whose tags we're searching
def member_tag_for_member(klass, member, type = :read)
  specific_tag = type == :read ? :attr_reader : :attr_writer
  (klass.tags(specific_tag) + klass.tags(:attr)).find {|tag| tag.name == member }
end

def members_from_tags(klass)

Returns:
  • (Array) - the list of members defined as attributes on the class

Parameters:
  • klass (ClassObject) -- the class with the attributes
def members_from_tags(klass)
  tags = klass.tags(:attr) + klass.tags(:attr_reader) + klass.tags(:attr_writer)
  tags.map(&:name).uniq
end

def return_type_from_tag(member_tag)

Returns:
  • (String) - the user-declared type of the struct member, or [Object] if

Parameters:
  • member_tag (Tags::Tag) -- the tag object to check for types
def return_type_from_tag(member_tag)
  member_tag && member_tag.types ? member_tag.types : "Object"
end