Skip to content

Instantly share code, notes, and snippets.

@dsingleton
Last active August 29, 2015 14:06
Show Gist options
  • Save dsingleton/0c93ebb223b83d2db527 to your computer and use it in GitHub Desktop.
Save dsingleton/0c93ebb223b83d2db527 to your computer and use it in GitHub Desktop.
Generate all outcomes based on next-node rules for SPL
namespace :smartdown do
def smartdown_coversheet_path(flow_name, coversheet_name= flow_name)
File.join(smartdown_flow_path(flow_name), "#{coversheet_name}.txt")
end
def smartdown_flow_path(flow_name)
Rails.root.join('lib', 'smartdown_flows', flow_name)
end
def smartdown_set_flow_status(flow_name, status)
coversheet = smartdown_coversheet_path(flow_name)
IO.write(coversheet, File.read(coversheet).sub(/status: .*/, "status: #{status}"))
end
desc "Rename a Smartdown directory package, including coversheet"
task :rename, [:old_name, :new_name] do |t, args|
old_name = args[:old_name]
new_name = args[:new_name]
`git mv #{smartdown_flow_path(old_name)} #{smartdown_flow_path(new_name)}`
`git mv #{smartdown_coversheet_path(new_name, old_name)} #{smartdown_coversheet_path(new_name)}`
end
desc "Convert a Smartdown package to transition status/name"
task :to_transition, [:name] do |t, args|
name = args[:name]
smartdown_set_flow_status(name, 'transition')
Rake::Task["smartdown:rename"].invoke(name, "#{name}-transition")
end
desc "Convert a Smartdown package from transition status/name to published"
task :from_transition, [:name] do |t, args|
name = args[:name]
smartdown_set_flow_status(name, 'published')
Rake::Task["smartdown:rename"].invoke(name, name.chomp('-transition'))
end
desc "Convert a Smartdown package from transition status/name to published"
task :find_missing_nodes, [:name] do |t, args|
flow = Smartdown.parse(smartdown_coversheet_path(args[:name]))
def nested_outcomes(rules)
rules.map { |rule|
if rule.respond_to? :outcome
rule.outcome
else
nested_outcomes(rule.children)
end
}
end
outcome_aspect_map = {
ma: "the mother is entitled to maternity allowance",
ma14: "the mother may be entitled to 14 week maternity allowance",
pay: "the mother or principal adopter is entitled to pay",
leave: "the mother or principal adopter is entitled to leave",
spl: "the mother or principal adopter is entitled to shared parental leave (if entitled to leave) and shared parental pay (if entitled to pay)",
patleave: "the partner is entitled to paternity leave",
patpay: "the partner is entitled to paternity pay",
additional: "the partner is entitled to additional paternity leave (if entitled to leave) and additional paternity pay (if entitled to pay)",
patspl: "the partner is entitled to shared parental leave (if entitled to leave) and shared parental pay (if entitled to pay)",
}
cover_node = flow.name.to_s
start_node = flow.coversheet.elements.find { |e|
e.is_a?(Smartdown::Model::Element::StartButton)
}.start_node.to_s
destination_nodes = flow.nodes.map(&:elements).flatten.select { |e|
e.is_a?(Smartdown::Model::NextNodeRules)
}.map(&:rules).map {
|rules| nested_outcomes(rules)
}.flatten.map(&:to_s).uniq
all_nodes = ([cover_node, start_node] + flow.nodes.map(&:name))
missing_nodes = destination_nodes - all_nodes
missing_nodes.each do |node_name|
node_filepath = File.join(smartdown_flow_path(flow.name), "outcomes", "#{node_name}.txt")
_, *node_aspects = node_name.split('_')
title = "\# #{node_name.humanize}"
node_content = ([title] + node_aspects.map { |aspect|
["\#\# #{aspect.humanize}",
outcome_aspect_map[aspect.to_sym],
"%{snippet_#{aspect}}"]
}.flatten.compact).join("\n\n")
File.write(node_filepath, node_content)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment