Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render ctx table #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/trailblazer/developer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ module Developer
require "trailblazer/developer/trace/present"
require "trailblazer/developer/trace/focusable"
require "trailblazer/developer/trace/inspector"
require "trailblazer/developer/render"
require "trailblazer/developer/render/circuit"
require "trailblazer/developer/render/context"
require "trailblazer/developer/render/linear"
require "trailblazer/developer/render/task_wrap"
require "trailblazer/developer/introspect"
15 changes: 15 additions & 0 deletions lib/trailblazer/developer/render.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Trailblazer
module Developer
module_function

def render(value, *args, **options)
renderer_for(value).(value, *args, **options)
end

# private
def renderer_for(value)
return Render::Context if value.is_a?(Trailblazer::Context::Container)
Render::Circuit
end
end
end
6 changes: 0 additions & 6 deletions lib/trailblazer/developer/render/circuit.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
module Trailblazer
module Developer
module_function

def render(activity, **options)
Render::Circuit.(activity, **options)
end

module Render
module Circuit
module_function
Expand Down
40 changes: 40 additions & 0 deletions lib/trailblazer/developer/render/context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Trailblazer
module Developer
module Render
module Context
module_function

def call(ctx, *args)
wrapped_options, mutable_options = ctx.decompose

render_table(wrapped_options, title: "ctx")
render_table(mutable_options, title: "ctx mutations")

args.each do |arg|
render_table(ctx[arg], title: arg)
end
end

# private
def render_table(data, title: nil)
puts "\n#{"*" * 10} #{title} #{"*" * 10}" if title

unless data.any?
puts Hirb::Helpers::UnicodeTable.render(
[{ "key" => "", "value" => "" }],
description: false,
)
return
end

puts Hirb::Helpers::UnicodeTable.render(
Array(data),
headers: { 0 => "key", 1 => "value" },
filters: { 0 => :inspect, 1 => :inspect },
description: false,
)
end
end
end
end
end
File renamed without changes.
93 changes: 93 additions & 0 deletions test/render_context_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
require "test_helper"

class RenderContextTest < Minitest::Spec
User = Struct.new(:id)

it "renders table for ctx without any mutations" do
# ctx without any mutations
ctx = Trailblazer::Context({ current_user: User.new(1), params: { name: "John" } })
output, _ = capture_io do
Trailblazer::Developer::Render::Context.(ctx)
end

assert_equal output, %{
********** ctx **********
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ key β”‚ value β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ :current_user β•Ž #<struct RenderContextTest::User id=1> β”‚
β”‚ :params β•Ž {:name=>"John"} β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

********** ctx mutations **********
β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”
β”‚ key β”‚ value β”‚
β”œβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β•Ž β”‚
β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜
}

end

it "renders table for ctx with mutations" do
ctx = Trailblazer::Context({ current_user: User.new(1), params: { name: "John" } })
ctx[:say] = "something"
ctx[:params] = { id: 1 }

output, _ = capture_io do
Trailblazer::Developer::Render::Context.(ctx)
end

assert_equal output, %{
********** ctx **********
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ key β”‚ value β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ :current_user β•Ž #<struct RenderContextTest::User id=1> β”‚
β”‚ :params β•Ž {:name=>"John"} β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

********** ctx mutations **********
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ key β”‚ value β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ :say β•Ž "something" β”‚
β”‚ :params β•Ž {:id=>1} β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
}
end

it "renders table for ctx with zoom on `:params`" do
ctx = Trailblazer::Context({ current_user: User.new(1), params: { name: "John" } })
ctx[:params][:embed] = ["user", "post"]

output, _ = capture_io do
Trailblazer::Developer::Render::Context.(ctx, :params)
end

assert_equal output, %{
********** ctx **********
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ key β”‚ value β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ :current_user β•Ž #<struct RenderContextTest::User id=1> β”‚
β”‚ :params β•Ž {:name=>"John", :embed=>["user", "post"]} β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

********** ctx mutations **********
β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”
β”‚ key β”‚ value β”‚
β”œβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β•Ž β”‚
β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜

********** params **********
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ key β”‚ value β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ :name β•Ž "John" β”‚
β”‚ :embed β•Ž ["user", "post"] β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
}
end
end