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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 34993300413ebe4676e7cf80c868d219262334dc
4
- data.tar.gz: 6ab8999f19b39b2d61116afea9bd67f406bc7b94
3
+ metadata.gz: 53a07ec3606692dbdf0cdc7c8250bbacf399097d
4
+ data.tar.gz: f1dc0b2f19e919f1ea6d2d8dcd921320a0fa8643
5
5
  SHA512:
6
- metadata.gz: 1096589dade1789f856a57e3bddd8b05ccf4a89c97965743a530b5f6d92efda5a9b25290c6260d0a66fd436ac57872a002b62f613fe9f07ba9f2668206326005
7
- data.tar.gz: 282ed2538a8ef0c9fbc2deb54581f7402cc89d7805c788ccba37d8a604b486055debaf6ffe214a6d29efe00d649fabd7393da295fed56ae838c3cc613c0ad89f
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
- @master["data"].fetch(key)
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
@@ -1,3 +1,3 @@
1
1
  class YamlMaster
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml_master
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
  - joker1007