growthbook 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2d8ce0f8ecb79cd594b5b6e820410acb3abc3773c8a3012f8cf39df388d94acb
4
- data.tar.gz: 0f9030d6c3db6fdcc7821e28fe18379967ed20aa88661d946ec67c56af3c6ef1
3
+ metadata.gz: 71807a4bbc1fb91e816a74e32d7d6c9e7d1dbed782b19efd3592fd5f5a944801
4
+ data.tar.gz: 03fa6cf71822d7f239afda323e9a28476721d64c6234b199103869207a2cd060
5
5
  SHA512:
6
- metadata.gz: 963c831c370739516b95dcecfb1ba321202a53ab053316f1e715df452b599fb0213b04355b5bd31d3f5c6c5b54100307f5ca2231321e4f04375a68774e8752c2
7
- data.tar.gz: 4858df411e5cf01bf35d08e1ea77bf588f04e7d6b49d99d750f0280c0bc0513486604e574bf580b005c2d0eb703ce52ffe507221b8aa83e184d29123cacac7ed
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
data/lib/growthbook.rb CHANGED
@@ -16,3 +16,4 @@ require 'growthbook/inline_experiment'
16
16
  require 'growthbook/inline_experiment_result'
17
17
  require 'growthbook/tracking_callback'
18
18
  require 'growthbook/util'
19
+ require 'growthbook/feature_usage_callback'
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.0
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