Module: HashDelegatorSelf
- Included in:
- MarkdownExec::HashDelegatorParent
- Defined in:
- lib/hash_delegator.rb
Instance Method Summary collapse
-
#apply_color_from_hash(string, color_methods, color_key, default_method: 'plain') ⇒ String
Applies an ANSI color method to a string using a specified color key.
-
#block_find(blocks, key, value, default = nil) ⇒ Object?
Searches for the first element in a collection where the specified key matches a given value.
- #code_merge(*bodies) ⇒ Object
- #count_matches_in_lines(lines, regex) ⇒ Object
- #create_directory_for_file(file_path) ⇒ Object
-
#create_file_and_write_string_with_permissions(file_path, content, chmod_value) ⇒ Object
Creates a file at the specified path, writes the given content to it, and sets file permissions if required.
-
#default_block_title_from_body(fcb) ⇒ Object
Updates the title of an FCB object from its body content if the title is nil or empty.
-
#delete_consecutive_blank_lines!(blocks_menu) ⇒ Object
delete the current line if it is empty and the previous is also empty.
-
#error_handler(name = '', opts = {}, error: $!) ⇒ Object
HashDelegator.remove_file_without_standard_errors(temp_blocks_file_path) end.
-
#format_execution_streams(key, files = {}) ⇒ String
Formats and returns the execution streams (like stdin, stdout, stderr) for a given key.
-
#indent_all_lines(body, indent = nil) ⇒ String
Indents all lines in a given string with a specified indentation string.
- #initialize_fcb_names(fcb) ⇒ Object
- #join_code_lines(lines) ⇒ Object
- #merge_lists(*args) ⇒ Object
- #next_link_state(block_name_from_cli:, was_using_cli:, block_state:, block_name: nil) ⇒ Object
- #parse_yaml_data_from_body(body) ⇒ Object
-
#read_required_blocks_from_temp_file(temp_blocks_file_path) ⇒ Array<String>
Reads required code blocks from a temporary file specified by an environment variable.
- #remove_file_without_standard_errors(path) ⇒ Object
-
#safeval(str) ⇒ Object
Evaluates the given string as Ruby code and rescues any StandardErrors.
- #set_file_permissions(file_path, chmod_value) ⇒ Object
-
#tty_prompt_without_disabled_symbol ⇒ TTY::Prompt
Creates a TTY prompt with custom settings.
-
#update_menu_attrib_yield_selected(fcb:, messages:, configuration: {}, &block) ⇒ Object
Updates the attributes of the given fcb object and conditionally yields to a block.
- #write_execution_output_to_file(files, filespec) ⇒ Object
-
#yield_line_if_selected(line, selected_messages, &block) ⇒ Object
Yields a line as a new block if the selected message type includes :line.
Instance Method Details
#apply_color_from_hash(string, color_methods, color_key, default_method: 'plain') ⇒ String
Applies an ANSI color method to a string using a specified color key. The method retrieves the color method from the provided hash. If the color key is not present in the hash, it uses a default color method.
54 55 56 57 |
# File 'lib/hash_delegator.rb', line 54 def apply_color_from_hash(string, color_methods, color_key, default_method: 'plain') color_method = color_methods.fetch(color_key, default_method).to_sym string.to_s.send(color_method) end |
#block_find(blocks, key, value, default = nil) ⇒ Object?
Searches for the first element in a collection where the specified key matches a given value. This method is particularly useful for finding a specific hash-like object within an enumerable collection. If no match is found, it returns a specified default value.
87 88 89 |
# File 'lib/hash_delegator.rb', line 87 def block_find(blocks, key, value, default = nil) blocks.find { |item| item[key] == value } || default end |
#code_merge(*bodies) ⇒ Object
91 92 93 |
# File 'lib/hash_delegator.rb', line 91 def code_merge(*bodies) merge_lists(*bodies) end |
#count_matches_in_lines(lines, regex) ⇒ Object
95 96 97 |
# File 'lib/hash_delegator.rb', line 95 def count_matches_in_lines(lines, regex) lines.count { |line| line.to_s.match(regex) } end |
#create_directory_for_file(file_path) ⇒ Object
99 100 101 |
# File 'lib/hash_delegator.rb', line 99 def create_directory_for_file(file_path) FileUtils.mkdir_p(File.dirname(file_path)) end |
#create_file_and_write_string_with_permissions(file_path, content, chmod_value) ⇒ Object
Creates a file at the specified path, writes the given content to it, and sets file permissions if required. Handles any errors encountered during the process.
109 110 111 112 113 114 115 116 |
# File 'lib/hash_delegator.rb', line 109 def (file_path, content, chmod_value) create_directory_for_file(file_path) File.write(file_path, content) (file_path, chmod_value) unless chmod_value.zero? rescue StandardError error_handler('create_file_and_write_string_with_permissions') end |
#default_block_title_from_body(fcb) ⇒ Object
Updates the title of an FCB object from its body content if the title is nil or empty.
123 124 125 126 127 |
# File 'lib/hash_delegator.rb', line 123 def default_block_title_from_body(fcb) return unless fcb.title.nil? || fcb.title.empty? fcb.derive_title_from_body end |
#delete_consecutive_blank_lines!(blocks_menu) ⇒ Object
delete the current line if it is empty and the previous is also empty
130 131 132 133 134 135 |
# File 'lib/hash_delegator.rb', line 130 def delete_consecutive_blank_lines!() .process_and_conditionally_delete! do |prev_item, current_item, _next_item| prev_item&.fetch(:chrome, nil) && !prev_item&.fetch(:oname).present? && current_item&.fetch(:chrome, nil) && !current_item&.fetch(:oname).present? end end |
#error_handler(name = '', opts = {}, error: $!) ⇒ Object
HashDelegator.remove_file_without_standard_errors(temp_blocks_file_path) end
146 147 148 149 150 151 |
# File 'lib/hash_delegator.rb', line 146 def error_handler(name = '', opts = {}, error: $!) Exceptions.error_handler( "HashDelegator.#{name} -- #{error}", opts ) end |
#format_execution_streams(key, files = {}) ⇒ String
Formats and returns the execution streams (like stdin, stdout, stderr) for a given key. It concatenates the array of strings found under the specified key in the run_state’s files.
169 170 171 |
# File 'lib/hash_delegator.rb', line 169 def format_execution_streams(key, files = {}) (files || {}).fetch(key, []).join end |
#indent_all_lines(body, indent = nil) ⇒ String
Indents all lines in a given string with a specified indentation string.
177 178 179 180 181 |
# File 'lib/hash_delegator.rb', line 177 def indent_all_lines(body, indent = nil) return body unless indent&.non_empty? body.lines.map { |line| indent + line.chomp }.join("\n") end |
#initialize_fcb_names(fcb) ⇒ Object
183 184 185 |
# File 'lib/hash_delegator.rb', line 183 def initialize_fcb_names(fcb) fcb.oname = fcb.dname = fcb.title || '' end |
#join_code_lines(lines) ⇒ Object
187 188 189 |
# File 'lib/hash_delegator.rb', line 187 def join_code_lines(lines) ((lines || []) + ['']).join("\n") end |
#merge_lists(*args) ⇒ Object
191 192 193 194 195 |
# File 'lib/hash_delegator.rb', line 191 def merge_lists(*args) # Filters out nil values, flattens the arrays, and ensures an empty list is returned if no valid lists are provided merged = args.compact.flatten merged.empty? ? [] : merged end |
#next_link_state(block_name_from_cli:, was_using_cli:, block_state:, block_name: nil) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/hash_delegator.rb', line 197 def next_link_state(block_name_from_cli:, was_using_cli:, block_state:, block_name: nil) # Set block_name based on block_name_from_cli block_name = @cli_block_name if block_name_from_cli # Determine the state of breaker based on was_using_cli and the block type # true only when block_name is nil, block_name_from_cli is false, was_using_cli is true, and the block_state.block[:shell] equals BlockType::BASH. In all other scenarios, breaker is false. breaker = !block_name && !block_name_from_cli && was_using_cli && block_state.block[:shell] == BlockType::BASH # Reset block_name_from_cli if the conditions are not met block_name_from_cli ||= false [block_name, block_name_from_cli, breaker] end |
#parse_yaml_data_from_body(body) ⇒ Object
211 212 213 214 215 |
# File 'lib/hash_delegator.rb', line 211 def parse_yaml_data_from_body(body) body.any? ? YAML.load(body.join("\n")) : {} rescue StandardError error_handler('parse_yaml_data_from_body', { abort: true }) end |
#read_required_blocks_from_temp_file(temp_blocks_file_path) ⇒ Array<String>
Reads required code blocks from a temporary file specified by an environment variable.
219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/hash_delegator.rb', line 219 def read_required_blocks_from_temp_file(temp_blocks_file_path) return [] if temp_blocks_file_path.to_s.empty? if File.exist?(temp_blocks_file_path) File.readlines( temp_blocks_file_path, chomp: true ) else [] end end |
#remove_file_without_standard_errors(path) ⇒ Object
231 232 233 |
# File 'lib/hash_delegator.rb', line 231 def remove_file_without_standard_errors(path) FileUtils.rm_f(path) end |
#safeval(str) ⇒ Object
Evaluates the given string as Ruby code and rescues any StandardErrors. If an error occurs, it calls the error_handler method with ‘safeval’.
239 240 241 242 243 |
# File 'lib/hash_delegator.rb', line 239 def safeval(str) eval(str) rescue StandardError # catches NameError, StandardError error_handler('safeval') end |
#set_file_permissions(file_path, chmod_value) ⇒ Object
245 246 247 |
# File 'lib/hash_delegator.rb', line 245 def (file_path, chmod_value) File.chmod(chmod_value, file_path) end |
#tty_prompt_without_disabled_symbol ⇒ TTY::Prompt
Creates a TTY prompt with custom settings. Specifically, it disables the default ‘cross’ symbol and defines a lambda function to handle interrupts.
252 253 254 255 256 257 258 259 260 |
# File 'lib/hash_delegator.rb', line 252 def tty_prompt_without_disabled_symbol TTY::Prompt.new( interrupt: lambda { puts raise TTY::Reader::InputInterrupt }, symbols: { cross: ' ' } ) end |
#update_menu_attrib_yield_selected(fcb:, messages:, configuration: {}, &block) ⇒ Object
Updates the attributes of the given fcb object and conditionally yields to a block. It initializes fcb names and sets the default block title from fcb’s body. If the fcb has a body and meets certain conditions, it yields to the given block.
269 270 271 272 273 274 275 276 |
# File 'lib/hash_delegator.rb', line 269 def (fcb:, messages:, configuration: {}, &block) initialize_fcb_names(fcb) return unless fcb.body default_block_title_from_body(fcb) MarkdownExec::Filter.yield_to_block_if_applicable(fcb, , configuration, &block) end |
#write_execution_output_to_file(files, filespec) ⇒ Object
278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/hash_delegator.rb', line 278 def write_execution_output_to_file(files, filespec) FileUtils.mkdir_p File.dirname(filespec) File.write( filespec, ["-STDOUT-\n", format_execution_streams(ExecutionStreams::StdOut, files), "-STDERR-\n", format_execution_streams(ExecutionStreams::StdErr, files), "-STDIN-\n", format_execution_streams(ExecutionStreams::StdIn, files), "\n"].join ) end |
#yield_line_if_selected(line, selected_messages, &block) ⇒ Object
Yields a line as a new block if the selected message type includes :line.
297 298 299 300 301 |
# File 'lib/hash_delegator.rb', line 297 def yield_line_if_selected(line, , &block) return unless block && .include?(:line) block.call(:line, MarkdownExec::FCB.new(body: [line])) end |