[UNSTABLE] Wrote JSONClimber to simplify sequence migrations.

pull/305/head
Rick Carlino 2017-01-05 14:15:44 -06:00
parent 497e748eb0
commit 454360f415
4 changed files with 67 additions and 1 deletions

View File

@ -0,0 +1,38 @@
module CeleryScript
# THIS IS A MORE MINIMAL VERSION OF CeleryScript::TreeClimber.
# It is a NON-VALIDATING tree climber.
# Don't use this on unverified data structures.
class JSONClimber
HASH_ONLY = "Expected a Hash."
NOT_NODE = "Expected hash with atleast a `kind` and `args` prop."
def self.climb(thing, &callable)
raise HASH_ONLY unless thing.is_a?(Hash)
raise NOT_NODE unless is_node?(thing)
go(thing, callable)
thing
rescue => q
binding.pry
end
private
def self.is_node?(maybe)
maybe.is_a?(Hash) &&
maybe.keys.include?(:kind) &&
maybe.keys.include?(:args)
end
def self.go(thing, callable)
if is_node?(thing)
callable.call(thing)
# Recurse into each arg
thing[:args].map { |x| go(x.last, callable) }
# Maybe recurse into body.
(thing[:body] || []).each { |x| go(x, callable) }
end
end
end
end

View File

@ -28,7 +28,8 @@ module SequenceMigration
AddVersionInfo,
UpdateChannelNames,
AddToolsToMoveAbs,
UpdateIfStatement
UpdateIfStatement,
CleanupArgNames
]
end

View File

@ -0,0 +1,19 @@
module SequenceMigration
# Background:
# This is a refactor of what once was called the `if_statement` block:
# * Added a `nothing` node type (sorry, we actually need it now).
# * if_statement becomes _if
# * sub_sequence_id becomes _then
# * Add _else arg to _if.
# * _then/_else expect a execute or nothing node instead of a number.
class CleanupArgNames < Base
VERSION = 4
CREATED_ON = "January 5 2017"
def up
sequence.traverse do |node|
puts node[:kind]
end
end
end
end

View File

@ -45,4 +45,12 @@ class Sequence < ActiveRecord::Base
def self.random
Sequence.order("RANDOM()").first
end
def traverse(&blk)
hash = as_json
.tap { |x| x[:kind] = "sequence" }
.deep_symbolize_keys
.slice(:kind, :args, :body)
CeleryScript::JSONClimber.climb(hash, &blk)
end
end