fino 1.0.4 → 1.1.0

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: 41afe38eceb38b92f9305cbb4da0f2ab9d0ed503b6e918d09506eff570d0d998
4
- data.tar.gz: c5358f8b1a40dda75057a40369f624179c6bbe93a55190843d1c9ebd7055a31d
3
+ metadata.gz: a2820a64cf64defc88202ae9e9ee7140efe02246c812e33cfe6ce07877f68b9e
4
+ data.tar.gz: 6b2b9c1cf4ae0ac28ad60912e0a5026cf01901b3a6006c03ecf1f5550e3f0c80
5
5
  SHA512:
6
- metadata.gz: b82b39b8c82c79cf715f8d1dbe97de58d78584bcd14a2cc7c745c1855a36cd6c16364fb367448bec57743334ba9444103812ab767618213b8693da1b15fc959c
7
- data.tar.gz: dad45241623885aeb26fd15c7122908c9e2893e73cb07bc66e9482c35bf5cbe1d66645dc5be8bb10e69e61163bac5c9fc255375267d5e1b616b8fbe4dbb8203e
6
+ metadata.gz: 76444316536ac50999142e41abba7dbc4939832a138fb6ad3bf06bf7fb1700b3007f842b874b513789dbeec101ab3c7110818e6f862bd58bd83be06e2cca9251
7
+ data.tar.gz: 3120460c8c7ee45f2be9f38e00dd12150a81e93d36b8bd7a932c16f73b2061e0d0c7c431d50431244cb2e66b9ee4faa11799d96b120716a1a29f0e5b4b68e8b3
data/README.md CHANGED
@@ -56,7 +56,7 @@ Fino.value(:temperature, at: :openai) #=> 0.7
56
56
 
57
57
  Fino.values(:model, :temperature, at: :openai) #=> ["gpt-4", 0.7]
58
58
 
59
- Fino.set("gpt-5", :model, at: :openai)
59
+ Fino.set(model: "gpt-5", at: :openai)
60
60
  Fino.value(:model, at: :openai) #=> "gpt-5"
61
61
  ```
62
62
 
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Fino::Definition::Section
4
+ attr_reader :name, :options
5
+
6
+ def initialize(name:, **options)
7
+ @name = name
8
+ @options = options
9
+ end
10
+
11
+ def label
12
+ options.fetch(:label, name.to_s.capitalize)
13
+ end
14
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Fino::Definition::Setting
4
+ attr_reader :setting_name, :section_definition, :type, :options
5
+
6
+ def initialize(type:, setting_name:, section_definition: nil, **options)
7
+ @setting_name = setting_name
8
+ @section_definition = section_definition
9
+ @type = type
10
+ @options = options
11
+ end
12
+
13
+ def type_class # rubocop:disable Metrics/MethodLength
14
+ @type_class ||=
15
+ case type
16
+ when :string
17
+ Fino::Settings::String
18
+ when :integer
19
+ Fino::Settings::Integer
20
+ when :float
21
+ Fino::Settings::Float
22
+ when :boolean
23
+ Fino::Settings::Boolean
24
+ else
25
+ raise "Unknown type #{type}"
26
+ end
27
+ end
28
+
29
+ def default
30
+ return @default if defined?(@default)
31
+
32
+ @default = options[:default]
33
+ end
34
+
35
+ def description
36
+ options[:description]
37
+ end
38
+
39
+ def path
40
+ @path ||= [setting_name, section_definition&.name].compact
41
+ end
42
+
43
+ def key
44
+ @key ||= path.reverse.join("/")
45
+ end
46
+ end
data/lib/fino/library.rb CHANGED
@@ -19,22 +19,42 @@ class Fino::Library
19
19
  pipeline.read(build_setting_definition(setting_name, at: at))
20
20
  end
21
21
 
22
- def settings(*setting_names, at: nil)
23
- setting_definitions = setting_names.map { |name| build_setting_definition(name, at: at) }
22
+ def settings(*setting_names, at: Fino::EMPTINESS)
23
+ setting_definitions =
24
+ if setting_names.empty? && at.equal?(Fino::EMPTINESS)
25
+ configuration.registry.setting_definitions
26
+ elsif at.equal?(Fino::EMPTINESS)
27
+ setting_names.map { |name| build_setting_definition(name) }
28
+ elsif setting_names.empty?
29
+ configuration.registry.setting_definitions(at: at)
30
+ else
31
+ setting_names.map { |name| build_setting_definition(name, at: at) }
32
+ end
33
+
24
34
  pipeline.read_multi(setting_definitions)
25
35
  end
26
36
 
27
- def all
28
- pipeline.read_multi(configuration.registry.setting_definitions)
37
+ def set(**setting_names_to_values)
38
+ at = setting_names_to_values.delete(:at)
39
+
40
+ setting_names_to_values.each do |setting_name, value|
41
+ setting_definition = build_setting_definition(setting_name, at: at)
42
+
43
+ pipeline.write(
44
+ setting_definition,
45
+ setting_definition.type_class.deserialize(value)
46
+ )
47
+ end
29
48
  end
30
49
 
31
- def set(value, setting_name, at: nil)
32
- setting_definition = build_setting_definition(setting_name, at: at)
50
+ def slice(**mapping)
51
+ setting_definitions = mapping.each_with_object([]) do |(section_name, setting_names), memo|
52
+ Array(setting_names).each do |setting_name|
53
+ memo << build_setting_definition(setting_name, at: section_name)
54
+ end
55
+ end
33
56
 
34
- pipeline.write(
35
- setting_definition,
36
- setting_definition.type_class.deserialize(value)
37
- )
57
+ pipeline.read_multi(setting_definitions)
38
58
  end
39
59
 
40
60
  private
@@ -42,7 +62,7 @@ class Fino::Library
42
62
  attr_reader :configuration
43
63
 
44
64
  def build_setting_definition(setting_name, at: nil)
45
- configuration.registry.fetch(setting_name, at)
65
+ configuration.registry.setting_definition(setting_name.to_s, at&.to_s)
46
66
  end
47
67
 
48
68
  def pipeline
data/lib/fino/pipeline.rb CHANGED
@@ -12,7 +12,7 @@ class Fino::Pipeline
12
12
  end
13
13
 
14
14
  def use(pipe_class, *args, **kwargs, &block)
15
- pipes.unshift ->(pipe) { pipe_class.new(pipe, *args, **kwargs, &block) }
15
+ pipes << ->(pipe) { pipe_class.new(pipe, *args, **kwargs, &block) }
16
16
  @pipeline = nil
17
17
  end
18
18
 
data/lib/fino/registry.rb CHANGED
@@ -3,17 +3,17 @@
3
3
  class Fino::Registry
4
4
  class DSL
5
5
  class SectionDSL
6
- def initialize(section_name, _options, registry)
7
- @section_name = section_name
6
+ def initialize(section_definition, registry)
7
+ @section_definition = section_definition
8
8
  @registry = registry
9
9
  end
10
10
 
11
11
  def setting(setting_name, type, **)
12
12
  @registry.register(
13
- Fino::SettingDefinition.new(
13
+ Fino::Definition::Setting.new(
14
14
  type: type,
15
15
  setting_name: setting_name,
16
- section_name: @section_name,
16
+ section_definition: @section_definition,
17
17
  **
18
18
  )
19
19
  )
@@ -26,7 +26,7 @@ class Fino::Registry
26
26
 
27
27
  def setting(setting_name, type, **)
28
28
  @registry.register(
29
- Fino::SettingDefinition.new(
29
+ Fino::Definition::Setting.new(
30
30
  type: type,
31
31
  setting_name: setting_name,
32
32
  **
@@ -35,7 +35,14 @@ class Fino::Registry
35
35
  end
36
36
 
37
37
  def section(section_name, options = {}, &)
38
- SectionDSL.new(section_name, options, @registry).instance_eval(&)
38
+ section_definition = Fino::Definition::Section.new(
39
+ name: section_name,
40
+ **options
41
+ )
42
+
43
+ @registry.register_section(section_definition)
44
+
45
+ SectionDSL.new(section_definition, @registry).instance_eval(&)
39
46
  end
40
47
  end
41
48
 
@@ -43,22 +50,45 @@ class Fino::Registry
43
50
 
44
51
  using Fino::Ext::Hash
45
52
 
46
- attr_reader :setting_definitions_by_path, :setting_definitions
53
+ attr_reader :setting_definitions, :section_definitions
47
54
 
48
55
  def initialize
49
- @setting_definitions_by_path = {}
50
56
  @setting_definitions = []
57
+ @setting_definitions_by_path = {}
58
+
59
+ @section_definitions = []
60
+ @section_definitions_by_name = {}
51
61
  end
52
62
 
53
- def fetch(*path)
54
- @setting_definitions_by_path.dig(*path.reverse).tap do |definition|
63
+ def setting_definition(*path)
64
+ @setting_definitions_by_path.dig(*path.compact.reverse).tap do |definition|
55
65
  raise UnknownSetting, "Unknown setting: #{path.compact.join('.')}" unless definition
56
66
  end
57
67
  end
58
68
 
69
+ def setting_definitions(at: Fino::EMPTINESS)
70
+ case at
71
+ when Fino::EMPTINESS
72
+ @setting_definitions
73
+ when nil
74
+ @setting_definitions.select { |d| d.section_definition.nil? }
75
+ else
76
+ @setting_definitions_by_path[at&.to_s]&.values || []
77
+ end
78
+ end
79
+
80
+ def section_definition(section_name)
81
+ @section_definitions_by_name[section_name.to_s]
82
+ end
83
+
59
84
  def register(setting_definition)
60
85
  @setting_definitions << setting_definition
61
86
 
62
- @setting_definitions_by_path.deep_set(setting_definition, *setting_definition.path)
87
+ @setting_definitions_by_path.deep_set(setting_definition, *setting_definition.path.map(&:to_s))
88
+ end
89
+
90
+ def register_section(section_definition)
91
+ @section_definitions << section_definition
92
+ @section_definitions_by_name.deep_set(section_definition, section_definition.name.to_s)
63
93
  end
64
94
  end
data/lib/fino/setting.rb CHANGED
@@ -40,17 +40,34 @@ module Fino::Setting
40
40
  definition.key
41
41
  end
42
42
 
43
+ def type
44
+ definition.type
45
+ end
46
+
47
+ def type_class
48
+ definition.type_class
49
+ end
50
+
51
+ def section_definition
52
+ definition.section_definition
53
+ end
54
+
43
55
  def section_name
44
- definition.section_name
56
+ definition.section_definition&.name
45
57
  end
46
58
 
47
59
  def default
48
60
  definition.default
49
61
  end
50
62
 
63
+ def description
64
+ definition.description
65
+ end
66
+
51
67
  def inspect
52
68
  attributes = [
53
69
  "key=#{key.inspect}",
70
+ "type=#{type_class.inspect}",
54
71
  "value=#{value.inspect}",
55
72
  "default=#{default.inspect}"
56
73
  ]
@@ -9,7 +9,10 @@ class Fino::Settings::Boolean
9
9
  end
10
10
 
11
11
  def deserialize(raw_value)
12
- raw_value == "1"
12
+ case raw_value
13
+ when "1", 1, true, "true", "t", "yes", "y" then true
14
+ else false
15
+ end
13
16
  end
14
17
  end
15
18
  end
data/lib/fino/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fino
4
- VERSION = "1.0.4"
4
+ VERSION = "1.1.0"
5
5
  REQUIRED_RUBY_VERSION = ">= 3.0.0"
6
6
  end
data/lib/fino.rb CHANGED
@@ -24,7 +24,7 @@ module Fino
24
24
  :values,
25
25
  :setting,
26
26
  :settings,
27
- :all,
27
+ :slice,
28
28
  :set
29
29
 
30
30
  module_function
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fino
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Egor Iskrenkov
@@ -35,6 +35,8 @@ files:
35
35
  - lib/fino/cache.rb
36
36
  - lib/fino/cache/memory.rb
37
37
  - lib/fino/configuration.rb
38
+ - lib/fino/definition/section.rb
39
+ - lib/fino/definition/setting.rb
38
40
  - lib/fino/error.rb
39
41
  - lib/fino/expirator.rb
40
42
  - lib/fino/ext/hash.rb
@@ -46,7 +48,6 @@ files:
46
48
  - lib/fino/pipeline.rb
47
49
  - lib/fino/registry.rb
48
50
  - lib/fino/setting.rb
49
- - lib/fino/setting_definition.rb
50
51
  - lib/fino/settings/boolean.rb
51
52
  - lib/fino/settings/float.rb
52
53
  - lib/fino/settings/integer.rb
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Fino::SettingDefinition
4
- attr_reader :setting_name, :section_name, :type, :options
5
-
6
- def initialize(type:, setting_name:, section_name: nil, **options)
7
- @setting_name = setting_name
8
- @section_name = section_name
9
- @type = type
10
- @options = options
11
- end
12
-
13
- def type_class
14
- case type
15
- when :string
16
- Fino::Settings::String
17
- when :integer
18
- Fino::Settings::Integer
19
- when :float
20
- Fino::Settings::Float
21
- when :boolean
22
- Fino::Settings::Boolean
23
- else
24
- raise "Unknown type #{type}"
25
- end
26
- end
27
-
28
- def default
29
- options[:default]
30
- end
31
-
32
- def path
33
- @path ||= [setting_name, section_name].compact
34
- end
35
-
36
- def key
37
- @key ||= path.reverse.join("/")
38
- end
39
- end