Skip to content

Commit fd94ea8

Browse files
authored
Merge pull request #229 from prefab-cloud/update-prefab.current-time-implementation
Update prefab.current time implementation
2 parents df6c6c5 + 7001668 commit fd94ea8

File tree

6 files changed

+33
-32
lines changed

6 files changed

+33
-32
lines changed

lib/prefab/context.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,6 @@ def reportable_tree
200200
end
201201

202202
def to_proto(namespace)
203-
prefab_context = {
204-
'current-time' => ConfigValueWrapper.wrap(Prefab::TimeHelpers.now_in_ms)
205-
}
206-
207-
prefab_context['namespace'] = ConfigValueWrapper.wrap(namespace) if namespace&.length&.positive?
208-
209203
reportable_contexts = {}
210204

211205
reportable_tree.each do |ctx|
@@ -217,8 +211,7 @@ def to_proto(namespace)
217211
PrefabProto::ContextSet.new(
218212
contexts: reportable_contexts.map do |name, context|
219213
context.to_proto
220-
end.concat([PrefabProto::Context.new(type: 'prefab',
221-
values: prefab_context)])
214+
end
222215
)
223216
end
224217

lib/prefab/criteria_evaluator.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,7 @@ def HIERARCHICAL_MATCH(criterion, properties)
8686
end
8787

8888
def IN_INT_RANGE(criterion, properties)
89-
value = if criterion.property_name == 'prefab.current-time'
90-
Time.now.utc.to_i * 1000
91-
else
92-
value_from_properties(criterion, properties)
93-
end
94-
89+
value = value_from_properties(criterion, properties)
9590
value && value >= criterion.value_to_match.int_range.start && value < criterion.value_to_match.int_range.end
9691
end
9792

@@ -150,7 +145,14 @@ def PROP_SEMVER_GREATER_THAN(criterion, properties)
150145
end
151146

152147
def value_from_properties(criterion, properties)
153-
criterion.property_name == NAMESPACE_KEY ? @namespace : properties.get(criterion.property_name)
148+
case criterion.property_name
149+
when NAMESPACE_KEY
150+
@namespace
151+
when 'prefab.current-time'
152+
Time.now.utc.to_i * 1000
153+
else
154+
properties.get(criterion.property_name)
155+
end
154156
end
155157

156158
COMPARE_TO_OPERATORS = {

test/support/common_helpers.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ def setup
1111
Prefab::Context.default_context.clear
1212
SemanticLogger.add_appender(io: $logs, filter: Prefab.log_filter)
1313
SemanticLogger.sync!
14-
Timecop.freeze('2023-08-09 15:18:12 -0400')
1514
end
1615

1716
def teardown

test/test_context.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,6 @@ def test_to_proto
141141
"id" => PrefabProto::ConfigValue.new(int: 2),
142142
"name" => PrefabProto::ConfigValue.new(string: "team-name")
143143
}
144-
),
145-
146-
PrefabProto::Context.new(
147-
type: "prefab",
148-
values: {
149-
'current-time' => PrefabProto::ConfigValue.new(int: Prefab::TimeHelpers.now_in_ms),
150-
'namespace' => PrefabProto::ConfigValue.new(string: namespace)
151-
}
152144
)
153145
]
154146
), contexts.to_proto(namespace)
@@ -226,13 +218,6 @@ def test_to_proto_with_parent
226218
"name" => PrefabProto::ConfigValue.new(string: "team-name")
227219
}
228220
),
229-
# via to_proto
230-
PrefabProto::Context.new(
231-
type: "prefab",
232-
values: {
233-
'current-time' => PrefabProto::ConfigValue.new(int: Prefab::TimeHelpers.now_in_ms),
234-
}
235-
)
236221
]
237222
)
238223

test/test_criteria_evaluator.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,28 @@ def test_semver_greater_than
10681068
assert_equal DESIRED_VALUE, evaluator.evaluate(context(user:{version: "3.0.0"})).unwrapped_value
10691069
end
10701070

1071+
def test_date_before_with_current_time
1072+
future_time = Time.now.utc + 3600 # 1 hour in the future
1073+
config = create_prefab_config(
1074+
operator: PrefabProto::Criterion::CriterionOperator::PROP_BEFORE,
1075+
property_name: 'prefab.current-time',
1076+
value_to_match: future_time.iso8601
1077+
)
1078+
evaluator = Prefab::CriteriaEvaluator.new(config, project_env_id: PROJECT_ENV_ID, resolver: nil, base_client: @base_client, namespace: nil)
1079+
assert_equal DESIRED_VALUE, evaluator.evaluate(context({})).unwrapped_value
1080+
end
1081+
1082+
def test_date_after_with_current_time
1083+
past_time = Time.now.utc - 3600 # 1 hour in the past
1084+
config = create_prefab_config(
1085+
operator: PrefabProto::Criterion::CriterionOperator::PROP_AFTER,
1086+
property_name: 'prefab.current-time',
1087+
value_to_match: past_time.iso8601
1088+
)
1089+
evaluator = Prefab::CriteriaEvaluator.new(config, project_env_id: PROJECT_ENV_ID, resolver: nil, base_client: @base_client, namespace: nil)
1090+
assert_equal DESIRED_VALUE, evaluator.evaluate(context({})).unwrapped_value
1091+
end
1092+
10711093
private
10721094

10731095
def string_list(values)

0 commit comments

Comments
 (0)