Skip to content

Commit

Permalink
Implement input wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
apexatoll committed Sep 18, 2023
1 parent df4cd3f commit a5e5f98
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/kangaru/input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Kangaru
class Input
attr_reader :controller, :action, :arguments

def initialize(controller:, action:, arguments:)
@controller = controller
@action = action
@arguments = arguments
end

def self.from_argv(tokens)
controller = InputParsers::ControllerParser.parse(tokens)
action = InputParsers::ActionParser.parse(tokens)
arguments = InputParsers::ArgumentParser.parse(tokens)

new(controller:, action:, arguments:)
end
end
end
15 changes: 15 additions & 0 deletions sig/kangaru/input.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Kangaru
class Input
attr_reader controller: String
attr_reader action: Symbol
attr_reader arguments: Hash[Symbol, untyped]

def initialize: (
controller: String,
action: Symbol,
arguments: Hash[Symbol, untyped]
) -> void

def self.from_argv: (Array[String]) -> Input
end
end
46 changes: 46 additions & 0 deletions spec/kangaru/input_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
RSpec.describe Kangaru::Input do
subject(:input) { described_class.new(**attributes) }

let(:attributes) { { controller:, action:, arguments: } }

let(:controller) { "FoobarController" }
let(:action) { :do_something }
let(:arguments) { { force: true } }

describe "#initialize" do
it "sets the attributes" do
expect(input).to have_attributes(**attributes)
end
end

describe ".from_argv" do
subject(:input) { described_class.from_argv(tokens) }

let(:tokens) { %w[foo bar baz] }

before do
allow(Kangaru::InputParsers::ControllerParser)
.to receive(:parse)
.with(tokens)
.and_return(controller)

allow(Kangaru::InputParsers::ActionParser)
.to receive(:parse)
.with(tokens)
.and_return(action)

allow(Kangaru::InputParsers::ArgumentParser)
.to receive(:parse)
.with(tokens)
.and_return(arguments)
end

it "returns an input object" do
expect(input).to be_a(described_class)
end

it "sets the expected attributes" do
expect(input).to have_attributes(**attributes)
end
end
end

0 comments on commit a5e5f98

Please sign in to comment.