Skip to content

Commit c279ea9

Browse files
authored
Merge pull request #69 from Invoca/BARN-63_find_tokens_alternative_parameter_start_end
Barn 63 find tokens alternative parameter start end
2 parents eea5944 + 101e474 commit c279ea9

File tree

5 files changed

+68
-10
lines changed

5 files changed

+68
-10
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
44

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

7+
## [2.1.0] - 2025-11-06
8+
### Added
9+
- Added optional `parameter_start` and `parameter_end` arguments for the following methods:
10+
- `ParameterSubstitution.find_tokens`
11+
- `ParameterSubstitution.find_formatters`
12+
- `ParameterSubstitution.find_warnings`
13+
714
## [2.0.0] - 2024-07-11
815
### Removed
916
- Remove support for eol ruby versions 2.7

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
parameter_substitution (2.0.0)
4+
parameter_substitution (2.1.0)
55
activesupport (>= 6.0)
66
builder (~> 3.2)
77
invoca-utils (~> 0.3)

lib/parameter_substitution.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,27 @@ def configure
6363
ParameterSubstitution.config = config
6464
end
6565

66-
def find_tokens(string_with_tokens, mapping: {})
67-
parse_expression(context_from_string(string_with_tokens, mapping)).substitution_parameter_names
66+
def find_tokens(string_with_tokens, mapping: {}, parameter_start: "<", parameter_end: ">")
67+
parse_expression(context_from_string(string_with_tokens, mapping, parameter_start: parameter_start, parameter_end: parameter_end)).substitution_parameter_names
6868
end
6969

70-
def find_formatters(string_with_tokens, mapping: {})
71-
parse_expression(context_from_string(string_with_tokens, mapping)).method_names
70+
def find_formatters(string_with_tokens, mapping: {}, parameter_start: "<", parameter_end: ">")
71+
parse_expression(context_from_string(string_with_tokens, mapping, parameter_start: parameter_start, parameter_end: parameter_end)).method_names
7272
end
7373

74-
def find_warnings(string_with_tokens, mapping: {})
75-
parse_expression(context_from_string(string_with_tokens, mapping)).parameter_and_method_warnings || []
74+
def find_warnings(string_with_tokens, mapping: {}, parameter_start: "<", parameter_end: ">")
75+
parse_expression(context_from_string(string_with_tokens, mapping, parameter_start: parameter_start, parameter_end: parameter_end)).parameter_and_method_warnings || []
7676
end
7777

7878
private
7979

80-
def context_from_string(string_with_tokens, mapping)
81-
ParameterSubstitution::Context.new(input: string_with_tokens, mapping: mapping)
80+
def context_from_string(string_with_tokens, mapping, parameter_start: "<", parameter_end: ">")
81+
ParameterSubstitution::Context.new(
82+
input: string_with_tokens,
83+
mapping:,
84+
parameter_start:,
85+
parameter_end:
86+
)
8287
end
8388

8489
def parse_expression(context)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
class ParameterSubstitution
4-
VERSION = "2.0.0"
4+
VERSION = "2.1.0"
55
end

spec/lib/parameter_substitution_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ def self.find(_key); end
8181
it "returns tokens that exist in mapping when one is provided" do
8282
expect(ParameterSubstitution.find_tokens(expression, mapping: mapping)).to eq(['call.start_time', 'do_a_barrel_roll'])
8383
end
84+
85+
context 'with non-default delimiters' do
86+
let(:square_expression) { "[call.start_time.blank_if_nil][do_a_barrel_roll.downcase]" }
87+
88+
it "returns tokens up to first dot when no mapping is provided" do
89+
expect(ParameterSubstitution.find_tokens(square_expression, parameter_start: "[", parameter_end: "]")).to eq(['call', 'do_a_barrel_roll'])
90+
end
91+
92+
it "returns tokens that exist in mapping when one is provided" do
93+
expect(ParameterSubstitution.find_tokens(square_expression, mapping: mapping, parameter_start: "[", parameter_end: "]")).to eq(['call.start_time', 'do_a_barrel_roll'])
94+
end
95+
end
8496
end
8597

8698
context '#find_formatters' do
@@ -91,6 +103,18 @@ def self.find(_key); end
91103
it "returns formatters after all tokens when mapping is provided" do
92104
expect(ParameterSubstitution.find_formatters(expression, mapping: mapping)).to eq(['blank_if_nil', 'downcase'])
93105
end
106+
107+
context 'with non-default delimiters' do
108+
let(:square_expression) { "[call.start_time.blank_if_nil][do_a_barrel_roll.downcase]" }
109+
110+
it "returns all formatters after first dot when no mapping is provided" do
111+
expect(ParameterSubstitution.find_formatters(square_expression, parameter_start: "[", parameter_end: "]")).to eq(['start_time', 'blank_if_nil', 'downcase'])
112+
end
113+
114+
it "returns formatters after all tokens when mapping is provided" do
115+
expect(ParameterSubstitution.find_formatters(square_expression, mapping: mapping, parameter_start: "[", parameter_end: "]")).to eq(['blank_if_nil', 'downcase'])
116+
end
117+
end
94118
end
95119

96120
context '#find_warnings' do
@@ -127,6 +151,28 @@ def self.find(_key); end
127151
.to eq(["Unknown param 'bobby' and methods 'test1', 'test2'", "Unknown param 'bobby2' and methods 'test3', 'test4'"])
128152
end
129153
end
154+
155+
context "with non-default delimiters" do
156+
let(:square_expression_with_valid_params) { "[foo]" }
157+
let(:square_expression_with_bad_params) { "[bobby][bobby2]" }
158+
let(:square_expression_with_bad_methods) { "[foo.test1.test2][foo.test3.test4][black.test1.test2]" }
159+
160+
it "returns empty array for valid parameters" do
161+
expect(ParameterSubstitution.find_warnings(square_expression_with_valid_params, mapping: default_mapping, parameter_start: "[", parameter_end: "]"))
162+
.to eq([])
163+
end
164+
165+
it "returns warnings for invalid parameters" do
166+
expect(ParameterSubstitution.find_warnings(square_expression_with_bad_params, parameter_start: "[", parameter_end: "]"))
167+
.to eq(["Unknown param 'bobby'", "Unknown param 'bobby2'"])
168+
end
169+
170+
it "returns warnings for invalid methods" do
171+
expect(ParameterSubstitution.find_warnings(square_expression_with_bad_methods, mapping: default_mapping, parameter_start: "[", parameter_end: "]"))
172+
.to eq(["Unknown methods 'test1', 'test2', 'test3', 'test4' used on parameter 'foo'",
173+
"Unknown methods 'test1', 'test2' used on parameter 'black'"])
174+
end
175+
end
130176
end
131177
end
132178

0 commit comments

Comments
 (0)