Module: PlainText::BuiltinType

Included in:
Part, Part::Boundary, Part::Paragraph
Defined in:
lib/plain_text/builtin_type.rb

Overview

Contains common methods for builtin-class emulating classes

The class that includes this module should have a method instance that returns the main instance of the builtin-class instance; e.g., instance may be equivalent to to_s, to_a, and alike.

Author:

  • Masa Sakano (Wise Babel Ltd)

Instance Method Summary collapse

Instance Method Details

#subclass_name(index_ini: 0) ⇒ String

Subclass name only

Make sure your class is a child class of Part, Part::Paragraph, or Part::Boundary. Otherwise this method would not be inherited, obviously.

Examples:

For a child class of Part

class PlainText::Part
  class Section < self
    class Subsection < self; end  # It must be a child class!
  end
end
ss = PlainText::Part::Section::Subsection.new ["abc"]
ss.subclass_name         # => "Part::Section::Subsection"
ss.subclass_name(index_ini: 1) # => "Section::Subsection"

For a child class of Boundary

class PlainText::Part::Boundary
  class SubBoundary < self
    class SubSubBoundary < self; end  # Grandchild
  end
end
ss = PlainText::Part::Boundary::SubBoundary::SubSubBoundary.new ["abc"]
ss.subclass_name  # => "Part::Boundary::SubBoundary::SubSubBoundary"
ss.subclass_name(index_ini: 2)    # => "SubBoundary::SubSubBoundary"

Parameters:

  • index_ini (Integer) (defaults to: 0)

    Starting index after split, e.g., if 1, “Part::” is removed and if 2, “Part::Boundary::” (for example) is removed.

Returns:

See Also:

  • Part#subclass_name


43
44
45
# File 'lib/plain_text/builtin_type.rb', line 43

def subclass_name(index_ini: 0)
  self.class.name.split(/\A#{Regexp.quote method(__method__).owner.name.split("::")[0..-2].join("::")}::/)[1].split('::')[index_ini..-1].join('::') || ''  # removing "::BuiltinType"
end