king_konf 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b05b410d3b35c4b341e2f10ec7ca684936c692f21a67452197b5e231c25c87f3
4
- data.tar.gz: fc5368013c4f4b73d461c3dfabf43be74f37153208f5e0caa1d152765fb37e61
3
+ metadata.gz: a1627c1be992905aafc33d3af345e53506aedfccfc00fba4c525150addc21709
4
+ data.tar.gz: a96fdbe1265f0f05c05a4cd35e0f2ac48c880b05380971e3112bd81e1a74f7f2
5
5
  SHA512:
6
- metadata.gz: '00248cac6619fde14c3a1cd29f2905aec16bf893cd99cf6b588ecfff14bef42b300296e8cb3d70914ae8df2c3729cfc0294d91e79682b188ea53f7f8c26e7ee2'
7
- data.tar.gz: 38875fec1190771e9e120ad504a9f475bb11a08f437c94e3188a097870cb5e1706bd0df5bbc77c55e7050a3a8fd6e3db1fee0ad9c626f2a236370a94edf7a621
6
+ metadata.gz: d16e6e0f81812138d41deb63a662a804a08b67d7fc4ad3803739c6227dd707d678e9cd7dbf64a5752e2f7c1e707983daf36d474777ed8242b6cbc80f3399ad20
7
+ data.tar.gz: a8d59948190f2b4c26b88ed5c0a24075ca7d53ef5739b7260e1e41885a3ea7bbbc4d53c48b6c8891e3b6c44638aee0555bbb04e7c1bb27334cef67aeb1f4b379
@@ -32,7 +32,7 @@ module KingKonf
32
32
  @variables.values
33
33
  end
34
34
 
35
- %i(boolean integer float string list).each do |type|
35
+ TYPES.each do |type|
36
36
  define_method(type) do |name, default: nil, required: false, allowed_values: nil, **options|
37
37
  description, @desc = @desc, nil
38
38
  variable = Variable.new(
@@ -1,3 +1,5 @@
1
+ require "king_konf/duration_decoder"
2
+
1
3
  module KingKonf
2
4
  module Decoder
3
5
  extend self
@@ -17,6 +19,10 @@ module KingKonf
17
19
  value
18
20
  end
19
21
 
22
+ def symbol(value, **)
23
+ value.to_sym
24
+ end
25
+
20
26
  def list(value, sep: ",", items: :string)
21
27
  value.split(sep).map {|s| public_send(items, s) }
22
28
  end
@@ -30,5 +36,16 @@ module KingKonf
30
36
  def float(value, **)
31
37
  Float(value)
32
38
  end
39
+
40
+ def duration(value, **)
41
+ case value
42
+ when ""
43
+ nil
44
+ when/^\d*$/
45
+ value.to_i
46
+ else
47
+ DurationDecoder.decode(value)
48
+ end
49
+ end
33
50
  end
34
51
  end
@@ -0,0 +1,28 @@
1
+ module KingKonf
2
+ # Decodes specially formatted duration strings.
3
+ module DurationDecoder
4
+ # Either a number or a time unit.
5
+ PART = /(\d+|[wdhms])/
6
+
7
+ # One or more parts, possibly separated by whitespace.
8
+ VALID_DURATION = /^(#{PART}\s*)+$/
9
+
10
+ UNITS = {
11
+ "s" => 1,
12
+ "m" => 60,
13
+ "h" => 60 * 60,
14
+ "d" => 60 * 60 * 24,
15
+ "w" => 60 * 60 * 24 * 7,
16
+ }
17
+
18
+ def self.decode(value)
19
+ if value !~ VALID_DURATION
20
+ raise ConfigError, "#{value.inspect} is not a duration: must be e.g. `1h 30m`"
21
+ end
22
+
23
+ value.scan(PART).flatten.each_slice(2).map {|number, letter|
24
+ number.to_i * UNITS.fetch(letter)
25
+ }.sum
26
+ end
27
+ end
28
+ end
@@ -30,6 +30,7 @@ module KingKonf
30
30
  when :integer then value.is_a?(Integer) || value.nil?
31
31
  when :float then value.is_a?(Float) || value.is_a?(Integer) || value.nil?
32
32
  when :boolean then value == true || value == false
33
+ when :symbol then value.is_a?(Symbol)
33
34
  else raise "invalid type #{@type}"
34
35
  end
35
36
  end
@@ -42,7 +43,7 @@ module KingKonf
42
43
  Decoder.public_send(@type, value, **options)
43
44
  end
44
45
 
45
- %i(boolean integer float string list).each do |type|
46
+ TYPES.each do |type|
46
47
  define_method("#{type}?") do
47
48
  @type == type
48
49
  end
@@ -1,3 +1,3 @@
1
1
  module KingKonf
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/king_konf.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  require "king_konf/version"
2
- require "king_konf/config"
3
2
 
4
3
  module KingKonf
4
+ # Variable value types.
5
+ TYPES = %i(boolean integer float string list symbol duration)
6
+
5
7
  ConfigError = Class.new(StandardError)
6
8
  end
9
+
10
+ require "king_konf/config"
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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Schierbeck
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-02 00:00:00.000000000 Z
11
+ date: 2018-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -87,6 +87,7 @@ files:
87
87
  - lib/king_konf/config.rb
88
88
  - lib/king_konf/config_file_loader.rb
89
89
  - lib/king_konf/decoder.rb
90
+ - lib/king_konf/duration_decoder.rb
90
91
  - lib/king_konf/variable.rb
91
92
  - lib/king_konf/version.rb
92
93
  homepage: https://github.com/dasch/king_konf