yaml-env-tag 0.1.0 → 0.2.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
- SHA1:
3
- metadata.gz: 320980f6b147ebb155338595cf94876182b2e60b
4
- data.tar.gz: d5047c9c8494a2cb074eb7337bfc0f287877b8ec
2
+ SHA256:
3
+ metadata.gz: 2ed0b32ad4e8f65504108d69a1d2374da10f8f2b430b9fac5bf0dbd6e888a074
4
+ data.tar.gz: 7a4ed9894da32fbc6035c98ca178be64aaa2c59a4c307b41950460422ca6857b
5
5
  SHA512:
6
- metadata.gz: c06d3fa7961c410439a962065700acc68abfb404d7de3f57fb12b3def14d9d97bfa1235082ca576268da923adbe9caa19dc611a22eb67f2049aa64e63d7c3d33
7
- data.tar.gz: 7ba5b9c211012b6831acb90a7699e311a5aa014709f504217f24918708280eb20f601da1d451b9dccd19383ba26f4b5d2057fcf2c887ada46b209ce395381bfa
6
+ metadata.gz: 60c7444f8f5b576f3224b93fc1c13a7477aa9fcf2c66a6507da4aef5da618ba1eecd751b32e1e2358f2fde584382035e8a32e6d246f55b6ca1c249fd83cca3d9
7
+ data.tar.gz: 4ded93c275a72f3acb41e4607e664290a002d2a162e8e719cbe74df09ce4a9f88d970b07885ffcb98c5ce73af3297e1d67239b1d1b9d4d4bb3282a7c97bfb02c
@@ -1,4 +1,4 @@
1
- = YAML !ENV tag
1
+ = YAML !ENV Tag
2
2
  Jakub Jirutka <https://github.com/jirutka>
3
3
  // custom
4
4
  :gem-name: yaml-env-tag
@@ -19,8 +19,9 @@ No need to use ERB in YAML just to set some keys from environment variables.
19
19
  [source, yaml]
20
20
  .*Sample YAML file using ENV! tag*
21
21
  oauth:
22
- client_id: ENV! API_CLIENT_ID
23
- client_secret: ENV! API_CLIENT_SECRET
22
+ base_uri: ENV! API_BASE_URI
23
+ client_id: ENV! [API_CLIENT_ID, "demo"]
24
+ client_secret: ENV! [API_CLIENT_SECRET, API_CLIENT_KEY, ~]
24
25
 
25
26
 
26
27
  == Usage
@@ -40,7 +41,40 @@ However, if you use `YAML.safe_load` (which is highly recommended), you need to
40
41
  [source, rb]
41
42
  YAML.safe_load(..., [YamlEnvTag::EnvVariable])
42
43
 
43
- If the specified environment variable does not exist (i.e. is not set), `YAML` will raise exception `YamlEnvTag::MissingEnvVariableError` when loading the file.
44
+
45
+ === Single Required Variable
46
+
47
+ Specify one environment variable as a `!ENV` tagged scalar.
48
+ If it does not exist (is not set), `YAML.load` (and other load methods) will raise `YamlEnvTag::MissingEnvVariableError`.
49
+
50
+ [source, yaml]
51
+ !ENV SOME_VARIABLE
52
+
53
+ This can be also written as a tagged sequence `!ENV [SOME_VARIABLE]` or `!ENV [SOME_VARIABLE, ~]`, all three variants are equivalent.
54
+
55
+
56
+ === Default Value
57
+
58
+ You can define a default value that is used when the specified environment variable does not exist.
59
+ This makes the variable optional.
60
+ Default value is the last element of a `!ENV` tagged sequence (array) with more than one element.
61
+
62
+ [source, yaml]
63
+ !ENV [SOME_VARIABLE, "default value"]
64
+
65
+
66
+ === Multiple Variables (Fallbacks)
67
+
68
+ You may also specify more environment variables in a `!ENV` tagged sequence (array) – the first one that does exist is used.
69
+ Keep in mind that the last element of a multi-element sequence is always interpreted as a default value, not a name of environment variable!
70
+
71
+ [source, yaml]
72
+ !ENV [SOME_VARIABLE, LEGACY_VARIABLE, "default value"]
73
+
74
+ If you want to raise an exception when none of the specified environment variables exist, use `~` (nil) as the last element:
75
+
76
+ [source, yaml]
77
+ !ENV [SOME_VARIABLE, LEGACY_VARIABLE, ~]
44
78
 
45
79
 
46
80
  == License
@@ -8,14 +8,35 @@ module YamlEnvTag
8
8
 
9
9
  # Deserializes from YAML. This method is called by {Psych}.
10
10
  #
11
- # @param coder [Psych::Coder, #scalar] coder with name of the environment
12
- # variable to read.
11
+ # @param coder [Psych::Coder] coder with +:scalar+ or +:seq+.
13
12
  # @raise MissingEnvVariableError if the specified environment variable
14
13
  # is not set (i.e. does not exist in +ENV+).
15
14
  def init_with(coder)
16
- var_name = coder.scalar
17
- value = ::ENV[var_name] or raise MissingEnvVariableError, var_name
18
- initialize(value)
15
+ *variables, default =
16
+ case coder.type
17
+ when :scalar
18
+ [coder.scalar, nil]
19
+ when :seq
20
+ coder.seq.size < 2 ? [*coder.seq, nil] : coder.seq
21
+ else
22
+ raise InvalidUsageError, "#{coder.tag} tag cannot be used on a #{coder.type} node"
23
+ end
24
+
25
+ initialize(variables, default)
26
+ end
27
+
28
+ protected
29
+
30
+ # @param variables [Array<String>] names of the environment variables;
31
+ # value of the first variable that does exist will be used.
32
+ # @param default_value [String, nil] the default value to use when none of
33
+ # the _variables_ exist.
34
+ def initialize(variables, default_value)
35
+ value = variables.lazy.map { |n| ::ENV[n] if n }.find(&:itself)
36
+ value ||= default_value
37
+
38
+ raise MissingEnvVariableError, variables.last if value.nil?
39
+ super(value.to_s)
19
40
  end
20
41
  end
21
42
  end
@@ -1,9 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module YamlEnvTag
4
- # An error class raised when environment variable specified by the `!ENV` tag
4
+ # The base exception class for YamlEnvTag.
5
+ class Error < ::RuntimeError; end
6
+
7
+ # Exception raised when the +!ENV+ tag is used on a wrong node type
8
+ # or on empty node.
9
+ class InvalidUsageError < Error; end
10
+
11
+ # Exception raised when environment variable specified by the +!ENV+ tag
5
12
  # is not set.
6
- class MissingEnvVariableError < ::RuntimeError
13
+ class MissingEnvVariableError < Error
7
14
 
8
15
  attr_reader :variable_name
9
16
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module YamlEnvTag
4
- VERSION = '0.1.0'.freeze
4
+ VERSION = '0.2.0'.freeze
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml-env-tag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakub Jirutka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-05 00:00:00.000000000 Z
11
+ date: 2017-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -100,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  version: '0'
101
101
  requirements: []
102
102
  rubyforge_project:
103
- rubygems_version: 2.6.11
103
+ rubygems_version: 2.7.3
104
104
  signing_key:
105
105
  specification_version: 4
106
106
  summary: Custom YAML tag for referring environment variables in YAML files