Skip to content

Commit 2a5029e

Browse files
committed
Allow override '/text/' to construct a regexp
1 parent 5379ab6 commit 2a5029e

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

lib/octocatalog-diff/api/v1/override.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class Override
1313
# Constructor: Accepts a key and value.
1414
# @param input [Hash] Must contain :key and :value
1515
def initialize(input)
16-
@key = input.fetch(:key)
16+
key = input.fetch(:key)
17+
@key = key =~ %r{\A/(.+)/\Z} ? Regexp.new(Regexp.last_match(1)) : key
1718
@value = parsed_value(input.fetch(:value))
1819
end
1920

@@ -29,6 +30,7 @@ def self.create_from_input(input, key = nil)
2930
# If input is not a string, we can still construct the object if the key is given.
3031
# That input would come directly from code and not from the command line, since inputs
3132
# from the command line are always strings.
33+
# Also support regular expressions for the key name, if delimited by //.
3234
if key.nil? && input.is_a?(String)
3335
unless input.include?('=')
3436
raise ArgumentError, "Fact override '#{input}' is not in 'key=(data type)value' format"

spec/octocatalog-diff/tests/api/v1/override_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@
66

77
describe OctocatalogDiff::API::V1::Override do
88
describe '#new' do
9-
it 'should raise error if fact_name is not supplied' do
9+
it 'should raise error if fact name is not supplied' do
1010
expect { described_class.new(value: 'bar') }.to raise_error(KeyError, /key/)
1111
end
1212

1313
it 'should raise error if value is not supplied' do
1414
expect { described_class.new(key: 'bar') }.to raise_error(KeyError, /value/)
1515
end
16+
17+
it 'should return a regexp if fact name is a regexp' do
18+
testobj = described_class.new(key: '/foo/', value: 'bar')
19+
expect(testobj.key).to eq(/foo/)
20+
expect(testobj.value).to eq('bar')
21+
end
1622
end
1723

1824
describe '#key' do

0 commit comments

Comments
 (0)