Class: Asciidoctor::DocTest::Example

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor/doctest/example.rb

Overview

This class represents a single test example.

Constant Summary collapse

NAME_SEPARATOR =
':'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, content: '', desc: '', opts: {}) ⇒ Example

Returns a new instance of Example

Parameters:

  • content (String)
  • desc (String)

    description

  • opts (Hash)

    options

  • name (String, Array<String>)

    a String in format group_name:local_name, or an Array with the group_name and the local_name.


35
36
37
38
39
40
# File 'lib/asciidoctor/doctest/example.rb', line 35

def initialize(name, content: '', desc: '', opts: {})
  self.name = name
  @content = content
  @desc = desc
  @opts = opts
end

Instance Attribute Details

#contentString

Returns raw content.

Returns:

  • (String)

    raw content.


21
22
23
# File 'lib/asciidoctor/doctest/example.rb', line 21

def content
  @content
end

#descString

Returns description.

Returns:

  • (String)

    description.


24
25
26
# File 'lib/asciidoctor/doctest/example.rb', line 24

def desc
  @desc
end

#group_nameString

Returns the first part of the name.

Returns:

  • (String)

    the first part of the name.


15
16
17
# File 'lib/asciidoctor/doctest/example.rb', line 15

def group_name
  @group_name
end

#local_nameString

Returns the second part of the name.

Returns:

  • (String)

    the second part of the name.


18
19
20
# File 'lib/asciidoctor/doctest/example.rb', line 18

def local_name
  @local_name
end

#optsHash

Returns options.

Returns:

  • (Hash)

    options.


27
28
29
# File 'lib/asciidoctor/doctest/example.rb', line 27

def opts
  @opts
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if self and other equals in attributes group_name, local_name and content (compared using ==), otherwise false.

Parameters:

  • other

    the object to compare with.

Returns:

  • (Boolean)

    true if self and other equals in attributes group_name, local_name and content (compared using ==), otherwise false.


115
116
117
118
119
120
# File 'lib/asciidoctor/doctest/example.rb', line 115

def ==(other)
  [:group_name, :local_name, :content].all? do |name|
    other.respond_to?(name) &&
      public_send(name) == other.public_send(name)
  end
end

#[](name) ⇒ Array<String>, Boolean

Returns value(s) of the named option.

Parameters:

  • name (#to_sym)

    the option name.

Returns:

  • (Array<String>, Boolean)

    the option value.


74
75
76
# File 'lib/asciidoctor/doctest/example.rb', line 74

def [](name)
  @opts[name.to_sym]
end

#[]=(name, value) ⇒ void

Sets or unsets the option.

Parameters:

  • name (#to_sym)

    the option name.

  • value (Array<String>, Boolean, String, nil)

    the option value; Array and Boolean are just assigned to the option, nil removes the option and others are added to the option as an array item.


87
88
89
90
91
92
93
94
95
96
# File 'lib/asciidoctor/doctest/example.rb', line 87

def []=(name, value)
  case value
  when nil
    @opts.delete(name.to_sym)
  when Array, TrueClass, FalseClass
    @opts[name.to_sym] = value.deep_dup
  else
    (@opts[name.to_sym] ||= []) << value.dup
  end
end

#empty?Boolean

Returns true when the content is blank, false otherwise.

Returns:

  • (Boolean)

    true when the content is blank, false otherwise.


100
101
102
# File 'lib/asciidoctor/doctest/example.rb', line 100

def empty?
  content.blank?
end

#eql?(other) ⇒ Boolean

Returns true if self and other are instances of the same class and all their attributes are equal (compared using ==), otherwise false.

Parameters:

  • other (Object)

    the object to compare with.

Returns:

  • (Boolean)

    true if self and other are instances of the same class and all their attributes are equal (compared using ==), otherwise false.


127
128
129
130
# File 'lib/asciidoctor/doctest/example.rb', line 127

def eql?(other)
  self.class == other.class &&
    instance_values == other.instance_values
end

#hashvoid

:nocov:


133
134
135
# File 'lib/asciidoctor/doctest/example.rb', line 133

def hash
  self.class.hash ^ instance_values.hash
end

#nameString

Returns the name in format group_name:local_name.

Returns:

  • (String)

    the name in format group_name:local_name.


44
45
46
# File 'lib/asciidoctor/doctest/example.rb', line 44

def name
  [@group_name, @local_name].join(NAME_SEPARATOR)
end

#name=(*name) ⇒ void

Parameters:

  • name (String, Array<String>)

    a String in format group_name:local_name, or an Array with the group_name and the local_name.


52
53
54
55
# File 'lib/asciidoctor/doctest/example.rb', line 52

def name=(*name)
  name.flatten!
  @group_name, @local_name = name.one? ? name.first.split(NAME_SEPARATOR, 2) : name
end

#name_match?(pattern) ⇒ Boolean

Returns true if the name matches against the pattern, false otherwise.

Parameters:

  • pattern (String)

    the glob pattern (e.g. inline_*:with*).

Returns:

  • (Boolean)

    true if the name matches against the pattern, false otherwise.


61
62
63
64
65
66
# File 'lib/asciidoctor/doctest/example.rb', line 61

def name_match?(pattern)
  globs = pattern.split(NAME_SEPARATOR, 2)
  [group_name, local_name].zip(globs).all? do |name, glob|
    File.fnmatch? glob || '*', name.to_s
  end
end

#to_sString

Returns a copy of the content.

Returns:

  • (String)

    a copy of the content.


106
107
108
# File 'lib/asciidoctor/doctest/example.rb', line 106

def to_s
  content.dup
end