-
Notifications
You must be signed in to change notification settings - Fork 33
/
split_creation.rb
55 lines (42 loc) · 1.25 KB
/
split_creation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class SplitCreation
include ActiveModel::Model
attr_accessor :app, :name
attr_reader :weighting_registry
validate :split_must_be_valid
def save
split.finished_at = nil
split.reassign_weight(merged_registry) unless split.registry == merged_registry
return false unless valid?
split.save
end
def save!
save || raise(errors.full_messages.to_sentence)
end
def self.create(params)
new(params).tap(&:save)
end
def weighting_registry=(registry)
@weighting_registry = registry.to_h.stringify_keys
end
def split
@split ||= app.splits.create_with(registry: merged_registry, feature_gate: feature_gate?).find_or_initialize_by(name: name)
end
def feature_gate?
name.end_with?("_enabled")
end
private
def zeroed_registry
found_split ? found_split.registry.keys.each_with_object({}) { |split_name, r| r[split_name] = 0 } : {}
end
def merged_registry
@merged_registry ||= zeroed_registry.merge(weighting_registry)
end
def found_split
@found_split ||= app.splits.find_by(name: name)
end
def split_must_be_valid
return if split.valid?
split.errors[:name].each { |e| errors.add(:name, e) }
split.errors[:registry].each { |e| errors.add(:weighting_registry, e) }
end
end