Sha256: 5b8bf51cc57a8b89122dd7dee580d5c21232b85db6c333d4065a38f0cb7c3af3

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module Bridgetown
  module Utils
    module RubyExec
      # @param context [Layout, Model::RepoOrigin] the execution context (i.e.
      #   `self` for the Ruby code)
      # @param ruby_code [String] the Ruby code to execute
      # @param file_path [String] the absolute path to the file
      # @param starting_line [Integer] the number to list as the starting line
      #   for compilation errors
      # @return [Hash]
      def self.process_ruby_data(context, ruby_code, file_path, starting_line)
        ruby_data = context.instance_eval(ruby_code, file_path.to_s, starting_line)
        ruby_data.is_a?(Array) ? { rows: ruby_data } : ruby_data.to_h
      rescue StandardError => e
        raise(
          "Ruby code isn't returning an array, or object which " \
          "responds to `to_h' (#{e.message})"
        )
      end

      def self.search_data_for_ruby_code(convertible)
        return if convertible.data.empty?

        # Iterate using `keys` here so inline Ruby script can add new data keys
        # if necessary without an error
        data_keys = convertible.data.keys
        data_keys.each do |k|
          v = convertible.data[k]
          next unless v.is_a?(Proc)

          convertible.data[k] = convertible.instance_exec(&v)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bridgetown-core-2.0.0.beta3 lib/bridgetown-core/utils/ruby_exec.rb
bridgetown-core-2.0.0.beta2 lib/bridgetown-core/utils/ruby_exec.rb
bridgetown-core-2.0.0.beta1 lib/bridgetown-core/utils/ruby_exec.rb