optify-config 0.4.5-arm64-darwin → 0.5.0-arm64-darwin

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: 8b10cb37278306bd9da1250a3a5958c2165fd14fb7289e64cc49c58f9d3e79f2
4
- data.tar.gz: 96213deb814fb5161074e3fd11995a2cbcd9a4d6e1964906148877b6f431a1d9
3
+ metadata.gz: 47cf7688c8f3c9577071f1341ff980f68dbbd97986819c4f8350a996ba4f5a37
4
+ data.tar.gz: bb291553dc8d1f317e8526c1ddcb8ae15c20830e43625dd830f5cd9440b0b810
5
5
  SHA512:
6
- metadata.gz: b0be294ba5a11d2d26167db9d3f9c05ef7766f5cb3a833e6c6c0fbed04c25eeb333b3df12a35e39aa525716bda6cb9455c6922efc7c974cd35dfe45b69d755b9
7
- data.tar.gz: fd15663c091e83330192ce2e1708f7ee7775ca24a65fa13926ef2cbf0eb00cb031de1e55316b6b3003698a834f836242f91bab2d50b933e9779bca4864fa2a74
6
+ metadata.gz: 1b8ddc42cce41b784eef183b84fb50481b1fe80313c998dd39607580ddfb6ba85181758dbe657597246265276dd72ab9326859cb3660d4ad4402f5c0ab059ff6
7
+ data.tar.gz: 4fbfa6f0613702b023ef4288033c2ebf9464f74d74e98d627d42397a9e33a04a87c2c0f12ddda5c266678ab1a78743025e01d263a94ef17aa10f6e2ee76d9419
Binary file
Binary file
Binary file
@@ -16,7 +16,7 @@ module Optify
16
16
  extend T::Helpers
17
17
  abstract!
18
18
 
19
- # Create a new instance of the class from a hash.
19
+ # Create a new immutable instance of the class from a hash.
20
20
  #
21
21
  # This is a class method that so that it can set members with private setters.
22
22
  # @param hash [Hash] The hash to create the instance from.
@@ -30,7 +30,8 @@ module Optify
30
30
  value = _convert_value(value, sig_return_type)
31
31
  result.instance_variable_set("@#{key}", value)
32
32
  end
33
- result
33
+
34
+ T.unsafe(result).freeze if T.unsafe(result).respond_to?(:freeze)
34
35
  end
35
36
 
36
37
  sig { params(value: T.untyped, type: T.untyped).returns(T.untyped) }
@@ -44,11 +45,11 @@ module Optify
44
45
  # Handle array of 1 type.
45
46
  type.type
46
47
  end
47
- return value.map { |v| _convert_value(v, type) }
48
+ return value.map { |v| _convert_value(v, type) }.freeze
48
49
  when Hash
49
50
  # Handle nilable hash.
50
51
  type = type.unwrap_nilable if type.respond_to?(:unwrap_nilable)
51
- return _convert_hash(value, type)
52
+ return _convert_hash(value, type).freeze
52
53
  end
53
54
 
54
55
  value
@@ -24,7 +24,8 @@ module Optify
24
24
  # @param feature_names [Array<String>] The enabled feature names to use to build the options.
25
25
  # @param config_class [ConfigType] The class of the configuration to return.
26
26
  # It is recommended to use a class that extends `Optify::BaseConfig` because it implements `from_hash`.
27
- # @param cache_options Set this if caching is desired. Only very simple caching is supported for now.
27
+ # @param cache_options [CacheOptions] Set this if caching is desired. Only very simple caching is supported for now.
28
+ # @param preferences [GetOptionsPreferences] The preferences to use when getting options.
28
29
  # @return [ConfigType] The options.
29
30
  sig do
30
31
  type_parameters(:Config)
@@ -32,12 +33,13 @@ module Optify
32
33
  key: String,
33
34
  feature_names: T::Array[String],
34
35
  config_class: T::Class[T.type_parameter(:Config)],
35
- cache_options: T.nilable(CacheOptions)
36
+ cache_options: T.nilable(CacheOptions),
37
+ preferences: T.nilable(Optify::GetOptionsPreferences)
36
38
  )
37
39
  .returns(T.type_parameter(:Config))
38
40
  end
39
- def get_options(key, feature_names, config_class, cache_options = nil)
40
- return get_options_with_cache(key, feature_names, config_class, cache_options) if cache_options
41
+ def get_options(key, feature_names, config_class, cache_options = nil, preferences = nil)
42
+ return get_options_with_cache(key, feature_names, config_class, cache_options, preferences) if cache_options
41
43
 
42
44
  unless config_class.respond_to?(:from_hash)
43
45
  raise NotImplementedError,
@@ -46,11 +48,23 @@ module Optify
46
48
  Recommended: extend `Optify::BaseConfig`."
47
49
  end
48
50
 
49
- options_json = get_options_json(key, feature_names)
51
+ options_json = if preferences
52
+ get_options_json_with_preferences(key, feature_names, preferences)
53
+ else
54
+ get_options_json(key, feature_names)
55
+ end
50
56
  hash = JSON.parse(options_json)
51
57
  T.unsafe(config_class).from_hash(hash)
52
58
  end
53
59
 
60
+ # (Optional) Eagerly initializes the cache.
61
+ # @return [OptionsProvider] `self`.
62
+ sig { returns(OptionsProvider) }
63
+ def init
64
+ @cache = T.let({}, T.nilable(T::Hash[T.untyped, T.untyped]))
65
+ self
66
+ end
67
+
54
68
  private
55
69
 
56
70
  NOT_FOUND_IN_CACHE_SENTINEL = Object.new
@@ -61,25 +75,29 @@ module Optify
61
75
  key: String,
62
76
  feature_names: T::Array[String],
63
77
  config_class: T::Class[T.type_parameter(:Config)],
64
- _cache_options: CacheOptions
78
+ _cache_options: CacheOptions,
79
+ preferences: T.nilable(Optify::GetOptionsPreferences)
65
80
  )
66
81
  .returns(T.type_parameter(:Config))
67
82
  end
68
- def get_options_with_cache(key, feature_names, config_class, _cache_options)
83
+ def get_options_with_cache(key, feature_names, config_class, _cache_options, preferences = nil)
69
84
  # Cache directly in Ruby instead of Rust because:
70
85
  # * Avoid any possible conversion overhead.
71
86
  # * Memory management: probably better to do it in Ruby for a Ruby app and avoid memory in Rust.
72
- # TODO: Consider aliases when caching. Right now, they are only visible in Rust
73
- # and we don't want the cache in Rust because we won't to avoid any conversion overhead.
74
- @cache ||= T.let({}, T.nilable(T::Hash[T.untyped, T.untyped]))
87
+ init unless @cache
88
+ feature_names = feature_names.map do |feature_name|
89
+ get_canonical_feature_name(feature_name)
90
+ end
91
+
75
92
  cache_key = [key, feature_names, config_class]
76
- result = @cache.fetch(cache_key, NOT_FOUND_IN_CACHE_SENTINEL)
93
+ result = @cache&.fetch(cache_key, NOT_FOUND_IN_CACHE_SENTINEL)
77
94
  return result unless result.equal?(NOT_FOUND_IN_CACHE_SENTINEL)
78
95
 
79
- result = get_options(key, feature_names, config_class)
80
- T.unsafe(result).freeze if T.unsafe(result).respond_to?(:freeze)
96
+ preferences ||= GetOptionsPreferences.new
97
+ preferences.skip_feature_name_conversion = true
98
+ result = get_options(key, feature_names, config_class, nil, preferences)
81
99
 
82
- @cache[cache_key] = result
100
+ T.must(@cache)[cache_key] = result
83
101
  end
84
102
  end
85
103
  end
data/rbi/optify.rbi CHANGED
@@ -26,15 +26,32 @@ module Optify
26
26
  class CacheOptions < BaseConfig
27
27
  end
28
28
 
29
+ # Preferences when getting options.
30
+ class GetOptionsPreferences
31
+ sig { params(value: T::Boolean).returns(GetOptionsPreferences) }
32
+ def skip_feature_name_conversion=(value); end
33
+ sig { returns(T::Boolean) }
34
+ def skip_feature_name_conversion; end
35
+ end
36
+
29
37
  # Provides configurations based on keys and enabled feature names.
30
38
  class OptionsProvider
39
+ # Map an alias or canonical feature name (perhaps derived from a file name) to a canonical feature name.
40
+ # Canonical feature names map to themselves.
41
+ #
42
+ # @param feature_name The name of an alias or a feature.
43
+ # @return The canonical feature name.
44
+ sig { params(feature_name: String).returns(String) }
45
+ def get_canonical_feature_name(feature_name); end
46
+
31
47
  # Fetches options based on the provided key and feature names.
32
48
  #
33
49
  # @param key [String] the key to fetch options for.
34
50
  # @param feature_names [Array<String>] The enabled feature names to use to build the options.
35
51
  # @param config_class [ConfigType] The class of the configuration to return.
36
52
  # It is recommended to use a class that extends `Optify::BaseConfig` because it implements `from_hash`.
37
- # @param cache_options Set this if caching is desired. Only very simple caching is supported for now.
53
+ # @param cache_options [CacheOptions] Set this if caching is desired. Only very simple caching is supported for now.
54
+ # @param preferences [GetOptionsPreferences] The preferences to use when getting options.
38
55
  # @return [ConfigType] The options.
39
56
  sig do
40
57
  type_parameters(:Config)
@@ -42,11 +59,12 @@ module Optify
42
59
  key: String,
43
60
  feature_names: T::Array[String],
44
61
  config_class: T::Class[T.type_parameter(:Config)],
45
- cache_options: T.nilable(CacheOptions)
62
+ cache_options: T.nilable(CacheOptions),
63
+ preferences: T.nilable(Optify::GetOptionsPreferences)
46
64
  )
47
65
  .returns(T.type_parameter(:Config))
48
66
  end
49
- def get_options(key, feature_names, config_class, cache_options = nil); end
67
+ def get_options(key, feature_names, config_class, cache_options = nil, preferences = nil); end
50
68
 
51
69
  # Fetches options in JSON format based on the provided key and feature names.
52
70
  #
@@ -55,6 +73,23 @@ module Optify
55
73
  # @return [String] the options in JSON.
56
74
  sig { params(key: String, feature_names: T::Array[String]).returns(String) }
57
75
  def get_options_json(key, feature_names); end
76
+
77
+ # Fetches options in JSON format based on the provided key and feature names.
78
+ #
79
+ # @param key [String] the key to fetch options for.
80
+ # @param feature_names [Array<String>] The enabled feature names to use to build the options.
81
+ # @param preferences [GetOptionsPreferences] The preferences to use when getting options.
82
+ # @return [String] the options in JSON.
83
+ sig do
84
+ params(key: String, feature_names: T::Array[String], preferences: GetOptionsPreferences)
85
+ .returns(String)
86
+ end
87
+ def get_options_json_with_preferences(key, feature_names, preferences); end
88
+
89
+ # (Optional) Eagerly initializes the cache.
90
+ # @return [OptionsProvider] `self`.
91
+ sig { returns(OptionsProvider) }
92
+ def init; end
58
93
  end
59
94
 
60
95
  # A builder for creating an `OptionsProvider` instance.
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.5
4
+ version: 0.5.0
5
5
  platform: arm64-darwin
6
6
  authors:
7
7
  - Justin D. Harris
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-02-10 00:00:00.000000000 Z
11
+ date: 2025-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sorbet-runtime