growthbook 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cba2938ec675b1626d643fb7493223a54d0faf1ffc37c2bacb5a06637c0a75cd
4
- data.tar.gz: bce59918f346b53182450ec2fdd92776767954712334bc5b7c0bd48d952c05ce
3
+ metadata.gz: 71807a4bbc1fb91e816a74e32d7d6c9e7d1dbed782b19efd3592fd5f5a944801
4
+ data.tar.gz: 03fa6cf71822d7f239afda323e9a28476721d64c6234b199103869207a2cd060
5
5
  SHA512:
6
- metadata.gz: 0c4ac9f40b23c8719b9cb585d92f089144df5441c787321fd0083ae72ff954f5f8027615cc3d226dcdb7c2bfdc3dc5911ef1abd472904c026a3e24c1dd81230c
7
- data.tar.gz: 735bfc1c4b82432a4a9b0664282062f43ab644309933afcddb43c9dc53cd32a278d1cf14221ad9b298a0bbdb0e2a12a8e77c4aa814d8ba3719e7840c62a4d09a
6
+ metadata.gz: af78de253f4b3c2ecfbcdde348237923b3ecdd0dbe547be7d0f177bc7dff2a6299d6f6ac7786c4e4e96c2c031ff46598897ef79214efeaba837cce4db2e3739b
7
+ data.tar.gz: fd4b68b94b0448dd1cd88b8aec1b96c1d76755133917c7a915a03b18e6a02cd2b2bc5d9f6735c51742d4af5a8707cec13f61ed571fd013059d45b207688a83f8
@@ -104,20 +104,68 @@ module Growthbook
104
104
  false
105
105
  end
106
106
 
107
+ def self.compare(val1, val2)
108
+ if val1.is_a?(Numeric) || val2.is_a?(Numeric)
109
+ val1 = val1.is_a?(Numeric) ? val1 : val1.to_f
110
+ val2 = val2.is_a?(Numeric) ? val2 : val2.to_f
111
+ end
112
+
113
+ return 1 if val1 > val2
114
+ return -1 if val1 < val2
115
+
116
+ 0
117
+ end
118
+
107
119
  def self.eval_operator_condition(operator, attribute_value, condition_value)
108
120
  case operator
121
+ when '$veq'
122
+ padded_version_string(attribute_value) == padded_version_string(condition_value)
123
+ when '$vne'
124
+ padded_version_string(attribute_value) != padded_version_string(condition_value)
125
+ when '$vgt'
126
+ padded_version_string(attribute_value) > padded_version_string(condition_value)
127
+ when '$vgte'
128
+ padded_version_string(attribute_value) >= padded_version_string(condition_value)
129
+ when '$vlt'
130
+ padded_version_string(attribute_value) < padded_version_string(condition_value)
131
+ when '$vlte'
132
+ padded_version_string(attribute_value) <= padded_version_string(condition_value)
109
133
  when '$eq'
110
- attribute_value == condition_value
134
+ begin
135
+ compare(attribute_value, condition_value).zero?
136
+ rescue StandardError
137
+ false
138
+ end
111
139
  when '$ne'
112
- attribute_value != condition_value
140
+ begin
141
+ compare(attribute_value, condition_value) != 0
142
+ rescue StandardError
143
+ false
144
+ end
113
145
  when '$lt'
114
- attribute_value < condition_value
146
+ begin
147
+ compare(attribute_value, condition_value).negative?
148
+ rescue StandardError
149
+ false
150
+ end
115
151
  when '$lte'
116
- attribute_value <= condition_value
152
+ begin
153
+ compare(attribute_value, condition_value) <= 0
154
+ rescue StandardError
155
+ false
156
+ end
117
157
  when '$gt'
118
- attribute_value > condition_value
158
+ begin
159
+ compare(attribute_value, condition_value).positive?
160
+ rescue StandardError
161
+ false
162
+ end
119
163
  when '$gte'
120
- attribute_value >= condition_value
164
+ begin
165
+ compare(attribute_value, condition_value) >= 0
166
+ rescue StandardError
167
+ false
168
+ end
121
169
  when '$regex'
122
170
  silence_warnings do
123
171
  re = Regexp.new(condition_value)
@@ -126,9 +174,13 @@ module Growthbook
126
174
  false
127
175
  end
128
176
  when '$in'
129
- condition_value.include? attribute_value
177
+ return false unless condition_value.is_a?(Array)
178
+
179
+ in?(attribute_value, condition_value)
130
180
  when '$nin'
131
- !(condition_value.include? attribute_value)
181
+ return false unless condition_value.is_a?(Array)
182
+
183
+ !in?(attribute_value, condition_value)
132
184
  when '$elemMatch'
133
185
  elem_match(condition_value, attribute_value)
134
186
  when '$size'
@@ -162,6 +214,29 @@ module Growthbook
162
214
  end
163
215
  end
164
216
 
217
+ def self.padded_version_string(input)
218
+ # Remove build info and leading `v` if any
219
+ # Split version into parts (both core version numbers and pre-release tags)
220
+ # "v1.2.3-rc.1+build123" -> ["1","2","3","rc","1"]
221
+ parts = input.gsub(/(^v|\+.*$)/, '').split(/[-.]/)
222
+
223
+ # If it's SemVer without a pre-release, add `~` to the end
224
+ # ["1","0","0"] -> ["1","0","0","~"]
225
+ # "~" is the largest ASCII character, so this will make "1.0.0" greater than "1.0.0-beta" for example
226
+ parts << '~' if parts.length == 3
227
+
228
+ # Left pad each numeric part with spaces so string comparisons will work ("9">"10", but " 9"<"10")
229
+ parts.map do |part|
230
+ /^[0-9]+$/.match?(part) ? part.rjust(5, ' ') : part
231
+ end.join('-')
232
+ end
233
+
234
+ def self.in?(actual, expected)
235
+ return expected.include?(actual) unless actual.is_a?(Array)
236
+
237
+ (actual & expected).any?
238
+ end
239
+
165
240
  # Sets $VERBOSE for the duration of the block and back to its original
166
241
  # value afterwards. Used for testing invalid regexes.
167
242
  def self.silence_warnings
@@ -329,7 +329,7 @@ module Growthbook
329
329
  def included_in_rollout?(seed:, hash_attribute:, hash_version:, range:, coverage:)
330
330
  return true if range.nil? && coverage.nil?
331
331
 
332
- hash_value = get_attribute(hash_attribute)
332
+ hash_value = get_attribute(hash_attribute).to_s
333
333
 
334
334
  return false if hash_value.empty?
335
335
 
@@ -344,7 +344,7 @@ module Growthbook
344
344
 
345
345
  def filtered_out?(filters)
346
346
  filters.any? do |filter|
347
- hash_value = get_attribute(filter['attribute'] || 'id')
347
+ hash_value = get_attribute(filter['attribute'] || 'id').to_s
348
348
 
349
349
  if hash_value.empty?
350
350
  false
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: growthbook
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - GrowthBook
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-13 00:00:00.000000000 Z
11
+ date: 2023-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec