confmaker 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/confoptions.rb +16 -5
- data/lib/confsources.rb +6 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4cb6afaa64af62710ff04a2e4ddffc46ec102a23
|
4
|
+
data.tar.gz: 4ba93a34edf2119d2235c3d79ae2f42762c1b2ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8480e97698069eae0eeaa900cc464483a65546c5f8552f23158c0f04720ab52f458ad304a462f3f898d81b3e9d622c87c23bc7568a782fd0f6f488c67c6b31a7
|
7
|
+
data.tar.gz: 91b736ad29e7b153925389757471a32df77621f56347db96500f80ae0749408581041a4007351c6a24ec3d65e2c0dee3e93b8f6da18d717d511eb83da9beb08d
|
data/lib/confoptions.rb
CHANGED
@@ -1,25 +1,37 @@
|
|
1
1
|
module ConfOptions
|
2
|
+
#Most common option. Only custom validator and getter supports
|
2
3
|
class Standard < Hash
|
3
4
|
def initialize init_hash
|
4
5
|
merge! init_hash
|
5
6
|
end
|
7
|
+
#return [name, value] pair
|
6
8
|
def to_pair
|
7
9
|
[self[:name],self[:value]]
|
8
10
|
end
|
11
|
+
#parse otion into some (hash most likely) by using getter Proc
|
9
12
|
def get
|
10
13
|
if self[:getter].is_a? Proc
|
11
14
|
self[:getter].call self
|
12
|
-
|
15
|
+
elsif (private_methods + methods).include? :default_getter
|
13
16
|
default_getter
|
17
|
+
else
|
18
|
+
fallback_getter
|
14
19
|
end
|
15
20
|
end
|
21
|
+
#human readable representation
|
16
22
|
def to_s
|
17
23
|
get.collect { |k,v|
|
18
24
|
k.to_s.capitalize.gsub('_',' ') + ': ' + v.to_s
|
19
25
|
}.join "\n"
|
20
26
|
end
|
27
|
+
private
|
28
|
+
#getter in noone defined
|
29
|
+
def fallback_getter
|
30
|
+
[to_pair].to_h
|
31
|
+
end
|
21
32
|
end
|
22
33
|
class String < Standard
|
34
|
+
#Validate :value by :validator Proc + default validator for String
|
23
35
|
def validate! context = ConfSources::Default.new
|
24
36
|
self[:validator].call(self,context) if self[:validator].is_a? Proc
|
25
37
|
default_validator if self[:check_regexp]
|
@@ -30,13 +42,15 @@ module ConfOptions
|
|
30
42
|
elsif self[:env_names].is_a? ::Array and self[:check_regexp].is_a? Regexp
|
31
43
|
default_getter
|
32
44
|
else
|
33
|
-
|
45
|
+
fallback_getter
|
34
46
|
end
|
35
47
|
end
|
36
48
|
private
|
49
|
+
#split value into matches by using check_regexp and map it into env_names if both defined
|
37
50
|
def default_getter
|
38
51
|
(self[:env_names].zip (self[:check_regexp].match self[:value]).to_a).to_h
|
39
52
|
end
|
53
|
+
#Basic class and check_regexp checks
|
40
54
|
def default_validator
|
41
55
|
raise RuntimeError, "Option #{self[:name]} become #{self[:value].class} during validation" unless self[:value].is_a? ::String or self[:value].kind_of? Numeric
|
42
56
|
raise ArgumentError, "#{self[:desc]} (#{self[:value].to_s}) must satisfy #{self[:check_regexp].to_s}" unless
|
@@ -48,9 +62,6 @@ module ConfOptions
|
|
48
62
|
default_validator
|
49
63
|
self[:validator].call(self,context) if self[:validator].is_a? Proc
|
50
64
|
end
|
51
|
-
def to_s
|
52
|
-
(self[:env_name] ? self[:env_name] : self[:name] ).to_s.capitalize.gsub('_',' ') + ': ' + self[:value].to_s
|
53
|
-
end
|
54
65
|
private
|
55
66
|
def default_getter
|
56
67
|
{ (self[:env_name] ? self[:env_name] : self[:name] ) => (self[:value] ? "true" : "") }
|
data/lib/confsources.rb
CHANGED
@@ -41,9 +41,15 @@ module ConfSources
|
|
41
41
|
el.validate! Clone.new(self)
|
42
42
|
}
|
43
43
|
end
|
44
|
+
#array of option hashes (including names, desc and so on)
|
44
45
|
def to_a
|
45
46
|
@options.collect{ |opt| opt.to_h }
|
46
47
|
end
|
48
|
+
#hash with parsed values
|
49
|
+
def to_h
|
50
|
+
@options.inject({}){|rez,opt| rez.merge opt.get}
|
51
|
+
end
|
52
|
+
#:name => :value hash
|
47
53
|
def to_pairs
|
48
54
|
@options.collect{ |opt| opt.to_pair }.to_h
|
49
55
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: confmaker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nothing
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-05-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -55,5 +55,5 @@ rubyforge_project:
|
|
55
55
|
rubygems_version: 2.5.2
|
56
56
|
signing_key:
|
57
57
|
specification_version: 4
|
58
|
-
summary:
|
58
|
+
summary: Configuration maker for your utility
|
59
59
|
test_files: []
|