summaryrefslogtreecommitdiff
path: root/controllers/session.rb
diff options
context:
space:
mode:
Diffstat (limited to 'controllers/session.rb')
-rw-r--r--controllers/session.rb27
1 files changed, 4 insertions, 23 deletions
diff --git a/controllers/session.rb b/controllers/session.rb
index fadb21b..9277a84 100644
--- a/controllers/session.rb
+++ b/controllers/session.rb
@@ -5,6 +5,7 @@ require 'babosa'
require 'fileutils'
require_relative '../logic/helpers/hash'
+require_relative '../components/storage'
module NanoBot
module Controllers
@@ -22,7 +23,9 @@ module NanoBot
if @stateless
@state = { history: [] }
else
- build_path_and_ensure_state_file!(state.strip)
+ @state_path = Components::Storage.build_path_and_ensure_state_file!(
+ state.strip, @cartridge
+ )
@state = load_state
end
end
@@ -44,28 +47,6 @@ module NanoBot
File.write(@state_path, JSON.generate(@state))
end
- def build_path_and_ensure_state_file!(key)
- path = Logic::Helpers::Hash.fetch(@cartridge, %i[state directory])
-
- path = "#{user_home!.sub(%r{/$}, '')}/.local/share/.nano-bots" if path.nil? || path.empty?
-
- path = "#{path.sub(%r{/$}, '')}/nano-bots-rb/#{@cartridge[:name].to_slug.normalize}"
- path = "#{path}/#{@cartridge[:version].to_slug.normalize}/#{key.to_slug.normalize}"
- path = "#{path}/state.json"
-
- @state_path = path
-
- FileUtils.mkdir_p(File.dirname(@state_path))
-
- File.write(@state_path, JSON.generate({ key:, history: [] })) unless File.exist?(@state_path)
- end
-
- def user_home!
- [Dir.home, `echo ~`.strip, '~'].find do |candidate|
- !candidate.nil? && !candidate.empty?
- end
- end
-
def boot(mode:)
return unless Logic::Helpers::Hash.fetch(@cartridge, %i[behaviors boot instruction])