optify-config 0.2.2-x86_64-linux → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0463acc530f486215383435fcad1b3f9e793b1f72b54426f12b32b61b4d94fda
4
- data.tar.gz: 79079c3c0f593a78f749ce0aee78118083c721ee4a885bea3217667101546e5d
3
+ metadata.gz: 315776d94257364c8eb04e7903470b47663c3ff67fdcbdc986645e9d1bf2d52e
4
+ data.tar.gz: '09b9c75f58222bc168f4f01cc950688caad14db583792d521a7a79c1b6af0847'
5
5
  SHA512:
6
- metadata.gz: 57f396decf6318a185431e62cf0d229b86e2720cd044f6d4486d88b48c06f24cf519c7fd52ea150d0cc453efadd046b2aec9cf333f0866c1f2bbe2fb0232a273
7
- data.tar.gz: 549f8a51fff4b97f8a6c7ae29fa6646c2524ebe80c75c2601132395733815f88756d060c165c460a7c8a3f52392028d43d80035e8d80be1fb06745098776c185
6
+ metadata.gz: ecb046e5c15cfdf8cc9a426f3c5fc31e24929386c951322fccdee3caa34afb54725136126934b2cf0380d22763bc6eb572eedef765af60bb7a64619e8f3d3894
7
+ data.tar.gz: b980f7c51edbd985c75e84b41900dab0e9544753a08e9aa07314c487eff7bff615df8e8cc2ce71750e2157808239487b32fe979c43f142274e0aea0b45b634fb
data/lib/optify.rb CHANGED
@@ -2,10 +2,12 @@
2
2
  # typed: strict
3
3
 
4
4
  # The implementation to use directly Ruby and with types declared.
5
- require_relative "optify_ruby/implementation"
6
-
5
+ require_relative 'optify_ruby/implementation'
7
6
 
8
7
  # The implementation in Rust which redefines some methods.
9
8
  # This yields some warnings, but we should redeclare the methods in Ruby to help with type checking anyway.
10
- # Warnings about redefining methods are normal and can be ignored because the implementations in Ruby are not implemented and only exist to help with type checking.
11
- require_relative "optify_ruby/optify_ruby"
9
+ # Warnings about redefining methods are normal and can be ignored
10
+ # because the implementations in Ruby are not implemented and only exist to help with type checking.
11
+ require_relative 'optify_ruby/optify_ruby'
12
+
13
+ require_relative 'optify_ruby/base_config'
@@ -0,0 +1,48 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require 'sorbet-runtime'
5
+ require 'tapioca'
6
+
7
+ module Optify
8
+ # A base class for classes from configuration files.
9
+ # Classes that derive from this can easily be used with `Optify::OptionsProvider.get_options`
10
+ # because they will have an implementation of `from_hash` that works recursively.
11
+ # This class is a work in progress with minimal error handling
12
+ # and doesn't handle certain cases such as nilable types yet.
13
+ # It may be moved to another gem in the future.
14
+ class BaseConfig
15
+ extend T::Sig
16
+ extend T::Helpers
17
+ abstract!
18
+
19
+ # Create a new instance of the class from a hash.
20
+ #
21
+ # @param hash [Hash] The hash to create the instance from.
22
+ # @return The new instance.
23
+ sig { params(hash: T::Hash[T.untyped, T.untyped]).returns(T.attached_class) }
24
+ def self.from_hash(hash)
25
+ result = new
26
+
27
+ hash.each do |key, value|
28
+ # TODO: Might need some error handling here, but it should be fine if type signatures are used.
29
+ # TODO Handle nilable types.
30
+ case value
31
+ when Array
32
+ sig_return_type = T::Utils.signature_for_method(instance_method(key)).return_type
33
+ inner_type = sig_return_type.type.raw_type
34
+ value = value.map { |v| inner_type.from_hash(v) } if inner_type.respond_to?(:from_hash)
35
+ when Hash
36
+ sig_return_type = T::Utils.signature_for_method(instance_method(key)).return_type
37
+ if sig_return_type.respond_to?(:raw_type)
38
+ type_for_key = sig_return_type.raw_type
39
+ value = type_for_key.from_hash(value) if type_for_key.respond_to?(:from_hash)
40
+ end
41
+ end
42
+
43
+ result.instance_variable_set("@#{key}", value)
44
+ end
45
+ result
46
+ end
47
+ end
48
+ end
@@ -27,11 +27,26 @@ module Optify
27
27
  #
28
28
  # @param key [String] the key to fetch options for.
29
29
  # @param feature_names [Array<String>] The enabled feature names to use to build the options.
30
- # @return [OpenStruct] the options.
31
- sig { params(key: String, feature_names: T::Array[String]).returns(OpenStruct) }
32
- def get_options(key, feature_names)
30
+ # @param config_class [ConfigType] The class of the configuration to return.
31
+ # It is recommended to use a class that extends `Optify::BaseConfig` because it implements `from_hash`.
32
+ # @return [ConfigType] The options.
33
+ sig do
34
+ type_parameters(:Config)
35
+ .params(
36
+ key: String,
37
+ feature_names: T::Array[String],
38
+ config_class: T::Class[T.type_parameter(:Config)]
39
+ )
40
+ .returns(T.type_parameter(:Config))
41
+ end
42
+ def get_options(key, feature_names, config_class)
33
43
  options_json = get_options_json(key, feature_names)
34
- JSON.parse(options_json, object_class: OpenStruct)
44
+ h = JSON.parse(options_json, object_class: Hash)
45
+ unless config_class.respond_to?(:from_hash)
46
+ raise NotImplementedError, 'The provided config class does not implement to `from_hash`.'
47
+ end
48
+
49
+ T.unsafe(config_class).from_hash(h)
35
50
  end
36
51
  end
37
52
 
@@ -56,4 +71,4 @@ module Optify
56
71
  raise NotImplementedError
57
72
  end
58
73
  end
59
- end
74
+ end
Binary file
data/rbi/optify.rbi CHANGED
@@ -1,16 +1,44 @@
1
- # typed: strict
1
+ # frozen_string_literal: true
2
+ # typed: strong
2
3
 
3
4
  # Tools for working with configurations declared in files.
4
5
  module Optify
6
+ # A base class for classes from configuration files.
7
+ # Classes that derive from this can easily be used with `Optify::OptionsProvider.get_options`
8
+ # because they will have an implementation of `from_hash` that works recursively.
9
+ # This class is a work in progress with minimal error handling
10
+ # and doesn't handle certain cases such as nilable types yet.
11
+ # It may be moved to another gem in the future.
12
+ class BaseConfig
13
+ abstract!
14
+
15
+ # Create a new instance of the class from a hash.
16
+ #
17
+ # @param hash [Hash] The hash to create the instance from.
18
+ # @return The new instance.
19
+ sig { params(hash: T::Hash[T.untyped, T.untyped]).returns(T.attached_class) }
20
+ def self.from_hash(hash); end
21
+ end
22
+
5
23
  # Provides configurations based on keys and enabled feature names.
6
24
  class OptionsProvider
7
25
  # Fetches options based on the provided key and feature names.
8
26
  #
9
27
  # @param key [String] the key to fetch options for.
10
28
  # @param feature_names [Array<String>] The enabled feature names to use to build the options.
11
- # @return [OpenStruct] the options.
12
- sig { params(key: String, feature_names: T::Array[String]).returns(OpenStruct) }
13
- def get_options(key, feature_names); end
29
+ # @param config_class [ConfigType] The class of the configuration to return.
30
+ # It is recommended to use a class that extends `Optify::BaseConfig` because it implements `from_hash`.
31
+ # @return [ConfigType] The options.
32
+ sig do
33
+ type_parameters(:Config)
34
+ .params(
35
+ key: String,
36
+ 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
14
42
 
15
43
  # Fetches options in JSON format based on the provided key and feature names.
16
44
  #
@@ -34,4 +62,4 @@ module Optify
34
62
  sig { returns(OptionsProvider) }
35
63
  def build; end
36
64
  end
37
- end
65
+ end
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.2.2
4
+ version: 0.3.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-04 00:00:00.000000000 Z
11
+ date: 2025-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sorbet-runtime
@@ -80,14 +80,16 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: 3.6.7
83
- description: Simplifies getting the right configuration options for a process using
84
- pre-loaded configurations from files to manage options for experiments or flights.
83
+ description: |-
84
+ Simplifies getting the right configuration options for a process using pre-loaded configurations
85
+ from files to manage options for experiments or flights.
85
86
  email:
86
87
  executables: []
87
88
  extensions: []
88
89
  extra_rdoc_files: []
89
90
  files:
90
91
  - lib/optify.rb
92
+ - lib/optify_ruby/base_config.rb
91
93
  - lib/optify_ruby/implementation.rb
92
94
  - lib/optify_ruby/optify_ruby.so
93
95
  - rbi/optify.rbi