Class: Mongoid::Config::Introspection::Option Private

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/config/introspection.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A helper class to represent an individual option, its name, its default value, and the comment that documents it.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, default, comment) ⇒ Option

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new Option instance with the given name, default value, and comment.

Parameters:

  • name (String)

    The option's name.

  • default (String)

    The option's default value, as a String representing the actual Ruby value.

  • comment (String)

    The multi-line comment describing the option.



56
57
58
# File 'lib/mongoid/config/introspection.rb', line 56

def initialize(name, default, comment)
  @name, @default, @comment = name, default, unindent(comment)
end

Instance Attribute Details

#commentString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The comment that describes this option, as scraped from mongoid/config.rb.

Returns:

  • (String)

    The (possibly multi-line) comment. Each line is prefixed with the Ruby comment character ("#").



34
35
36
# File 'lib/mongoid/config/introspection.rb', line 34

def comment
  @comment
end

#defaultObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The default value of this option.

Returns:

  • (Object)

    The default value of the option, typically a String, Symbol, nil, true, or false.



27
28
29
# File 'lib/mongoid/config/introspection.rb', line 27

def default
  @default
end

#nameString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The name of this option.

Returns:

  • (String)

    The name of the option



21
22
23
# File 'lib/mongoid/config/introspection.rb', line 21

def name
  @name
end

Class Method Details

.from_captures(captures) ⇒ Option

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Instantiate an option from an array of Regex captures.

Parameters:

  • captures (Array<String>)

    The array with the Regex captures to use to instantiate the option. The element at index 1 must be the comment, at index 2 must be the name, and at index 3 must be the default value.

Returns:

  • (Option)

    The newly instantiated Option object.



44
45
46
# File 'lib/mongoid/config/introspection.rb', line 44

def self.from_captures(captures)
  new(captures[2], captures[3], captures[1])
end

Instance Method Details

#==(other) ⇒ true | false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Compare self with the given option.

Returns:

  • (true | false)

    If name, default, and comment are all the same, return true. Otherwise, false.



87
88
89
90
91
# File 'lib/mongoid/config/introspection.rb', line 87

def ==(other)
  name == other.name &&
    default == other.default &&
    comment == other.comment
end

#deprecated?true | false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reports whether or not the text "(Deprecated)" is present in the option's comment.

Returns:

  • (true | false)

    whether the option is deprecated or not.



79
80
81
# File 'lib/mongoid/config/introspection.rb', line 79

def deprecated?
  comment.include?('(Deprecated)')
end

#indented_comment(indent: 2, indent_first_line: false) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Indent the comment by the requested amount, optionally indenting the first line, as well.

param [ Integer ] indent The number of spaces to indent each line (Default: 2) param [ true | false ] indent_first_line Whether or not to indent the first line of the comment (Default: false)

Returns:

  • (String)

    the reformatted comment



69
70
71
72
73
# File 'lib/mongoid/config/introspection.rb', line 69

def indented_comment(indent: 2, indent_first_line: false)
  comment.gsub(/^/, ' ' * indent).tap do |result|
    result.strip! unless indent_first_line
  end
end