about summary refs log tree commit diff
path: root/app/lib
diff options
context:
space:
mode:
authorFire Demon <firedemon@creature.cafe>2020-07-31 10:58:52 -0500
committerFire Demon <firedemon@creature.cafe>2020-08-30 05:45:16 -0500
commitd9339677d6daa33dc020cf3400c3dec210b06f31 (patch)
tree38de2c33e4f61df299348b89da2e9c8600d78247 /app/lib
parent4c47a96f67d303dec7b6047474380050053ac3d7 (diff)
[Command Tags] Add support for persistent variables
Diffstat (limited to 'app/lib')
-rw-r--r--app/lib/command_tag/command/account_tools.rb (renamed from app/lib/command_tag/commands/account_tools.rb)2
-rw-r--r--app/lib/command_tag/command/hello_world.rb (renamed from app/lib/command_tag/commands/hello_world.rb)2
-rw-r--r--app/lib/command_tag/command/parent_status_tools.rb (renamed from app/lib/command_tag/commands/parent_status_tools.rb)2
-rw-r--r--app/lib/command_tag/command/status_tools.rb (renamed from app/lib/command_tag/commands/status_tools.rb)2
-rw-r--r--app/lib/command_tag/command/text_tools.rb (renamed from app/lib/command_tag/commands/text_tools.rb)2
-rw-r--r--app/lib/command_tag/command/variables.rb40
-rw-r--r--app/lib/command_tag/commands.rb5
-rw-r--r--app/lib/command_tag/commands/variables.rb22
-rw-r--r--app/lib/command_tag/processor.rb2
9 files changed, 51 insertions, 28 deletions
diff --git a/app/lib/command_tag/commands/account_tools.rb b/app/lib/command_tag/command/account_tools.rb
index 473df7a39..6d2e8eca3 100644
--- a/app/lib/command_tag/commands/account_tools.rb
+++ b/app/lib/command_tag/command/account_tools.rb
@@ -1,5 +1,5 @@
 # frozen_string_literal: true
-module CommandTag::Commands::AccountTools
+module CommandTag::Command::AccountTools
   def handle_account_at_start(args)
     return if args[0].blank?
 
diff --git a/app/lib/command_tag/commands/hello_world.rb b/app/lib/command_tag/command/hello_world.rb
index cc770ef80..ab10b495b 100644
--- a/app/lib/command_tag/commands/hello_world.rb
+++ b/app/lib/command_tag/command/hello_world.rb
@@ -1,6 +1,6 @@
 # frozen_string_literal: true
 
-module CommandTag::Commands::HelloWorld
+module CommandTag::Command::HelloWorld
   def handle_helloworld_startup
     @vars['hello_world'] = ['Hello, world!']
   end
diff --git a/app/lib/command_tag/commands/parent_status_tools.rb b/app/lib/command_tag/command/parent_status_tools.rb
index d19cf902b..5f127b2da 100644
--- a/app/lib/command_tag/commands/parent_status_tools.rb
+++ b/app/lib/command_tag/command/parent_status_tools.rb
@@ -1,5 +1,5 @@
 # frozen_string_literal: true
-module CommandTag::Commands::ParentStatusTools
+module CommandTag::Command::ParentStatusTools
   def handle_publish_once_at_end(_)
     is_blank = status_text_blank?
     @status.published = true if @parent.blank? || !is_blank?
diff --git a/app/lib/command_tag/commands/status_tools.rb b/app/lib/command_tag/command/status_tools.rb
index d00efe054..7187ebd24 100644
--- a/app/lib/command_tag/commands/status_tools.rb
+++ b/app/lib/command_tag/command/status_tools.rb
@@ -1,5 +1,5 @@
 # frozen_string_literal: true
-module CommandTag::Commands::StatusTools
+module CommandTag::Command::StatusTools
   def handle_title_before_save(args)
     return unless author_of_status?
 
diff --git a/app/lib/command_tag/commands/text_tools.rb b/app/lib/command_tag/command/text_tools.rb
index e004c7bec..2c44167b4 100644
--- a/app/lib/command_tag/commands/text_tools.rb
+++ b/app/lib/command_tag/command/text_tools.rb
@@ -1,6 +1,6 @@
 # frozen_string_literal: true
 
-module CommandTag::Commands::TextTools
+module CommandTag::Command::TextTools
   def handle_code_at_start(args)
     return if args.count < 2
 
diff --git a/app/lib/command_tag/command/variables.rb b/app/lib/command_tag/command/variables.rb
new file mode 100644
index 000000000..b0781d1aa
--- /dev/null
+++ b/app/lib/command_tag/command/variables.rb
@@ -0,0 +1,40 @@
+# frozen_string_literal: true
+
+module CommandTag::Command::Variables
+  def handle_variables_startup
+    @vars.merge!(persistent_vars_from(@account.metadata.fields)) if @account.metadata.present?
+  end
+
+  def handle_variables_shutdown
+    @account.metadata.update!(fields: nonpersistent_vars_from(@account.metadata.fields).merge(persistent_vars_from(@vars)))
+  end
+
+  def handle_set_at_start(args)
+    return if args.blank?
+
+    args[0] = normalize(args[0])
+
+    case args.count
+    when 1
+      @vars.delete(args[0])
+    else
+      @vars[args[0]] = args[1..-1]
+    end
+  end
+
+  def do_unset_at_start(args)
+    args.each do |arg|
+      @vars.delete(normalize(arg))
+    end
+  end
+
+  private
+
+  def persistent_vars_from(vars)
+    vars.select { |key, value| key.start_with?('persist:') && value.present? && value.is_a?(Array) }
+  end
+
+  def nonpersistent_vars_from(vars)
+    vars.reject { |key, value| key.start_with?('persist:') || value.blank? }
+  end
+end
diff --git a/app/lib/command_tag/commands.rb b/app/lib/command_tag/commands.rb
index 0248e6e99..f27486427 100644
--- a/app/lib/command_tag/commands.rb
+++ b/app/lib/command_tag/commands.rb
@@ -1,7 +1,10 @@
 # frozen_string_literal: true
+
+Dir[File.join(__dir__, 'command', '*.rb')].sort.each { |file| require file }
+
 module CommandTag::Commands
   def self.included(base)
-    CommandTag::Commands.constants.map(&CommandTag::Commands.method(:const_get)).grep(Module) do |mod|
+    CommandTag::Command.constants.map(&CommandTag::Command.method(:const_get)).grep(Module) do |mod|
       base.include(mod)
     end
   end
diff --git a/app/lib/command_tag/commands/variables.rb b/app/lib/command_tag/commands/variables.rb
deleted file mode 100644
index 997131cd9..000000000
--- a/app/lib/command_tag/commands/variables.rb
+++ /dev/null
@@ -1,22 +0,0 @@
-# frozen_string_literal: true
-
-module CommandTag::Commands::Variables
-  def handle_set_at_start(args)
-    return if args.blank?
-
-    args[0] = normalize(args[0])
-
-    case args.count
-    when 1
-      @vars.delete(args[0])
-    else
-      @vars[args[0]] = args[1..-1]
-    end
-  end
-
-  def do_unset_at_start(args)
-    args.each do |arg|
-      @vars.delete(normalize(arg))
-    end
-  end
-end
diff --git a/app/lib/command_tag/processor.rb b/app/lib/command_tag/processor.rb
index aba7be158..13c987fec 100644
--- a/app/lib/command_tag/processor.rb
+++ b/app/lib/command_tag/processor.rb
@@ -10,6 +10,8 @@
 #                                                                             #
 ###############################################################################
 
+require_relative 'commands'
+
 class CommandTag::Processor
   include Redisable
   include ImgProxyHelper