comff 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/comff.rb +86 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 81febd188a0b78041e46865bd468b8c4171c0cd155b860a0aed6a8c4dbb3bfa2
4
+ data.tar.gz: 1e776c415139797c02e09e3ab8495fb67701cf6fd6272b17c955e6ee105d1abe
5
+ SHA512:
6
+ metadata.gz: e9fa38c550dbf1b57e792d5cf54b5267aca7e516b1fc8ad9f3c9e1590864f67db1d1c442889606d08564e1648a21fac16679834fa3b4ef17252b545bf8f39b74
7
+ data.tar.gz: 62ed95ef28cd91d2376631e9152814e6a9141a589ebe5f207e7f73f005cc1f85e7262e03f35cf27a81a391e71c8f5f15bc0676f89e2ae9dcfc0fd6bbb9b58406
data/lib/comff.rb ADDED
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+ require 'yaml'
3
+
4
+ class Comff
5
+ def initialize(file_content = nil)
6
+ @conf = nil
7
+
8
+ load_conf_file!(file_content) if file_content
9
+ end
10
+
11
+ def get_bool(key, default=nil)
12
+ raw_value = get_str(key)
13
+ return default if raw_value == nil
14
+
15
+ value = raw_value&.to_s&.downcase
16
+
17
+ return true if value == "true"
18
+ return false if value == "false"
19
+
20
+ raise "Invalid value '#{raw_value}' for key #{key}"
21
+ end
22
+
23
+ def get_bool!(key)
24
+ value = get_bool(key)
25
+ raise "Config key #{key} is required" if value == nil
26
+
27
+ value
28
+ end
29
+
30
+ def get_int(key, default=nil)
31
+ value = get_str(key)
32
+ return default if value == nil
33
+ Integer(value)
34
+ end
35
+
36
+ def get_int!(key)
37
+ value = get_int(key)
38
+ raise "Config key #{key} is required" unless value
39
+ Integer(value)
40
+ end
41
+
42
+ def get_str(key, default=nil)
43
+ env_var_name = key_to_env_var_name(key)
44
+ return ENV[env_var_name] if ENV.key?(env_var_name)
45
+
46
+ parts = key.split('.')
47
+ base_obj = if parts.length == 1
48
+ @conf
49
+ else
50
+ @conf.dig(*parts[0...-1])
51
+ end
52
+
53
+ base_obj.fetch(parts.last, default)
54
+ end
55
+
56
+ def get_str!(key)
57
+ none = Object.new
58
+ value = get_str(key, none)
59
+ raise "Config key #{key} is required" if value == none
60
+
61
+ value
62
+ end
63
+
64
+ def self.load_global(file_content)
65
+ @global = Comff.new(file_content)
66
+ end
67
+
68
+ def self.get_str!(*args); @global.get_str!(*args); end
69
+ def self.get_str(*args); @global.get_str(*args); end
70
+ def self.get_int!(*args); @global.get_int!(*args); end
71
+ def self.get_int(*args); @global.get_int(*args); end
72
+ def self.get_bool!(*args); @global.get_bool!(*args); end
73
+ def self.get_bool(*args); @global.get_bool(*args); end
74
+
75
+ private
76
+
77
+ # Convert key format from foo.bar.qux
78
+ # into FOO_BAR_QUX
79
+ def key_to_env_var_name(key)
80
+ key.split('.').map(&:upcase).join('_')
81
+ end
82
+
83
+ def load_conf_file!(file_content)
84
+ @conf = YAML.safe_load(file_content)
85
+ end
86
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: comff
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - André D. Piske
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-07-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ''
14
+ email: andrepiske@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/comff.rb
20
+ homepage: https://github.com/andrepiske/comff
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.2.23
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Configuration loader
43
+ test_files: []