[UNSTABLE] Make sequence.args user configurable

pull/551/head
Rick Carlino 2017-12-01 09:22:29 -06:00
parent ccb873f65f
commit c2c9afb14d
8 changed files with 36 additions and 12 deletions

View File

@ -6,7 +6,6 @@ module SequenceMigration
CREATED_ON = "NOVEMBER 30 2017"
def up
binding.pry
sequence.args["locals"] ||= Sequence::NOTHING
end
end

View File

@ -154,7 +154,7 @@ module CeleryScriptSettingsBag
.defineNode(:send_message, [:message, :message_type], [:channel])
.defineNode(:execute, [:sequence_id])
.defineNode(:_if, [:lhs, :op, :rhs, :_then, :_else], [:pair])
.defineNode(:sequence, [:version], STEPS)
.defineNode(:sequence, [:version, :locals], STEPS)
.defineNode(:home, [:speed, :axis], [])
.defineNode(:find_home, [:speed, :axis], [])
.defineNode(:zero, [:axis], [])

View File

@ -3,7 +3,9 @@
# most of the functionality of a programming language such a variables and
# conditional logic.
class Sequence < ApplicationRecord
NOTHING = { "kind" => "nothing", "args" => {} }
NOTHING = { "kind" => "nothing", "args" => {} }
DEFAULT_ARGS = { "locals" => NOTHING,
"version" => SequenceMigration::Base.latest_version }
# Does some extra magic for serialized columns for us, such as providing a
# default value and making hashes have indifferent access.
class CustomSerializer
@ -47,9 +49,7 @@ class Sequence < ApplicationRecord
before_validation :set_defaults
def set_defaults
self.args ||= {}
self.args["version"] ||= SequenceMigration::Base.latest_version
self.args["locals"] ||= NOTHING
self.args = {}.merge(DEFAULT_ARGS).merge(self.args)
self.color ||= "gray"
self.kind ||= "sequence"
end

View File

@ -22,15 +22,13 @@ module Sequences
# first level, though. I would like to recursively strip out "noise" via
# CeleryScript::JSONClimber. I am holding off for now in the name of time.
(inputs[:body] || []).map! { |x| x.slice(*ALLOWED_NODE_KEYS) }
inputs[:args] = Sequence::DEFAULT_ARGS.merge(inputs[:args] || {})
add_error :body, :syntax_error, checker.error.message if !checker.valid?
end
def seq
@seq ||= {body: [],
args: { version: SequenceMigration::Base.latest_version },
kind: "sequence"}.merge(inputs.symbolize_keys.slice(:body,
:kind,
:args))
@seq ||= {body: [], args: {}, kind: "sequence"}
.merge(inputs.symbolize_keys.slice(:body, :kind, :args))
end
def reload_dependencies(sequence)

View File

@ -76,6 +76,31 @@ describe Api::SequencesController do
expect(validated_count).to eq(new_count)
end
it 'doesnt allow nonsense in `sequence.args.locals`' do
input = { name: "Scare Birds",
body: [],
# Intentional nonsense to check validation logic.
args: { locals: { kind: "wait", args: { milliseconds: 5000 } } }
}
sign_in user
post :create, body: input.to_json, params: {format: :json}
expect(response.status).to eq(422)
end
it 'strips excess `args`' do
pending
input = { name: "Scare Birds",
body: [],
# Intentional nonsense to check validation logic.
args: { foo: "BAR" } }
sign_in user
post :create, body: input.to_json, params: {format: :json}
expect(response.status).to eq(200)
expect(sequence.last.args[:foo]).to eq(nil)
end
it 'tracks Points' do
point = FactoryBot.create(:point, device: user.device)
SequenceDependency.delete_all

View File

@ -8,6 +8,8 @@ describe Api::SequencesController do
let(:user) { FactoryBot.create(:user) }
it 'doesnt allow nonsense in `sequence.args.locals`'
it 'refreshes sequence dependencies on update' do
SequenceDependency.destroy_all
old_count = SequenceDependency.count

View File

@ -6,6 +6,7 @@ describe CeleryScript::Checker do
{
kind: "sequence",
args: {
locals: Sequence::NOTHING,
version: 0
},
comment: "Properly formatted, syntactically valid sequence.",

View File

@ -64,7 +64,6 @@ describe SequenceMigration do
expect(s.body[0]["args"]["speed"]).to eq(200)
expect(s.body[1]["args"]["speed"]).to eq(200)
expect(s.body[2]["args"]["speed"]).to eq(200)
expect(s.args["locals"]).to eq(nil)
s.maybe_migrate