Skip to content

Commit

Permalink
fix: rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
bgiori committed Jan 1, 2025
1 parent 5056aeb commit b146438
Show file tree
Hide file tree
Showing 11 changed files with 4,111 additions and 4,099 deletions.
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ Naming/FileName:
Exclude:
- lib/amplitude-experiment.rb # Gem name, added for easier Gemfile usage

Naming/AccessorMethodName:
Enabled: false

Naming/MethodParameterName:
Enabled: false

Metrics/ClassLength:
Enabled: false

Expand Down
53 changes: 27 additions & 26 deletions lib/experiment/evaluation/evaluation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,30 @@ def evaluate_flag(target, flag)
result = nil
flag.segments.each do |segment|
result = evaluate_segment(target, flag, segment)
if result
# Merge all metadata into the result
metadata = {}
metadata.merge!(flag.metadata) if flag.metadata
metadata.merge!(segment.metadata) if segment.metadata
metadata.merge!(result.metadata) if result.metadata
result.metadata = metadata
break
end
next unless result

# Merge all metadata into the result
metadata = {}
metadata.merge!(flag.metadata) if flag.metadata
metadata.merge!(segment.metadata) if segment.metadata
metadata.merge!(result.metadata) if result.metadata
result.metadata = metadata
break
end
result
end

def evaluate_segment(target, flag, segment)
if !segment.conditions
# Null conditions always match
variant_key = bucket(target, segment)
variant_key ? flag.variants[variant_key] : nil
else
if segment.conditions
match = evaluate_conditions(target, segment.conditions)
if match
variant_key = bucket(target, segment)
variant_key ? flag.variants[variant_key] : nil
end
else
# Null conditions always match
variant_key = bucket(target, segment)
variant_key ? flag.variants[variant_key] : nil
end
end

Expand All @@ -71,6 +71,7 @@ def match_condition(target, condition)
elsif set_operator?(condition.op)
prop_value_string_list = coerce_string_array(prop_value)
return false unless prop_value_string_list

match_set(prop_value_string_list, condition.op, condition.values)
else
prop_value_string = coerce_string(prop_value)
Expand All @@ -87,7 +88,7 @@ def get_hash(key)
end

def bucket(target, segment)
if !segment.bucket
unless segment.bucket
# Null bucket means segment is fully rolled out
return segment.variant
end
Expand All @@ -106,14 +107,12 @@ def bucket(target, segment)
segment.bucket.allocations.each do |allocation|
allocation_start = allocation.range[0]
allocation_end = allocation.range[1]
if allocation_value >= allocation_start && allocation_value < allocation_end
allocation.distributions.each do |distribution|
distribution_start = distribution.range[0]
distribution_end = distribution.range[1]
if distribution_value >= distribution_start && distribution_value < distribution_end
return distribution.variant
end
end
next unless allocation_value >= allocation_start && allocation_value < allocation_end

allocation.distributions.each do |distribution|
distribution_start = distribution.range[0]
distribution_end = distribution.range[1]
return distribution.variant if distribution_value >= distribution_start && distribution_value < distribution_end
end
end

Expand Down Expand Up @@ -189,7 +188,7 @@ def match_string(prop_value, op, filter_values)
def matches_is?(prop_value, filter_values)
if contains_booleans?(filter_values)
lower = prop_value.downcase
return filter_values.any? { |value| value.downcase == lower } if ['true', 'false'].include?(lower)
return filter_values.any? { |value| value.downcase == lower } if %w[true false].include?(lower)
end
filter_values.any? { |value| prop_value == value }
end
Expand All @@ -203,8 +202,8 @@ def matches_contains?(prop_value, filter_values)
def matches_comparable?(prop_value, op, filter_values, type_transformer, type_comparator)
prop_value_transformed = type_transformer.call(prop_value)
filter_values_transformed = filter_values
.map { |filter_value| type_transformer.call(filter_value) }
.compact
.map { |filter_value| type_transformer.call(filter_value) }
.compact

if !prop_value_transformed || filter_values_transformed.empty?
filter_values.any? { |filter_value| comparator(prop_value, op, filter_value) }
Expand Down Expand Up @@ -258,6 +257,7 @@ def parse_number(value)
def coerce_string(value)
return nil if value.nil?
return value.to_json if value.is_a?(Hash)

value.to_s
end

Expand Down Expand Up @@ -300,6 +300,7 @@ def set_equals?(xa, ya)

def matches_set_contains_all?(prop_values, filter_values)
return false if prop_values.length < filter_values.length

filter_values.all? { |filter_value| matches_is?(filter_value, prop_values) }
end

Expand Down
6 changes: 3 additions & 3 deletions lib/experiment/evaluation/murmur3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ def rotate_left(x, n, width = 32)

def read_int_le(data, index = 0)
n = (data[index] << 24) |
(data[index + 1] << 16) |
(data[index + 2] << 8) |
data[index + 3]
(data[index + 1] << 16) |
(data[index + 2] << 8) |
data[index + 3]
reverse_bytes(n)
end

Expand Down
1 change: 1 addition & 0 deletions lib/experiment/evaluation/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def self.select(selectable, selector)

selector.each do |selector_element|
return nil if selector_element.nil? || selectable.nil?

selectable = selectable[selector_element]
end

Expand Down
2 changes: 1 addition & 1 deletion lib/experiment/evaluation/semantic_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class SemanticVersion
MAJOR_MINOR_REGEX = '(\d+)\.(\d+)'
PATCH_REGEX = '(\d+)'
PRERELEASE_REGEX = '(-(([-\w]+\.?)*))?'
VERSION_PATTERN = /^#{MAJOR_MINOR_REGEX}(\.#{PATCH_REGEX}#{PRERELEASE_REGEX})?$/
VERSION_PATTERN = /^#{MAJOR_MINOR_REGEX}(\.#{PATCH_REGEX}#{PRERELEASE_REGEX})?$/.freeze

def initialize(major, minor, patch, pre_release = nil)
@major = major
Expand Down
9 changes: 3 additions & 6 deletions lib/experiment/evaluation/topological_sort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class CycleError < StandardError
attr_accessor :path

def initialize(path)
super("Detected a cycle between flags #{path}")
self.path = path
Expand All @@ -14,7 +15,7 @@ class TopologicalSort
def self.sort(flags, flag_keys = nil)
available = flags.clone
result = []
starting_keys = flag_keys == nil || flag_keys.length == 0 ? flags.keys : flag_keys
starting_keys = flag_keys.nil? || flag_keys.empty? ? flags.keys : flag_keys

starting_keys.each do |flag_key|
traversal = parent_traversal(flag_key, available)
Expand All @@ -24,8 +25,6 @@ def self.sort(flags, flag_keys = nil)
result
end

private

# Perform depth-first traversal of flag dependencies
def self.parent_traversal(flag_key, available, path = [])
flag = available[flag_key]
Expand All @@ -42,9 +41,7 @@ def self.parent_traversal(flag_key, available, path = [])
result = []

flag.dependencies.each do |parent_key|
if path.any? { |p| p == parent_key }
raise CycleError, path
end
raise CycleError, path if path.any? { |p| p == parent_key }

traversal = parent_traversal(parent_key, available, path)
result.concat(traversal) if traversal
Expand Down
1 change: 1 addition & 0 deletions lib/experiment/local/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def initialize(api_key, config = nil)
Logger::INFO
end
raise ArgumentError, 'Experiment API key is empty' if @api_key.nil? || @api_key.empty?

@engine = Evaluation::Engine.new

@assignment_service = nil
Expand Down
32 changes: 16 additions & 16 deletions spec/experiment/deployment/deployment_runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ module AmplitudeExperiment
let(:cohort_id) { '1234' }
before(:each) do
@flag = Evaluation::Flag.from_hash({
'key' => 'flag',
'variants' => {},
'segments' => [
{
'conditions' => [
[
{
'selector' => %w[context user cohort_ids],
'op' => 'set contains any',
'values' => [cohort_id]
}
]
]
}
]
})
'key' => 'flag',
'variants' => {},
'segments' => [
{
'conditions' => [
[
{
'selector' => %w[context user cohort_ids],
'op' => 'set contains any',
'values' => [cohort_id]
}
]
]
}
]
})
end

describe '#start' do
Expand Down
Loading

0 comments on commit b146438

Please sign in to comment.