Skip to content

Commit

Permalink
Create renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
apexatoll committed Sep 25, 2023
1 parent d6ec7b8 commit 9480e81
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/kangaru/renderer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Kangaru
class Renderer
attr_reader :command

def initialize(command)
@command = command
end

def render(binding)
return unless view_file.exist?

puts ERB.new(view_file.read).result(binding)
end

private

def view_file
Kangaru.application.view_file(
controller: command.controller,
action: command.action.to_s
)
end
end
end
13 changes: 13 additions & 0 deletions sig/kangaru/renderer.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Kangaru
class Renderer
attr_reader command: Command

def initialize: (Command) -> void

def render: (Binding) -> void

private

def view_file: -> Pathname
end
end
68 changes: 68 additions & 0 deletions spec/kangaru/renderer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
RSpec.describe Kangaru::Renderer do
subject(:renderer) { described_class.new(command) }

let(:command) { instance_spy(Kangaru::Command) }
let(:application) { instance_spy(Kangaru::Application, view_file:) }

let(:view_file) do
instance_spy(Pathname, read: view_file_contents, exist?: view_file_exists?)
end

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

before do
allow(Kangaru).to receive(:application).and_return(application)
end

describe "#render" do
subject(:render) { renderer.render(binding) }

context "when view file does not exist" do
let(:view_file_exists?) { false }

it "does not output any text" do
expect { render }.not_to output.to_stdout
end
end

context "when view file exists" do
let(:view_file_exists?) { true }

context "when instance variables do not exist in binding" do
let(:expected_output) do
<<~STRING
Your name is , you are years old.
STRING
end

it "outputs the view file contents with nil interpolated" do
expect { render }.to output(expected_output).to_stdout
end
end

context "when instance variables exist in binding" do
around do |spec|
instance_variable_set(:@name, "Foo Bar")
instance_variable_set(:@age, 30)
spec.run
remove_instance_variable(:@name)
remove_instance_variable(:@age)
end

let(:expected_output) do
<<~STRING
Your name is Foo Bar, you are 30 years old.
STRING
end

it "outputs the interpolated view file contents" do
expect { render }.to output(expected_output).to_stdout
end
end
end
end
end

0 comments on commit 9480e81

Please sign in to comment.