-
Notifications
You must be signed in to change notification settings - Fork 5
/
models.rb
136 lines (113 loc) · 4.54 KB
/
models.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# frozen_string_literal: true
require_relative 'constants'
module Flagsmith
module Engine
# SegmentModel
class Segment
attr_reader :id, :name
attr_accessor :rules, :feature_states
def initialize(id:, name:, rules: [], feature_states: [])
@id = id
@name = name
@rules = rules
@feature_states = feature_states
end
class << self
def build(json)
feature_states = json.fetch(:feature_states, []).map { |fs| Flagsmith::Engine::FeatureState.build(fs) }
rules = json.fetch(:rules, []).map { |rule| Flagsmith::Engine::Segments::Rule.build(rule) }
new(
**json.slice(:id, :name)
.merge(feature_states: feature_states, rules: rules)
)
end
end
end
module Segments
# SegmentConditionModel
class Condition
include Constants
attr_reader :operator, :value, :property
MATCHING_FUNCTIONS = {
EQUAL => ->(other_value, self_value) { other_value == self_value },
GREATER_THAN => ->(other_value, self_value) { other_value > self_value },
GREATER_THAN_INCLUSIVE => ->(other_value, self_value) { other_value >= self_value },
LESS_THAN => ->(other_value, self_value) { other_value < self_value },
LESS_THAN_INCLUSIVE => ->(other_value, self_value) { other_value <= self_value },
NOT_EQUAL => ->(other_value, self_value) { other_value != self_value },
CONTAINS => ->(other_value, self_value) { other_value.include? self_value },
NOT_CONTAINS => ->(other_value, self_value) { !other_value.include? self_value },
REGEX => ->(other_value, self_value) { other_value.match? self_value }
}.freeze
def initialize(operator:, value:, property: nil)
@operator = operator
@value = value
@property = property
end
def match_trait_value?(trait_value)
# handle some exceptions
trait_value = Semantic::Version.new(trait_value.gsub(/:semver$/, '')) if @value.is_a?(String) && @value.match?(/:semver$/)
return match_in_value(trait_value) if @operator == IN
return match_modulo_value(trait_value) if @operator == MODULO
type_as_trait_value = format_to_type_of(trait_value)
formatted_value = type_as_trait_value ? type_as_trait_value.call(@value) : @value
MATCHING_FUNCTIONS[operator]&.call(trait_value, formatted_value)
end
# rubocop:disable Metrics/AbcSize
def format_to_type_of(input)
{
'String' => ->(v) { v.to_s },
'Semantic::Version' => ->(v) { Semantic::Version.new(v.to_s.gsub(/:semver$/, '')) },
'TrueClass' => ->(v) { ['True', 'true', 'TRUE', true, 1, '1'].include?(v) },
'FalseClass' => ->(v) { !['False', 'false', 'FALSE', false, 0, '0'].include?(v) },
'Integer' => ->(v) { v.to_i },
'Float' => ->(v) { v.to_f }
}[input.class.to_s]
end
# rubocop:enable Metrics/AbcSize
def match_modulo_value(trait_value)
divisor, remainder = @value.split('|')
trait_value.is_a?(Numeric) && trait_value % divisor.to_f == remainder.to_f # rubocop:disable Lint/FloatComparison
rescue StandardError
false
end
def match_in_value(trait_value)
return @value.split(',').include?(trait_value.to_s) if trait_value.is_a?(String) || trait_value.is_a?(Integer)
false
end
class << self
def build(json)
new(**json.slice(:operator, :value).merge(property: json[:property_]))
end
end
end
# SegmentRuleModel
class Rule
include Constants
MATCHING_FUNCTIONS = {
ANY_RULE => :any?,
ALL_RULE => :all?,
NONE_RULE => :none?
}.freeze
attr_accessor :type, :rules, :conditions
def initialize(type: nil, rules: [], conditions: [])
@type = type
@rules = rules
@conditions = conditions
end
def matching_function
MATCHING_FUNCTIONS[type]
end
class << self
def build(json)
rules = json.fetch(:rules, []).map { |r| Flagsmith::Engine::Segments::Rule.build(r) }
conditions = json.fetch(:conditions, []).map { |c| Flagsmith::Engine::Segments::Condition.build(c) }
new(
type: json[:type], rules: rules, conditions: conditions
)
end
end
end
end
end
end