Skip to content

Instantly share code, notes, and snippets.

@jmmastey
Last active August 29, 2015 13:56
Show Gist options
  • Save jmmastey/9084521 to your computer and use it in GitHub Desktop.
Save jmmastey/9084521 to your computer and use it in GitHub Desktop.
def to_json(data = nil)
format_testfile(data ||= @data)
end
private
# block definitions
def format_testfile(data)
format_chunk(nil, data, &method(:format_loan_type))
end
def format_loan_type(type, io)
format_chunk(type, io, &method(:format_scenarios))
end
def format_scenarios(type, scenarios)
format_chunk(type, scenarios, &method(:format_scenario))
end
def format_scenario(scenario, tasks)
format_chunk(scenario, tasks, '[', ']', &method(:format_task))
end
def format_task(*task)
task = Hash[task] # this makes me squicky.
task.include?(:account) ? format_output_task(task) : format_input_task(task)
end
#### format_chunk def
def format_chunk(label, enum, open_bracket = '{', close_bracket = '}', &iterator)
output = ""
output << (newline + "\"#{label}\": ") unless label.nil?
output << open_bracket
aggregated_out = with_indent do
enum.map { |args| iterator.call(*args) }
end
output << aggregated_out.join(",") + newline
output << close_bracket
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment