Class: MarkdownExec::FCB

Inherits:
Object show all
Defined in:
lib/fcb.rb

Overview

Fenced Code Block (FCB)

This class represents a fenced code block in a markdown document. It allows for setting and getting attributes related to the code block, such as body, call, headings, and more.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FCB

Returns a new instance of FCB.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fcb.rb', line 16

def initialize(options = {})
  @attrs = {
    body: nil,
    call: nil,
    headings: [],
    dname: nil,
    indent: '',
    name: nil,
    nickname: nil,
    oname: nil,
    reqs: [],
    shell: '',
    title: '',
    random: Random.new.rand,
    text: nil # displayable in menu
  }.merge(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)

:reek:ManualDispatch



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fcb.rb', line 69

def method_missing(method, *args, &block)
  method_name = method.to_s
  if @attrs.respond_to?(method_name)
    @attrs.send(method_name, *args, &block)
  elsif method_name[-1] == '='
    @attrs[method_name.chop.to_sym] = args[0]
  else
    @attrs[method_name.to_sym]
  end
rescue StandardError => err
  warn("ERROR ** FCB.method_missing(method: #{method_name}," \
       " *args: #{args.inspect}, &block)")
  warn err.inspect
  warn(caller[0..4])
  # raise StandardError, error
  raise err # Here, we simply propagate the original error instead of wrapping it in a StandardError.
end

Instance Method Details

#derive_title_from_bodyString

Derives a title from the body of an FCB object.

Parameters:

  • fcb (Object)

    The FCB object whose title is to be derived.

Returns:

  • (String)

    The derived title.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fcb.rb', line 41

def derive_title_from_body
  unless (body_content = @attrs[:body])
    # empty body -> empty title
    @attrs[:title] = ''
    return
  end

  # body -> title
  @attrs[:title] = if body_content.count == 1
                     body_content.first
                   else
                     format_multiline_body_as_title(body_content)
                   end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/fcb.rb', line 89

def respond_to_missing?(method_name, include_private = false)
  @attrs.key?(method_name.to_sym) || super
end

#title=(value) ⇒ Object



34
35
36
# File 'lib/fcb.rb', line 34

def title=(value)
  @attrs[:title] = value
end

#to_hObject



93
94
95
# File 'lib/fcb.rb', line 93

def to_h
  @attrs
end

#to_yamlObject



97
98
99
# File 'lib/fcb.rb', line 97

def to_yaml
  @attrs.to_yaml
end