optify-config 0.3.1-x86_64-linux → 0.4.0-x86_64-linux
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 +4 -4
- data/lib/optify_ruby/implementation.rb +46 -4
- data/rbi/optify.rbi +11 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0cd8b5c2a180c50fb96ddc0de87c5bc202d8284dd3d0841a11983992d8d850e7
|
4
|
+
data.tar.gz: 3a9a083c2cf74d7fd91f4860acc3f78f676a08a4ade0030c911f27b65bd01bb9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e76ab78301cc0369c2994d62cdb57ca92c33e60bee0257b2aa0af3dd2508a26718d00c5d35a6681d0610d037f665eb53e5660ed7ab6194b701086a2f5f484fc2
|
7
|
+
data.tar.gz: 9420836983600d66c1f1f54786cb70e11752f0429a8947ce222e58d245b9dc7c81e48c20baf83a0fbe183b2cf0090c0e9776fe1c82b8e2c1690fe26c91242321
|
@@ -2,12 +2,18 @@
|
|
2
2
|
# typed: strict
|
3
3
|
|
4
4
|
require 'json'
|
5
|
-
require 'ostruct'
|
6
5
|
|
7
6
|
require 'sorbet-runtime'
|
8
7
|
|
8
|
+
require_relative './base_config'
|
9
|
+
|
9
10
|
# Tools for working with configurations declared in files.
|
10
11
|
module Optify
|
12
|
+
# Options for caching.
|
13
|
+
# Only enabling or disabling caching is supported for now.
|
14
|
+
class CacheOptions < BaseConfig
|
15
|
+
end
|
16
|
+
|
11
17
|
# Provides configurations based on keys and enabled feature names.
|
12
18
|
class OptionsProvider
|
13
19
|
extend T::Sig
|
@@ -29,25 +35,61 @@ module Optify
|
|
29
35
|
# @param feature_names [Array<String>] The enabled feature names to use to build the options.
|
30
36
|
# @param config_class [ConfigType] The class of the configuration to return.
|
31
37
|
# It is recommended to use a class that extends `Optify::BaseConfig` because it implements `from_hash`.
|
38
|
+
# @param cache_options Set this if caching is desired. Only very simple caching is supported for now.
|
32
39
|
# @return [ConfigType] The options.
|
33
40
|
sig do
|
34
41
|
type_parameters(:Config)
|
35
42
|
.params(
|
36
43
|
key: String,
|
37
44
|
feature_names: T::Array[String],
|
38
|
-
config_class: T::Class[T.type_parameter(:Config)]
|
45
|
+
config_class: T::Class[T.type_parameter(:Config)],
|
46
|
+
cache_options: T.nilable(CacheOptions)
|
39
47
|
)
|
40
48
|
.returns(T.type_parameter(:Config))
|
41
49
|
end
|
42
|
-
def get_options(key, feature_names, config_class)
|
50
|
+
def get_options(key, feature_names, config_class, cache_options = nil)
|
51
|
+
return get_options_with_cache(key, feature_names, config_class, cache_options) if cache_options
|
52
|
+
|
43
53
|
options_json = get_options_json(key, feature_names)
|
44
54
|
h = JSON.parse(options_json, object_class: Hash)
|
45
55
|
unless config_class.respond_to?(:from_hash)
|
46
|
-
raise NotImplementedError,
|
56
|
+
raise NotImplementedError,
|
57
|
+
"The provided config class must implement `from_hash` as a class method
|
58
|
+
in order to be converted."
|
47
59
|
end
|
48
60
|
|
49
61
|
T.unsafe(config_class).from_hash(h)
|
50
62
|
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
NOT_FOUND_IN_CACHE_SENTINEL = Object.new
|
67
|
+
|
68
|
+
sig do
|
69
|
+
type_parameters(:Config)
|
70
|
+
.params(
|
71
|
+
key: String,
|
72
|
+
feature_names: T::Array[String],
|
73
|
+
config_class: T::Class[T.type_parameter(:Config)],
|
74
|
+
_cache_options: CacheOptions
|
75
|
+
)
|
76
|
+
.returns(T.type_parameter(:Config))
|
77
|
+
end
|
78
|
+
def get_options_with_cache(key, feature_names, config_class, _cache_options)
|
79
|
+
# Cache directly in Ruby instead of Rust because:
|
80
|
+
# * Avoid any possible conversion overhead.
|
81
|
+
# * Memory management: probably better to do it in Ruby for a Ruby app and avoid memory in Rust.
|
82
|
+
# TODO: Handle aliases. Right now, they are only visible in Rust
|
83
|
+
# and we don't want the cache in Rust because we won't to avoid any conversion overhead.
|
84
|
+
@cache ||= T.let({}, T.nilable(T::Hash[T.untyped, T.untyped]))
|
85
|
+
cache_key = [key, feature_names, config_class]
|
86
|
+
result = @cache.fetch(cache_key, NOT_FOUND_IN_CACHE_SENTINEL)
|
87
|
+
return result unless result.equal?(NOT_FOUND_IN_CACHE_SENTINEL)
|
88
|
+
|
89
|
+
result = get_options(key, feature_names, config_class)
|
90
|
+
|
91
|
+
@cache[cache_key] = result
|
92
|
+
end
|
51
93
|
end
|
52
94
|
|
53
95
|
# A builder for creating an `OptionsProvider` instance.
|
data/rbi/optify.rbi
CHANGED
@@ -20,6 +20,11 @@ module Optify
|
|
20
20
|
def self.from_hash(hash); end
|
21
21
|
end
|
22
22
|
|
23
|
+
# Options for caching.
|
24
|
+
# Only enabling or disabling caching is supported for now.
|
25
|
+
class CacheOptions < BaseConfig
|
26
|
+
end
|
27
|
+
|
23
28
|
# Provides configurations based on keys and enabled feature names.
|
24
29
|
class OptionsProvider
|
25
30
|
# Fetches options based on the provided key and feature names.
|
@@ -34,11 +39,12 @@ module Optify
|
|
34
39
|
.params(
|
35
40
|
key: String,
|
36
41
|
feature_names: T::Array[String],
|
37
|
-
config_class: T::Class[T.type_parameter(:Config)]
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
+
config_class: T::Class[T.type_parameter(:Config)],
|
43
|
+
cache_options: T.nilable(CacheOptions)
|
44
|
+
)
|
45
|
+
.returns(T.type_parameter(:Config))
|
46
|
+
end
|
47
|
+
def get_options(key, feature_names, config_class, cache_options = nil); end
|
42
48
|
|
43
49
|
# Fetches options in JSON format based on the provided key and feature names.
|
44
50
|
#
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: optify-config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: x86_64-linux
|
6
6
|
authors:
|
7
7
|
- Justin D. Harris
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-02-
|
11
|
+
date: 2025-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sorbet-runtime
|