class MemoryIO::Types::Record

@api private

Class that handles a registered object in {Types::Type.find}. For example, this class will parse inline-docs to generate README.md.

Attributes

keys[R]

@return [Array<Symbol>]

All symbols that can find this record in {Type.find}.
obj[R]

@return [Object]

Whatever.

Public Class Methods

new(object, keys, option = {}) click to toggle source

Instantiate a {Record} object.

@param [Object] object @param [Array<Symbol>] keys

@option [Thread::Backtrace::Location] caller

This option should present if and only if +object+ is a subclass of {Types::Type}.

@option [String] doc

Docstring.
Automatically parse from caller location if this parameter isn't present.
# File lib/memory_io/types/record.rb, line 28
def initialize(object, keys, option = {})
  @obj = object
  @keys = keys
  @force_doc = option[:doc]
  @caller = option[:caller]
end

Public Instance Methods

doc() click to toggle source

Get the doc string.

@return [String]

If option +doc+ had been passed in {#initialize}, this method simply returns it.
Otherwise, parse the file for inline-docs.
If neither +doc+ nor +caller+ had been passed to {#initialize}, an empty string is returned.
# File lib/memory_io/types/record.rb, line 41
def doc
  return @force_doc if @force_doc
  return '' unless @caller

  parse_file_doc(@caller.absolute_path, @caller.lineno)
end

Private Instance Methods

parse_file_doc(file, lineno) click to toggle source

@return [String]

# File lib/memory_io/types/record.rb, line 51
def parse_file_doc(file, lineno)
  return '' unless ::File.file?(file)

  strings = []
  lines = ::IO.binread(file).split("\n")
  (lineno - 1).downto(1) do |no|
    str = lines[no - 1]
    break if str.nil?

    str.strip!
    break unless str.start_with?('#')

    strings.unshift(str[2..-1] || '')
  end
  trim_docstring(strings)
end
trim_docstring(strings) click to toggle source
# File lib/memory_io/types/record.rb, line 68
def trim_docstring(strings)
  strings = strings.drop_while { |s| s.start_with?('@') }.take_while { |s| !s.start_with?('@') }
  strings.drop_while(&:empty?).reverse.drop_while(&:empty?).reverse.join("\n") + "\n"
end