Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

Note: This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.3.0] - 2026-02-24
### Added
- Added `dollars_to_cents` formatter, which converts a dollar amount (provided as a string or numeric value) to an integer representing the equivalent amount in cents. For example, "12.34" would be converted to 1234.

## [3.2.0] - 2026-02-19
### Added
- Added `find_all_tokens` method that recursively collects all substitution parameter names, including those nested inside method call arguments.
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
parameter_substitution (3.2.0)
parameter_substitution (3.3.0)
activesupport (>= 6.0)
builder (~> 3.2)
invoca-utils (~> 0.3)
Expand All @@ -22,7 +22,7 @@
tzinfo (~> 2.0)
appraisal (2.5.0)
bundler
rake

Check failure on line 25 in Gemfile.lock

View check run for this annotation

Security Scanner as a Service / Bundle Audit

Gemfile.lock#L25

thor Warning Message: https://github.com/advisories/GHSA-mqcp-p2hv-vw6x CVE: CVE-2025-54314 Severity: low
thor (>= 0.14.0)
appraisal-matrix (0.2.0)
appraisal (~> 2.2)
Expand Down
11 changes: 11 additions & 0 deletions lib/parameter_substitution/formatters/dollars_to_cents.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class ParameterSubstitution::Formatters::DollarsToCents < ParameterSubstitution::Formatters::Base
def self.description
"Converts a dollar amount to cents as an integer"
end

def self.format(value)
(Float(value) * 100).round.to_i rescue nil
end
end
2 changes: 1 addition & 1 deletion lib/parameter_substitution/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

class ParameterSubstitution
VERSION = "3.2.0"
VERSION = "3.3.0"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

require_relative '../../../../lib/parameter_substitution/formatters/dollars_to_cents'

describe ParameterSubstitution::Formatters::DollarsToCents do
context "DollarsToCents formatter test" do
before do
@format_class = ParameterSubstitution::Formatters::DollarsToCents
end

it "have a key" do
expect(@format_class.key).to eq("dollars_to_cents")
end

it "provide a description" do
expect(@format_class.description).to eq("Converts a dollar amount to cents as an integer")
end

it "converts valid string dollar amounts to cents" do
expect(@format_class.format("10.50")).to eq(1050)
expect(@format_class.format("10")).to eq(1000)
expect(@format_class.format("0.01")).to eq(1)
expect(@format_class.format("123.456")).to eq(12346)
expect(@format_class.format("0.001")).to eq(0)
expect(@format_class.format("100")).to eq(10000)
expect(@format_class.format("99.99")).to eq(9999)
expect(@format_class.format("-10.50")).to eq(-1050)
end

it "converts valid numeric dollar amounts to cents" do
expect(@format_class.format(10.50)).to eq(1050)
expect(@format_class.format(10)).to eq(1000)
expect(@format_class.format(0.01)).to eq(1)
expect(@format_class.format(123.456)).to eq(12346)
expect(@format_class.format(0.001)).to eq(0)
expect(@format_class.format(100)).to eq(10000)
expect(@format_class.format(99.99)).to eq(9999)
expect(@format_class.format(-10.50)).to eq(-1050)
end
Comment on lines +30 to +39
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

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

Consider adding test cases for edge values that test rounding at exactly 0.5 cents, such as "0.005" (should round to 1) and "0.015" (should round to 2), to explicitly verify and document the rounding behavior when the fractional cent is exactly at the midpoint.

Copilot uses AI. Check for mistakes.

it "returns nil for nil or invalid strings" do
expect(@format_class.format(nil)).to eq(nil)
expect(@format_class.format("")).to eq(nil)
expect(@format_class.format("$10.50")).to eq(nil)
expect(@format_class.format("not_a_number")).to eq(nil)
end
end
end
Loading