diff options
author | icebaker <icebaker@proton.me> | 2023-05-11 19:24:50 -0300 |
---|---|---|
committer | icebaker <icebaker@proton.me> | 2023-05-11 19:24:50 -0300 |
commit | ec5e25547a401141586c87621266f9cd68c59e3c (patch) | |
tree | 547b3c7fa04c9e695785b9beeda0be5a4a77b006 /controllers/instance.rb |
first commit
Diffstat (limited to 'controllers/instance.rb')
-rw-r--r-- | controllers/instance.rb | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/controllers/instance.rb b/controllers/instance.rb new file mode 100644 index 0000000..5635658 --- /dev/null +++ b/controllers/instance.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require 'yaml' + +require_relative '../logic/helpers/hash' +require_relative '../components/provider' +require_relative './interfaces/repl' +require_relative './session' + +module NanoBot + module Controllers + class Instance + def initialize(cartridge_path:, state: nil) + load_cartridge!(cartridge_path) + + provider = Components::Provider.new(@cartridge[:provider]) + + @session = Session.new(provider:, cartridge: @cartridge, state:) + end + + def debug + @session.debug + end + + def eval(input) + @session.evaluate_and_print(input, mode: 'eval') + end + + def repl + Interfaces::REPL.start(@cartridge, @session) + end + + private + + def load_cartridge!(path) + @cartridge = Logic::Helpers::Hash.symbolize_keys( + YAML.safe_load(File.read(path), permitted_classes: [Symbol]) + ) + + inject_environment_variables!(@cartridge) + end + + def inject_environment_variables!(node) + case node + when Hash + node.each do |key, value| + node[key] = inject_environment_variables!(value) + end + when Array + node.each_with_index do |value, index| + node[index] = inject_environment_variables!(value) + end + when String + node.start_with?('ENV') ? ENV.fetch(node.sub(/^ENV./, '')) : node + else + node + end + end + end + end +end |