lib/fcb.rb in markdown_exec-1.6 vs lib/fcb.rb in markdown_exec-1.7
- old
+ new
@@ -17,20 +17,54 @@
@attrs = {
body: nil,
call: nil,
headings: [],
dname: nil,
+ indent: '',
name: nil,
oname: nil,
reqs: [],
shell: '',
title: '',
random: Random.new.rand,
text: nil # displayable in menu
}.merge(options)
end
+ def title=(value)
+ @attrs[:title] = value
+ end
+
+ # Derives a title from the body of an FCB object.
+ # @param fcb [Object] The FCB object whose title is to be derived.
+ # @return [String] The derived title.
+ def derive_title_from_body
+ body_content = @attrs[:body]
+ unless body_content
+ @attrs[:title] = ''
+ return
+ end
+
+ @attrs[:title] = if body_content.count == 1
+ body_content.first
+ else
+ format_multiline_body_as_title(body_content)
+ end
+ end
+
+ private
+
+ # Formats multiline body content as a title string.
+ # indents all but first line with two spaces so it displays correctly in menu
+ # @param body_lines [Array<String>] The lines of body content.
+ # @return [String] Formatted title.
+ def format_multiline_body_as_title(body_lines)
+ body_lines.map.with_index do |line, index|
+ index.zero? ? line : " #{line}"
+ end.join("\n") << "\n"
+ end
+
# :reek:ManualDispatch
def method_missing(method, *args, &block)
method_name = method.to_s
if @attrs.respond_to?(method_name)
@attrs.send(method_name, *args, &block)
@@ -46,11 +80,13 @@
warn(caller[0..4])
# raise StandardError, error
raise err # Here, we simply propagate the original error instead of wrapping it in a StandardError.
end
- def respond_to_missing?(method_name, _include_private = false)
+ public
+
+ def respond_to_missing?(method_name, include_private = false)
@attrs.key?(method_name.to_sym) || super
end
def to_h
@attrs
@@ -61,19 +97,23 @@
end
end
end
if $PROGRAM_NAME == __FILE__
+ require 'bundler/setup'
+ Bundler.require(:default)
+
require 'minitest/autorun'
require 'yaml'
class FCBTest < Minitest::Test
def setup
@fcb_data = {
body: 'Sample body',
call: 'Sample call',
headings: %w[Header1 Header2],
dname: 'Sample name',
+ indent: '',
name: 'Sample name',
oname: 'Sample name',
reqs: %w[req1 req2],
shell: 'bash',
text: 'Sample Text',