opto 1.5.3 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -7
- data/lib/opto.rb +1 -1
- data/lib/opto/group.rb +30 -2
- data/lib/opto/option.rb +6 -26
- data/lib/opto/types/array.rb +74 -0
- data/lib/opto/types/enum.rb +3 -3
- data/lib/opto/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 622c74b8329576a787113264f93f0cc5c8341aac
|
4
|
+
data.tar.gz: 1d47bf107b35bb43ead69de0c351b6971ef096ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb0e8e17ccd405b4b4804a2f94cc7f81897e5b6c73ddc5442d6c758c86f7df96abca6800e09216619668abb0f5ec0cf3a207649d425bc6956da01f7b31233f10
|
7
|
+
data.tar.gz: 0d53fca76c0ed5ff96fc3587d9cd1393dda7ed5a891a25bf1f1a399edb28a10ebba7f4f2ded8be94ddfa9fe7e8d143104252f409603c2e7f1470c343374e4194
|
data/README.md
CHANGED
@@ -92,7 +92,7 @@ Simple so far. Now let's mix in "resolvers" which can fetch the value from a num
|
|
92
92
|
# Generate random strings
|
93
93
|
vault_iv:
|
94
94
|
type: string
|
95
|
-
from:
|
95
|
+
from:
|
96
96
|
random_string:
|
97
97
|
length: 64
|
98
98
|
charset: ascii_printable # Other charsets include hex, hex_upcase, alphanumeric, etc.
|
@@ -116,7 +116,7 @@ Simple so far. Now let's mix in "resolvers" which can fetch the value from a num
|
|
116
116
|
env: FOOFOO
|
117
117
|
file: # if env is not set, try to read it from this file, returns nil if not readable
|
118
118
|
path: /tmp/aws_secret.txt
|
119
|
-
ignore_errors: true
|
119
|
+
ignore_errors: true
|
120
120
|
random_string: 30 # not there either, generate a random string.
|
121
121
|
```
|
122
122
|
|
@@ -151,10 +151,10 @@ There's also rudimentary conditional support:
|
|
151
151
|
```
|
152
152
|
|
153
153
|
```ruby
|
154
|
-
group.option('bar').skip?
|
154
|
+
group.option('bar').skip?
|
155
155
|
=> false
|
156
156
|
group.option('foo').value = 'world'
|
157
|
-
group.option('bar').skip?
|
157
|
+
group.option('bar').skip?
|
158
158
|
=> true
|
159
159
|
```
|
160
160
|
|
@@ -167,10 +167,10 @@ There's also rudimentary conditional support:
|
|
167
167
|
|
168
168
|
```ruby
|
169
169
|
group.option('foo').value = 'world'
|
170
|
-
group.option('bar').skip?
|
170
|
+
group.option('bar').skip?
|
171
171
|
=> false
|
172
172
|
group.option('foo').value = 'hello'
|
173
|
-
group.option('bar').skip?
|
173
|
+
group.option('bar').skip?
|
174
174
|
=> true
|
175
175
|
```
|
176
176
|
|
@@ -179,7 +179,7 @@ There's also rudimentary conditional support:
|
|
179
179
|
|
180
180
|
- name: bar
|
181
181
|
type: integer
|
182
|
-
skip_if:
|
182
|
+
skip_if:
|
183
183
|
- foo: hello # AND
|
184
184
|
- baz: world
|
185
185
|
|
@@ -360,6 +360,19 @@ Global validations:
|
|
360
360
|
}
|
361
361
|
```
|
362
362
|
|
363
|
+
### array
|
364
|
+
```ruby
|
365
|
+
{
|
366
|
+
split: ',', # Use this pattern to split an incoming string into an array
|
367
|
+
join: false, # Set to a pattern such as ',' to output a comma separated string
|
368
|
+
empty_is_nil: false, # When true, an empty array will become nil
|
369
|
+
sort: false, # Sort the array before output
|
370
|
+
uniq: false, # Remove duplicates before output
|
371
|
+
count: false, # Instead of outputting the array, output the array size
|
372
|
+
compact: false # Remove nils before output
|
373
|
+
}
|
374
|
+
```
|
375
|
+
|
363
376
|
## Default resolvers
|
364
377
|
Hint is the value that gets passed to the resolver when doing for example: `env: FOO` (FOO is the hint)
|
365
378
|
|
data/lib/opto.rb
CHANGED
data/lib/opto/group.rb
CHANGED
@@ -34,7 +34,7 @@ module Opto
|
|
34
34
|
[]
|
35
35
|
when Hash
|
36
36
|
options.first.map {|k,v| Option.new({name: k.to_s, group: self}.merge(v))}
|
37
|
-
when Array
|
37
|
+
when ::Array
|
38
38
|
options.first.map {|opt| opt.kind_of?(Opto::Option) ? opt : Option.new(opt.merge(group: self)) }
|
39
39
|
else
|
40
40
|
raise TypeError, "Invalid type #{options.first.class} for Opto::Group.new"
|
@@ -107,6 +107,34 @@ module Opto
|
|
107
107
|
opt.nil? ? nil : opt.value
|
108
108
|
end
|
109
109
|
|
110
|
-
|
110
|
+
def any_true?(conditions)
|
111
|
+
normalize_ifs(conditions).any? { |s| s.call(self) == true }
|
112
|
+
end
|
113
|
+
|
114
|
+
def all_true?(conditions)
|
115
|
+
normalize_ifs(conditions).all? { |s| s.call(self) == true }
|
116
|
+
end
|
117
|
+
|
118
|
+
def normalize_ifs(ifs)
|
119
|
+
case ifs
|
120
|
+
when NilClass
|
121
|
+
[]
|
122
|
+
when ::Array
|
123
|
+
ifs.map do |iff|
|
124
|
+
lambda { |grp| !grp.value_of(iff).nil? }
|
125
|
+
end
|
126
|
+
when Hash
|
127
|
+
ifs.each_with_object([]) do |(k, v), arr|
|
128
|
+
arr << lambda { |grp| grp.value_of(k.to_s) == v }
|
129
|
+
end
|
130
|
+
when String, Symbol
|
131
|
+
[lambda { |grp| !grp.value_of(ifs.to_s).nil? }]
|
132
|
+
else
|
133
|
+
raise TypeError, "Invalid syntax for conditional"
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
|
138
|
+
def_delegators :@options, *(::Array.instance_methods - [:__send__, :object_id, :to_h, :to_a, :is_a?, :kind_of?, :instance_of?])
|
111
139
|
end
|
112
140
|
end
|
data/lib/opto/option.rb
CHANGED
@@ -10,7 +10,7 @@ if RUBY_VERSION < '2.1'
|
|
10
10
|
end
|
11
11
|
|
12
12
|
module Opto
|
13
|
-
# What is an option? It's like a variable that has a value, which can be validated or
|
13
|
+
# What is an option? It's like a variable that has a value, which can be validated or
|
14
14
|
# manipulated on creation. The value can be resolved from a number of origins, such as
|
15
15
|
# an environment variable or random string generator.
|
16
16
|
class Option
|
@@ -57,7 +57,7 @@ module Opto
|
|
57
57
|
# description: 'Enter a name for your cat',
|
58
58
|
# from:
|
59
59
|
# env: 'CAT_NAME'
|
60
|
-
# only_if:
|
60
|
+
# only_if:
|
61
61
|
# pet: 'cat'
|
62
62
|
# min_length: 2
|
63
63
|
# max_length: 20
|
@@ -91,8 +91,6 @@ module Opto
|
|
91
91
|
val = opts.delete(:value)
|
92
92
|
@skip_if = opts.delete(:skip_if)
|
93
93
|
@only_if = opts.delete(:only_if)
|
94
|
-
@skip_lambdas = normalize_ifs(@skip_if)
|
95
|
-
@only_lambdas = normalize_ifs(@only_if)
|
96
94
|
@from = normalize_from_to(opts.delete(:from))
|
97
95
|
@to = normalize_from_to(opts.delete(:to))
|
98
96
|
@type_options = opts
|
@@ -149,8 +147,9 @@ module Opto
|
|
149
147
|
# Returns true if this field should not be processed because of the conditionals
|
150
148
|
# @return [Boolean]
|
151
149
|
def skip?
|
152
|
-
return
|
153
|
-
return true
|
150
|
+
return false if group.nil?
|
151
|
+
return true if group.any_true?(skip_if)
|
152
|
+
return true unless group.all_true?(only_if)
|
154
153
|
false
|
155
154
|
end
|
156
155
|
|
@@ -247,28 +246,9 @@ module Opto
|
|
247
246
|
handler.errors
|
248
247
|
end
|
249
248
|
|
250
|
-
def normalize_ifs(ifs)
|
251
|
-
case ifs
|
252
|
-
when NilClass
|
253
|
-
[]
|
254
|
-
when Array
|
255
|
-
ifs.map do |iff|
|
256
|
-
lambda { |opt| !opt.value_of(iff).nil? }
|
257
|
-
end
|
258
|
-
when Hash
|
259
|
-
ifs.each_with_object([]) do |(k, v), arr|
|
260
|
-
arr << lambda { |opt| opt.value_of(k.to_s) == v }
|
261
|
-
end
|
262
|
-
when String, Symbol
|
263
|
-
[lambda { |opt| !opt.value_of(ifs.to_s).nil? }]
|
264
|
-
else
|
265
|
-
raise TypeError, "Invalid syntax for if"
|
266
|
-
end
|
267
|
-
end
|
268
|
-
|
269
249
|
def normalize_from_to(inputs)
|
270
250
|
case inputs
|
271
|
-
when Array
|
251
|
+
when ::Array
|
272
252
|
case inputs.first
|
273
253
|
when String, Symbol
|
274
254
|
inputs.each_with_object({}) { |o, hash| hash[o.to_s.snakecase.to_sym] = name }
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require_relative '../type'
|
2
|
+
if RUBY_VERSION < '2.1'
|
3
|
+
using Opto::Extension::SnakeCase
|
4
|
+
using Opto::Extension::HashStringOrSymbolKey
|
5
|
+
end
|
6
|
+
|
7
|
+
module Opto
|
8
|
+
module Types
|
9
|
+
# An array
|
10
|
+
#
|
11
|
+
# Options:
|
12
|
+
# - split: an incoming string will be split using this pattern
|
13
|
+
# - join: when outputting, join the array using this pattern into a string
|
14
|
+
# - empty_is_nil: an empty array will be replaced with nil
|
15
|
+
# - sort: when true, sorts the array before output
|
16
|
+
# - uniq: when true, removes duplicates
|
17
|
+
# - compact: when true, removes nils and blanks
|
18
|
+
# - count: when true, the output is the count of items in the array
|
19
|
+
class Array < Opto::Type
|
20
|
+
using Opto::Extension::HashStringOrSymbolKey unless RUBY_VERSION < '2.1'
|
21
|
+
|
22
|
+
|
23
|
+
OPTIONS = {
|
24
|
+
split: ',',
|
25
|
+
join: false,
|
26
|
+
empty_is_nil: false,
|
27
|
+
sort: false,
|
28
|
+
uniq: false,
|
29
|
+
count: false,
|
30
|
+
compact: false
|
31
|
+
}
|
32
|
+
|
33
|
+
sanitizer :split do |value|
|
34
|
+
if value.kind_of?(::Array)
|
35
|
+
value
|
36
|
+
elsif value.kind_of?(::String)
|
37
|
+
value.split(options[:split])
|
38
|
+
else
|
39
|
+
[value]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
sanitizer :sort do |value|
|
44
|
+
(value && options[:sort]) ? value.sort : value
|
45
|
+
end
|
46
|
+
|
47
|
+
sanitizer :uniq do |value|
|
48
|
+
(value && options[:uniq]) ? value.uniq : value
|
49
|
+
end
|
50
|
+
|
51
|
+
sanitizer :compact do |value|
|
52
|
+
(value && options[:compact]) ? value.compact : value
|
53
|
+
end
|
54
|
+
|
55
|
+
sanitizer :empty_is_nil do |value|
|
56
|
+
(options[:empty_is_nil] && value.empty?) ? nil : value
|
57
|
+
end
|
58
|
+
|
59
|
+
sanitizer :output do |value|
|
60
|
+
if value
|
61
|
+
if options[:join]
|
62
|
+
value.join(options[:join])
|
63
|
+
elsif options[:count]
|
64
|
+
value.size
|
65
|
+
else
|
66
|
+
value
|
67
|
+
end
|
68
|
+
else
|
69
|
+
nil
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/lib/opto/types/enum.rb
CHANGED
@@ -19,7 +19,7 @@ module Opto
|
|
19
19
|
# Opto::Option.new(
|
20
20
|
# name: 'foo',
|
21
21
|
# type: 'enum',
|
22
|
-
# options:
|
22
|
+
# options:
|
23
23
|
# - foo
|
24
24
|
# - bar
|
25
25
|
# - cat
|
@@ -76,12 +76,12 @@ module Opto
|
|
76
76
|
options.each_with_object([]) do |(key, value), array|
|
77
77
|
array << { value: key, label: key, description: value }
|
78
78
|
end
|
79
|
-
when Array
|
79
|
+
when ::Array
|
80
80
|
case options.first
|
81
81
|
when Hash
|
82
82
|
options.each do |opt|
|
83
83
|
if opt[:value].nil? || opt[:description].nil?
|
84
|
-
raise TypeError, "Option definition requires value and description and can have label when using hash syntax"
|
84
|
+
raise TypeError, "Option definition requires value and description and can have label when using hash syntax"
|
85
85
|
end
|
86
86
|
end
|
87
87
|
options
|
data/lib/opto/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opto
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kimmo Lehto
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -83,6 +83,7 @@ files:
|
|
83
83
|
- lib/opto/setter.rb
|
84
84
|
- lib/opto/setters/environment_variable.rb
|
85
85
|
- lib/opto/type.rb
|
86
|
+
- lib/opto/types/array.rb
|
86
87
|
- lib/opto/types/boolean.rb
|
87
88
|
- lib/opto/types/enum.rb
|
88
89
|
- lib/opto/types/integer.rb
|