@@ -54,41 +54,48 @@ def initialize(operator:, value:, property: nil)
5454 @property = property
5555 end
5656
57- def match_trait_value? ( trait_value ) # rubocop:disable Metrics/MethodLength
58- if @value . is_a? ( String ) && @value . match? ( /:semver$/ )
59- begin
60- trait_value = Semantic ::Version . new ( trait_value . to_s . gsub ( /:semver$/ , '' ) )
61- rescue ArgumentError , Semantic ::Version ::ValidationFailed => _e
62- return false
63- end
64- end
57+ def match_trait_value? ( trait_value )
58+ trait_value = parse_semver_trait_value ( trait_value )
59+ return false if trait_value . nil?
6560
6661 return match_in_value ( trait_value ) if @operator == IN
6762 return match_modulo_value ( trait_value ) if @operator == MODULO
6863 return MATCHING_FUNCTIONS [ REGEX ] &.call ( trait_value , @value ) if @operator == REGEX
6964
65+ match_with_type_conversion ( trait_value )
66+ end
67+
68+ def parse_semver_trait_value ( trait_value )
69+ return trait_value unless @value . is_a? ( String ) && @value . match? ( /:semver$/ )
70+
71+ Semantic ::Version . new ( trait_value . to_s . gsub ( /:semver$/ , '' ) )
72+ rescue ArgumentError , Semantic ::Version ::ValidationFailed
73+ nil
74+ end
75+
76+ def match_with_type_conversion ( trait_value )
7077 type_as_trait_value = format_to_type_of ( trait_value )
7178 formatted_value = type_as_trait_value ? type_as_trait_value . call ( @value ) : @value
72-
7379 MATCHING_FUNCTIONS [ operator ] &.call ( trait_value , formatted_value )
7480 end
7581
76- def format_to_type_of ( input ) # rubocop:disable Metrics/AbcSize
77- {
78- 'String' => -> ( v ) { v . to_s } ,
79- 'Semantic::Version' => -> ( v ) { Semantic ::Version . new ( v . to_s . gsub ( /:semver$/ , '' ) ) } ,
80- # Double check this is the desired behavior between SDKs
81- 'TrueClass' => -> ( v ) { [ 'True' , 'true' , 'TRUE' , true , 1 , '1' ] . include? ( v ) } ,
82- 'FalseClass' => -> ( v ) { ![ 'False' , 'false' , 'FALSE' , false ] . include? ( v ) } ,
83- 'Integer' => lambda { |v |
84- i = v . to_i
85- i . to_s == v . to_s ? i : v
86- } ,
87- 'Float' => lambda { |v |
88- f = v . to_f
89- f . to_s == v . to_s ? f : v
90- }
91- } [ input . class . to_s ]
82+ TYPE_CONVERTERS = {
83+ 'String' => -> ( v ) { v . to_s } ,
84+ 'Semantic::Version' => -> ( v ) { Semantic ::Version . new ( v . to_s . gsub ( /:semver$/ , '' ) ) } ,
85+ 'TrueClass' => -> ( v ) { [ 'True' , 'true' , 'TRUE' , true , 1 , '1' ] . include? ( v ) } ,
86+ 'FalseClass' => -> ( v ) { ![ 'False' , 'false' , 'FALSE' , false ] . include? ( v ) } ,
87+ 'Integer' => lambda { |v |
88+ i = v . to_i
89+ i . to_s == v . to_s ? i : v
90+ } ,
91+ 'Float' => lambda { |v |
92+ f = v . to_f
93+ f . to_s == v . to_s ? f : v
94+ }
95+ } . freeze
96+
97+ def format_to_type_of ( input )
98+ TYPE_CONVERTERS [ input . class . to_s ]
9299 end
93100
94101 def match_modulo_value ( trait_value )
@@ -98,18 +105,25 @@ def match_modulo_value(trait_value)
98105 false
99106 end
100107
101- def match_in_value ( trait_value ) # rubocop:disable Metrics/AbcSize
102- return false if trait_value . nil? || trait_value . is_a? ( TrueClass ) || trait_value . is_a? ( FalseClass ) || ( [ true , false ] . include? trait_value )
103-
108+ def match_in_value ( trait_value )
109+ return false if invalid_in_value? ( trait_value )
104110 return @value . include? ( trait_value . to_s ) if @value . is_a? ( Array )
105111
106- if @value . is_a? ( String )
107- begin
108- parsed = JSON . parse ( @value )
109- return parsed . include? ( trait_value . to_s ) if parsed . is_a? ( Array )
110- rescue JSON ::ParserError
111- end
112- end
112+ parse_and_match_string_value ( trait_value )
113+ end
114+
115+ def invalid_in_value? ( trait_value )
116+ trait_value . nil? || [ TrueClass , FalseClass ] . any? { |klass | trait_value . is_a? ( klass ) }
117+ end
118+
119+ def parse_and_match_string_value ( trait_value ) # rubocop:disable Metrics/AbcSize
120+ return @value . to_s . split ( ',' ) . include? ( trait_value . to_s ) unless @value . is_a? ( String )
121+
122+ parsed = JSON . parse ( @value )
123+ return parsed . include? ( trait_value . to_s ) if parsed . is_a? ( Array )
124+
125+ @value . to_s . split ( ',' ) . include? ( trait_value . to_s )
126+ rescue JSON ::ParserError
113127 @value . to_s . split ( ',' ) . include? ( trait_value . to_s )
114128 end
115129
0 commit comments