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

Convert param input to String #105

Merged
merged 2 commits into from
Jun 20, 2024
Merged
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
23 changes: 21 additions & 2 deletions lib/stackup/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def to_a
if value == :use_previous_value
record[:use_previous_value] = true
else
record[:parameter_value] = value
record[:parameter_value] = ParameterValue.new(value).to_param
end
end
end
Expand Down Expand Up @@ -75,7 +75,26 @@ def value
if use_previous_value
:use_previous_value
else
parameter_value
ParameterValue.new(parameter_value).to_param
end
end

end

class ParameterValue

def initialize(value)
@value = value
end

def to_param
case @value
when TrueClass, FalseClass, Integer then
@value.to_s
when Array
@value.map { |v| ParameterValue.new(v).to_param }.join(",")
else
@value
Comment on lines +84 to +97
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why this is a class. Couldn't it just be a plain function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why this is a class. Couldn't it just be a plain function?

Was following the convention used elsewhere in the same file, e.g. ParameterStruct

end
end

Expand Down
45 changes: 45 additions & 0 deletions spec/stackup/parameters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,51 @@

end

context "with non-string values" do

let(:input_records) do
[
{ :parameter_key => "Integer", :parameter_value => 123_456_789 },
{ :parameter_key => "Truthy", :parameter_value => true },
{ :parameter_key => "Falsey", :parameter_value => false },
{ :parameter_key => "Null", :parameter_value => nil },
{ :parameter_key => "Array", :parameter_value => ["one", 2] },
{ :parameter_key => "String", :parameter_value => "string" }
]
end

subject(:parameters) { Stackup::Parameters.new(input_records) }

describe "#to_hash" do
it "converts the to strings where appropriate" do
expected = {
"Integer" => "123456789",
"Truthy" => "true",
"Falsey" => "false",
"Null" => nil,
"Array" => "one,2",
"String" => "string"
}
expect(parameters.to_hash).to eql(expected)
end
end

describe "#to_a" do
it "converts the to strings where appropriate" do
expected = [
{ :parameter_key => "Integer", :parameter_value => "123456789" },
{ :parameter_key => "Truthy", :parameter_value => "true" },
{ :parameter_key => "Falsey", :parameter_value => "false" },
{ :parameter_key => "Null", :parameter_value => nil },
{ :parameter_key => "Array", :parameter_value => "one,2" },
{ :parameter_key => "String", :parameter_value => "string" }
]
expect(parameters.to_a).to eql(expected)
end
end

end

context "with empty parameter file" do

let(:input_records) { false }
Expand Down
Loading