yaml_master 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 +4 -4
- data/lib/yaml_master.rb +26 -2
- data/lib/yaml_master/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53a07ec3606692dbdf0cdc7c8250bbacf399097d
|
4
|
+
data.tar.gz: f1dc0b2f19e919f1ea6d2d8dcd921320a0fa8643
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 513e962e3febc9658017f1095a9ce800308b1bcb285f105057419edc32757ecfbb11249a81cb8642e02fd9f3b39d26805cfbb304418f57166a4c005c96339f91
|
7
|
+
data.tar.gz: 5fb39d061b168fba6056db7dcf3aee0063e51f66e38c66b4a1d902ce33778af129795d3884495f718ce2bb6a9869c23d908d1166fc7f416d854b9c0a7e193ea3
|
data/lib/yaml_master.rb
CHANGED
@@ -3,12 +3,19 @@ require "yaml_master/version"
|
|
3
3
|
require "yaml"
|
4
4
|
require "erb"
|
5
5
|
require "pathname"
|
6
|
+
require "pp"
|
6
7
|
|
7
8
|
YAML.add_domain_type(nil, "include") do |type, val|
|
8
9
|
YAML.load_file(val)
|
9
10
|
end
|
10
11
|
|
11
12
|
class YamlMaster
|
13
|
+
class KeyFetchError < StandardError
|
14
|
+
def initialize(data, key)
|
15
|
+
super("cannot fetch key \"#{key}\" from\n#{data.pretty_inspect}")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
12
19
|
attr_reader :master, :master_path
|
13
20
|
|
14
21
|
def initialize(io_or_filename)
|
@@ -27,7 +34,7 @@ class YamlMaster
|
|
27
34
|
raise "data key is necessary on toplevel" unless @master["data"]
|
28
35
|
end
|
29
36
|
|
30
|
-
def generate(key, output, options = {})
|
37
|
+
def generate(key, output = nil, options = {})
|
31
38
|
puts "gen: #{output}" if options[:verbose]
|
32
39
|
yaml = YAML.dump(fetch_data_from_master(key))
|
33
40
|
|
@@ -47,7 +54,24 @@ class YamlMaster
|
|
47
54
|
private
|
48
55
|
|
49
56
|
def fetch_data_from_master(key)
|
50
|
-
|
57
|
+
keys = split_key(key)
|
58
|
+
keys.inject(@master["data"]) do |data, k|
|
59
|
+
data.fetch(k)
|
60
|
+
end
|
61
|
+
rescue
|
62
|
+
raise KeyFetchError.new(@master["data"], key)
|
63
|
+
end
|
64
|
+
|
65
|
+
def split_key(key)
|
66
|
+
keys = key.split(".")
|
67
|
+
array_pattern = /\[(\d+)\]/
|
68
|
+
keys.map do |k|
|
69
|
+
if k.match(array_pattern)
|
70
|
+
Regexp.last_match[1].to_i
|
71
|
+
else
|
72
|
+
k
|
73
|
+
end
|
74
|
+
end
|
51
75
|
end
|
52
76
|
|
53
77
|
class EmbeddedMethods
|
data/lib/yaml_master/version.rb
CHANGED