king_konf 0.1.6 → 0.1.7

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
  SHA1:
3
- metadata.gz: bbb0ce734f3e5eff7f0c2fca94727d80ca6e9ca8
4
- data.tar.gz: 58e2b1c6539e83090d91be374963bcd1ad2dae6a
3
+ metadata.gz: 9cfc7834fa0e179ec6d7d68fe27c8b449b461145
4
+ data.tar.gz: 5960fea340b02ee4efe9be802b9b0a74154cd289
5
5
  SHA512:
6
- metadata.gz: 9745236ead021ed7741da1dedd6f9fa8db02c09412f9ecff22e0b5f9c90e6e8082b6cd28c41af2ea8f5fd674958a3680216ee21bc50f718074f1a6c1bbf06bb7
7
- data.tar.gz: c4467ce101c3a2b514266f771e7bc035e94eea558873ee8dd0ce1eb0a7808e356fa26b83a74041df02a4b34474fedbd9d87ef2fbcdb53302297ac61e4dd9e997
6
+ metadata.gz: 80bb1349da489d686f787e3107f671fb773653adde8600cd030f73d72d7bacd535422bc1497c2c32102c88dbb4d90b1c99c3c323660097169b2aaa1480c79cff
7
+ data.tar.gz: dc459ac8ac90b3d5ee040946de5fef611b06d35b07446c62184a137688bc4a4371e2c73d5408e4f058dcf699cc5ed694e57f848766a329d692de17991f8a319b
data/README.md CHANGED
@@ -29,10 +29,11 @@ class MyApplication::Config < KingKonf::Config
29
29
  # The prefix is used to identify environment variables. Here, we require
30
30
  # that all environment variables used for config start with `MY_APP_`,
31
31
  # followed by the all caps name of the variable.
32
- prefix :my_app
32
+ env_prefix :my_app
33
33
 
34
- # Strings are the simplest:
35
- string :title
34
+ # Strings are the simplest. This variable is required and *must* be set. By default,
35
+ # a variable is optional.
36
+ string :title, required: true
36
37
 
37
38
  # Integer variables require the value to be a valid integer:
38
39
  integer :score
@@ -67,12 +68,20 @@ ENV["MY_APP_CODES"] = "435;2342;8678"
67
68
 
68
69
  config = MyApplication::Config.new
69
70
 
71
+ # This validates that all required variables have been set, raising
72
+ # KingKonf::ConfigError if one is missing.
73
+ config.validate!
74
+
70
75
  config.title #=> "Hello, World!"
71
76
  config.score #=> 85
72
77
  config.promoted #=> true
73
78
  config.allow_comments #=> false
74
79
  config.tags #=> ["greetings", "introductions", "articles"]
75
80
  config.codes #=> [435, 2342, 8678]
81
+
82
+ # Boolean variables also get a nice query method alias:
83
+ config.promoted? #=> true
84
+ config.allow_comments? #=> false
76
85
  ```
77
86
 
78
87
  If you prefer to use a config file, that's also possible. Simply load a YAML file with `#load_file`:
@@ -6,9 +6,9 @@ module KingKonf
6
6
  @variables = {}
7
7
 
8
8
  class << self
9
- def prefix(prefix = nil)
10
- @prefix = prefix if prefix
11
- @prefix
9
+ def env_prefix(prefix = nil)
10
+ @env_prefix = prefix if prefix
11
+ @env_prefix
12
12
  end
13
13
 
14
14
  def variable(name)
@@ -23,7 +23,7 @@ module KingKonf
23
23
  @variables.values
24
24
  end
25
25
 
26
- %i(boolean integer string list).each do |type|
26
+ %i(boolean integer float string list).each do |type|
27
27
  define_method(type) do |name, default: nil, required: false, **options|
28
28
  description, @desc = @desc, nil
29
29
  variable = Variable.new(
@@ -42,6 +42,10 @@ module KingKonf
42
42
  get(name)
43
43
  end
44
44
 
45
+ if variable.boolean?
46
+ alias_method("#{name}?", name)
47
+ end
48
+
45
49
  define_method("#{name}=") do |value|
46
50
  set(name, value)
47
51
  end
@@ -87,9 +91,9 @@ module KingKonf
87
91
  variable = self.class.variable(name)
88
92
 
89
93
  if variable.valid?(value)
90
- instance_variable_set("@#{name}", value)
94
+ instance_variable_set("@#{name}", variable.cast(value))
91
95
  else
92
- raise ConfigError, "invalid value #{value.inspect}, expected #{variable.type}"
96
+ raise ConfigError, "invalid value #{value.inspect} for variable `#{name}`, expected #{variable.type}"
93
97
  end
94
98
  end
95
99
 
@@ -111,7 +115,7 @@ module KingKonf
111
115
 
112
116
  def load_env(env)
113
117
  loaded_keys = []
114
- prefix = self.class.prefix ? "#{self.class.prefix.upcase}_" : ""
118
+ prefix = self.class.env_prefix ? "#{self.class.env_prefix.upcase}_" : ""
115
119
 
116
120
  self.class.variables.each do |variable|
117
121
  key = prefix + variable.name.upcase.to_s
@@ -124,7 +128,7 @@ module KingKonf
124
128
  end
125
129
 
126
130
  # Only complain about unknown ENV vars if there's a prefix defined.
127
- if self.class.prefix
131
+ if self.class.env_prefix
128
132
  env.keys.grep(/^#{prefix}/).each do |key|
129
133
  unless loaded_keys.include?(key)
130
134
  raise ConfigError, "unknown environment variable #{key}"
@@ -11,6 +11,13 @@ module KingKonf
11
11
  @options = options
12
12
  end
13
13
 
14
+ def cast(value)
15
+ case @type
16
+ when :float then value.to_f
17
+ else value
18
+ end
19
+ end
20
+
14
21
  def required?
15
22
  @required
16
23
  end
@@ -20,6 +27,7 @@ module KingKonf
20
27
  when :string then value.is_a?(String) || value.nil?
21
28
  when :list then value.is_a?(Array)
22
29
  when :integer then value.is_a?(Integer) || value.nil?
30
+ when :float then value.is_a?(Float) || value.is_a?(Integer) || value.nil?
23
31
  when :boolean then value == true || value == false
24
32
  else raise "invalid type #{@type}"
25
33
  end
@@ -29,7 +37,7 @@ module KingKonf
29
37
  Decoder.public_send(@type, value, **options)
30
38
  end
31
39
 
32
- %i(boolean integer string list).each do |type|
40
+ %i(boolean integer float string list).each do |type|
33
41
  define_method("#{type}?") do
34
42
  @type == type
35
43
  end
@@ -1,3 +1,3 @@
1
1
  module KingKonf
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: king_konf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Schierbeck
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-18 00:00:00.000000000 Z
11
+ date: 2017-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler