Skip to content

Commit 278c40b

Browse files
committed
BARN-77: allow context parameter overrides in ParameterSubstitution helper methods
1 parent c279ea9 commit 278c40b

File tree

4 files changed

+38
-23
lines changed

4 files changed

+38
-23
lines changed

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.1.0)
4+
parameter_substitution (3.0.0.pre.0)
55
activesupport (>= 6.0)
66
builder (~> 3.2)
77
invoca-utils (~> 0.3)

lib/parameter_substitution.rb

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

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
66+
def find_tokens(string_with_tokens, mapping: {}, context_overrides: nil)
67+
context = build_context(string_with_tokens, mapping, context_overrides)
68+
parse_expression(context).substitution_parameter_names
6869
end
6970

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
71+
def find_formatters(string_with_tokens, mapping: {}, context_overrides: nil)
72+
context = build_context(string_with_tokens, mapping, context_overrides)
73+
parse_expression(context).method_names
7274
end
7375

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 || []
76+
def find_warnings(string_with_tokens, mapping: {}, context_overrides: nil)
77+
context = build_context(string_with_tokens, mapping, context_overrides)
78+
parse_expression(context).parameter_and_method_warnings || []
7679
end
7780

7881
private
7982

80-
def context_from_string(string_with_tokens, mapping, parameter_start: "<", parameter_end: ">")
81-
ParameterSubstitution::Context.new(
83+
# Build context with optional overrides
84+
# @param [String] string_with_tokens The input string containing tokens
85+
# @param [Hash] mapping The mapping of parameters to values
86+
# @param [Hash, nil] context_overrides Optional overrides for context attributes
87+
# @return [ParameterSubstitution::Context] The constructed context
88+
def build_context(string_with_tokens, mapping, context_overrides)
89+
base_options = {
8290
input: string_with_tokens,
83-
mapping:,
84-
parameter_start:,
85-
parameter_end:
86-
)
91+
mapping: mapping
92+
}
93+
94+
ParameterSubstitution::Context.new(**base_options.merge(context_overrides || {}))
8795
end
8896

8997
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.1.0"
4+
VERSION = "3.0.0-0"
55
end

spec/lib/parameter_substitution_spec.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,13 @@ def self.find(_key); end
8686
let(:square_expression) { "[call.start_time.blank_if_nil][do_a_barrel_roll.downcase]" }
8787

8888
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'])
89+
context_overrides = { parameter_start: "[", parameter_end: "]" }
90+
expect(ParameterSubstitution.find_tokens(square_expression, context_overrides: context_overrides)).to eq(['call', 'do_a_barrel_roll'])
9091
end
9192

9293
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+
context_overrides = { parameter_start: "[", parameter_end: "]" }
95+
expect(ParameterSubstitution.find_tokens(square_expression, mapping: mapping, context_overrides: context_overrides)).to eq(['call.start_time', 'do_a_barrel_roll'])
9496
end
9597
end
9698
end
@@ -108,18 +110,20 @@ def self.find(_key); end
108110
let(:square_expression) { "[call.start_time.blank_if_nil][do_a_barrel_roll.downcase]" }
109111

110112
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'])
113+
context_overrides = { parameter_start: "[", parameter_end: "]" }
114+
expect(ParameterSubstitution.find_formatters(square_expression, context_overrides: context_overrides)).to eq(['start_time', 'blank_if_nil', 'downcase'])
112115
end
113116

114117
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'])
118+
context_overrides = { parameter_start: "[", parameter_end: "]" }
119+
expect(ParameterSubstitution.find_formatters(square_expression, mapping: mapping, context_overrides: context_overrides)).to eq(['blank_if_nil', 'downcase'])
116120
end
117121
end
118122
end
119123

120124
context '#find_warnings' do
121125
let(:expression_with_valid_params) { "<foo>" }
122-
let(:expression_with_bad_paramss) { "<bobby><bobby2>" }
126+
let(:expression_with_bad_params) { "<bobby><bobby2>" }
123127
let(:expression_with_bad_methods) { "<foo.test1.test2><foo.test3.test4><black.test1.test2>" }
124128
let(:expression_with_bad_params_and_methods) { "<bobby.test1.test2><bobby2.test3.test4>" }
125129
let(:expression_with_mixed_bad_params_and_methods) { "<bobby.test1.test2><foo.test3.test4>" }
@@ -133,7 +137,7 @@ def self.find(_key); end
133137

134138
context "when there are invalid parameters" do
135139
it "returns 2 warnings" do
136-
expect(ParameterSubstitution.find_warnings(expression_with_bad_paramss)).to eq(["Unknown param 'bobby'", "Unknown param 'bobby2'"])
140+
expect(ParameterSubstitution.find_warnings(expression_with_bad_params)).to eq(["Unknown param 'bobby'", "Unknown param 'bobby2'"])
137141
end
138142
end
139143

@@ -158,17 +162,20 @@ def self.find(_key); end
158162
let(:square_expression_with_bad_methods) { "[foo.test1.test2][foo.test3.test4][black.test1.test2]" }
159163

160164
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: "]"))
165+
context_overrides = { parameter_start: "[", parameter_end: "]" }
166+
expect(ParameterSubstitution.find_warnings(square_expression_with_valid_params, mapping: default_mapping, context_overrides: context_overrides))
162167
.to eq([])
163168
end
164169

165170
it "returns warnings for invalid parameters" do
166-
expect(ParameterSubstitution.find_warnings(square_expression_with_bad_params, parameter_start: "[", parameter_end: "]"))
171+
context_overrides = { parameter_start: "[", parameter_end: "]" }
172+
expect(ParameterSubstitution.find_warnings(square_expression_with_bad_params, context_overrides: context_overrides))
167173
.to eq(["Unknown param 'bobby'", "Unknown param 'bobby2'"])
168174
end
169175

170176
it "returns warnings for invalid methods" do
171-
expect(ParameterSubstitution.find_warnings(square_expression_with_bad_methods, mapping: default_mapping, parameter_start: "[", parameter_end: "]"))
177+
context_overrides = { parameter_start: "[", parameter_end: "]" }
178+
expect(ParameterSubstitution.find_warnings(square_expression_with_bad_methods, mapping: default_mapping, context_overrides: context_overrides))
172179
.to eq(["Unknown methods 'test1', 'test2', 'test3', 'test4' used on parameter 'foo'",
173180
"Unknown methods 'test1', 'test2' used on parameter 'black'"])
174181
end

0 commit comments

Comments
 (0)