module Trailblazer::Developer::Trace::Focusable
Public Instance Methods
capture_variables_from(ctx, focus_on:, inspector: Trace::Inspector, **flow_options)
click to toggle source
Get inspect of {focus_on.variables} or current {ctx}
# File lib/trailblazer/developer/trace/focusable.rb, line 11 def capture_variables_from(ctx, focus_on:, inspector: Trace::Inspector, **flow_options) # ctx keys to be captured, for example [:current_user, :model, ....] variables = (selected = focus_on[:variables]).any? ? selected : ctx.keys variables.each_with_object({}) do |variable, result| if variable.is_a?(Proc) # To allow deep key access from ctx result[:Custom] = inspector.(variable.call(ctx), **flow_options) else result[variable] = inspector.(ctx[variable], **flow_options) end end end
patched_vertical_table()
click to toggle source
Overrding `Hirb::Helpers::VerticalTable#render_rows` because there is no option to customize vertical table's row header :( We need it to print if given entity is Input/Output
@private
# File lib/trailblazer/developer/trace/focusable.rb, line 62 def patched_vertical_table table = Class.new(Hirb::Helpers::VerticalTable) table.send(:define_method, :render_rows) do longest_header = Hirb::String.size (@headers.values.sort_by {|e| Hirb::String.size(e) }.last || '') stars = "*" * [(longest_header + (longest_header / 2)), 3].max @rows.map do |row| "#{stars} #{@options[:table_header]} #{stars}\n" + @fields.map{ |f| "#{Hirb::String.rjust(@headers[f], longest_header)}: #{row[f]}" }.join("\n") end end table end
tree_nodes_for(level, input:, output:, **options)
click to toggle source
Generate
Hirb's vertical table nodes from captured ctx of each step |– some step name | |– ********** Input **********
message: "WTF!" seq: []
| `– ********** Output **********
message: "WTF!" seq: [:a]
# File lib/trailblazer/developer/trace/focusable.rb, line 32 def tree_nodes_for(level, input:, output:, **options) # TODO: Reverting `Hash#compact` usage as it is not supported in Ruby <= 2.4 # Once the support is droped, revert actual code with below and remove entity check. # input_output_nodes = { Input: input, Output: output }.compact.collect do |table_header, entity| input_output_nodes = { Input: input, Output: output }.collect do |table_header, entity| next unless entity next unless Array( entity.data[:focused_variables] ).any? table = vertical_table_for(entity.data[:focused_variables], table_header: table_header) Present::TreeNodes::Node.new(level + 1, table, input, output, options).freeze end input_output_nodes.compact end
vertical_table_for(focused_variables, table_header:)
click to toggle source
@private
# File lib/trailblazer/developer/trace/focusable.rb, line 49 def vertical_table_for(focused_variables, table_header:) patched_vertical_table.render( Array[ focused_variables ], description: nil, table_header: table_header, # Custom option, not from Hirb ) end