Class: MarkdownExec::FCB

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

Overview

Define a mock fenced code block class for testing purposes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chrome: false, dname: nil, oname: nil, shell: '', start_line: nil, type: nil) ⇒ FCB

Returns a new instance of FCB.



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

def initialize(options = {})
  @attrs = {
    body: nil,
    call: nil,
    dname: nil,
    headings: [],
    indent: '',
    name: nil,
    nickname: nil,
    oname: nil,
    random: Random.new.rand,
    reqs: [],
    shell: '',
    start_line: nil,
    text: nil, # displayable in menu
    title: '',
    type: ''
  }.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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/fcb.rb', line 107

def method_missing(method, *args, &block)
  method_name = method.to_s
  if @attrs.respond_to?(method_name)
if method_name == 'fetch'
  ww args[0]
end
    @attrs.send(method_name, *args, &block)
  elsif method_name[-1] == '='
# ww caller.take(3) if method_name == 'label='
ww method_name
    @attrs[method_name.chop.to_sym] = args[0]
  else
# ww caller.take(3) if method_name == 'label'
ww method_name
    @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 err
end

Instance Attribute Details

#chromeObject

Returns the value of attribute chrome.



232
233
234
# File 'lib/filter.rb', line 232

def chrome
  @chrome
end

#disabledObject

Returns the value of attribute disabled.



232
233
234
# File 'lib/filter.rb', line 232

def disabled
  @disabled
end

#onameObject

Returns the value of attribute oname.



232
233
234
# File 'lib/filter.rb', line 232

def oname
  @oname
end

#shellObject

Returns the value of attribute shell.



137
138
139
# File 'lib/fcb.rb', line 137

def shell
  @attrs[:shell]
end

#start_lineObject

Returns the value of attribute start_line.



232
233
234
# File 'lib/filter.rb', line 232

def start_line
  @start_line
end

#typeObject

Returns the value of attribute type.



145
146
147
# File 'lib/fcb.rb', line 145

def type
  @attrs[:type]
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
                     FCB::format_multiline_body_as_title(body_content)
                   end
end

#fetch(key, default) ⇒ Object



246
247
248
# File 'lib/filter.rb', line 246

def fetch(key, default)
  instance_variable_get("@#{key}") || default
end

#for_menu!(block_calls_scan: , block_name_match: , block_name_nick_match: ) ⇒ Object

Processes a block to generate its summary, modifying its attributes

based on various matching criteria.

It handles special formatting for bash blocks, extracting and setting

properties like call, stdin, stdout, and dname.

Parameters:

  • fcb (Object)

    An object representing a functional code block.

Returns:

  • (Object)

    The modified functional code block with updated summary attributes.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fcb.rb', line 64

def for_menu!(
  block_calls_scan: @delegate_object[:block_calls_scan],
  block_name_match: @delegate_object[:block_name_match],
  block_name_nick_match: @delegate_object[:block_name_nick_match]
)
  call = @attrs[:call] = @attrs[:start_line]&.match(
    Regexp.new(block_calls_scan)
  )&.fetch(1, nil)
  titlexcall = call ? @attrs[:title].sub("%#{call}", '') : @attrs[:title]

  oname = if block_name_nick_match.present? &&
             @attrs[:oname] =~ Regexp.new(block_name_nick_match)
            @attrs[:nickname] = $~[0]
            derive_title_from_body
          else
            bm = NamedCaptureExtractor::extract_named_groups(
              titlexcall,
              block_name_match
            )
            bm && bm[1] ? bm[:title] : titlexcall
          end
  @attrs[:title] = @attrs[:oname] = oname

  @attrs[:dname] = HashDelegator.indent_all_lines(
    (yield oname, BLOCK_TYPE_COLOR_OPTIONS[@attrs[:type]]),
    @attrs[:indent]
  )
end

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

Returns:

  • (Boolean)


133
134
135
# File 'lib/fcb.rb', line 133

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

#to_hObject



153
154
155
# File 'lib/fcb.rb', line 153

def to_h
  @attrs.to_h
end

#to_yamlObject



157
158
159
# File 'lib/fcb.rb', line 157

def to_yaml
  @attrs.to_yaml
end

#variable_expansion!(pattern, replacement_dictionary) ⇒ Object

Expand in body and dname



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/fcb.rb', line 162

def variable_expansion!(pattern, replacement_dictionary)
  ### update name, nickname, title, label ???
  @attrs[:dname] = @attrs[:dname].gsub(pattern) { |match|
    replacement_dictionary[match]
  }
  if @attrs[:body]
    @attrs[:body] = @attrs[:body].map do |line|
      if line.empty?
        line
      else
        line.gsub(pattern) do |match|
          replacement_dictionary[match]
        end
      end
    end
  end
end