Skip to content

Commit

Permalink
Add rendering to controller
Browse files Browse the repository at this point in the history
  • Loading branch information
apexatoll committed Sep 25, 2023
1 parent 9480e81 commit 50929bd
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/kangaru/controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
module Kangaru
class Controller
attr_reader :command
attr_reader :command, :renderer

def initialize(command)
@command = command
@renderer = Renderer.new(command)
end

def execute
public_send(command.action)

renderer.render(binding)
end
end
end
1 change: 1 addition & 0 deletions sig/kangaru/controller.rbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Kangaru
class Controller
attr_reader command: Command
attr_reader renderer: Renderer

def initialize: (Command) -> void

Expand Down
51 changes: 49 additions & 2 deletions spec/features/command_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
RSpec.describe "Running a command", :with_gem do
subject(:run_command!) { SomeGem.run!(argv) }

let(:argv) { [] }

include_context :kangaru_initialised

describe "calling SomeGem.run! directly" do
subject(:run_command!) { SomeGem.run!(argv) }

let(:target) do
Module.new { def self.do_something = nil }
end
Expand Down Expand Up @@ -266,4 +268,49 @@ def some_action
end
end
end

describe "rendering view files" do
before do
gem.gem_file("default_controller").write(<<~RUBY)
module SomeGem
class DefaultController < Kangaru::Controller
def default
@name = "Some Name"
@age = 30
end
end
end
RUBY
end

context "when view file does not exist" do
before { gem.load! }

it "does not output to stdout" do
expect { run_command! }.not_to output.to_stdout
end
end

context "when view file exists" do
before do
gem.gem_dir("views").mkdir
gem.gem_dir("views/default").mkdir
gem.view_file(controller: "default", action: "default").write(view_file)

gem.load!
end

let(:view_file) do
<<~ERB
Hello <%= @name %>, you are <%= @age %> years old.
ERB
end

it "outputs the interpolated view file" do
expect { run_command! }.to output(<<~TEXT).to_stdout
Hello Some Name, you are 30 years old.
TEXT
end
end
end
end
20 changes: 20 additions & 0 deletions spec/kangaru/controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,25 @@

let(:action) { :some_action }

let(:renderer) { instance_spy(Kangaru::Renderer) }

before do
allow(Kangaru::Renderer).to receive(:new).and_return(renderer)
end

describe "#initialize" do
it "sets the command" do
expect(controller.command).to eq(command)
end

it "instantiates a renderer" do
controller
expect(Kangaru::Renderer).to have_received(:new).with(command).once
end

it "sets the renderer" do
expect(controller.renderer).to eq(renderer)
end
end

describe "#execute" do
Expand All @@ -30,6 +45,11 @@
execute
expect(controller).to have_received(action).once
end

it "renders the command output" do
execute
expect(renderer).to have_received(:render).with(a_kind_of(Binding)).once
end
end
end

Expand Down

0 comments on commit 50929bd

Please sign in to comment.