optify-config 0.3.1-aarch64-linux → 0.4.0-aarch64-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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1ca08576e6486f44d10f34b164024a7b1827d9821b45e73a33eb6ff80689cc90
4
- data.tar.gz: 00403717bb667efee520dc03a1a169d0592fc34216802083d2f71fca1158ba72
3
+ metadata.gz: 80aa089ed4b89d881246babfec79def4e156e0cc3574c49461a1edc0ca4c5d1d
4
+ data.tar.gz: 53862c20e3009adb89e4c3c02856590ed5ec76c1d76977f1f21bb7714e38ae6a
5
5
  SHA512:
6
- metadata.gz: 56f54eb5bcf62da381d6c1397b1858c48228cd270a48a3c33a6c9882fc5f168d3afdbcad163f7069b7a911dbf403188910ce38eaebdf8140e26d5a046f14dd1a
7
- data.tar.gz: 8dfb6107be09d5b942a208be82d9c95bcecd561d41413a722845a939e54bbecde3194dac9803a0542b90e71c1e398e6e8afbbefe7172ea920945cf43ede3fe8d
6
+ metadata.gz: ba979c843fc765e5cfa152c0c3715b1f768c56176898453f99006a9f46a545d70583fbfabb98c94b24f8f7733f181646adc4837aa284536084b6d80d2d392276
7
+ data.tar.gz: 2b09394038fb04536d916ae311c41d626697faed9af92477fd59a9d32384713beff71fce53701de4585c8621d34698b84feb9a43c29dfd5f8d31d017b259ac7f
@@ -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, 'The provided config class does not implement to `from_hash`.'
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
- .returns(T.type_parameter(:Config))
40
- end
41
- def get_options(key, feature_names, config_class); end
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.3.1
4
+ version: 0.4.0
5
5
  platform: aarch64-linux
6
6
  authors:
7
7
  - Justin D. Harris
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-02-07 00:00:00.000000000 Z
11
+ date: 2025-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sorbet-runtime