yaml-sugar 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/yaml-sugar/sugar.rb +49 -0
- data/lib/yaml-sugar/version.rb +1 -1
- data/lib/yaml-sugar/yaml-sugar.rb +10 -32
- metadata +2 -1
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'deep_merge'
|
2
|
+
require 'find'
|
3
|
+
require 'hashugar'
|
4
|
+
require 'yaml'
|
5
|
+
require 'ostruct'
|
6
|
+
|
7
|
+
# YamlSugar class
|
8
|
+
module YamlSugar
|
9
|
+
class Sugar
|
10
|
+
attr_reader :config
|
11
|
+
|
12
|
+
def method_missing(method_sym, *_args)
|
13
|
+
if @config.respond_to? method_sym
|
14
|
+
@config.send method_sym
|
15
|
+
else
|
16
|
+
super
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def set(attr, value)
|
21
|
+
self.class.instance_eval do
|
22
|
+
define_method(attr) { value }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def load(dir, args = {})
|
27
|
+
deep_merge = args.fetch :deep_merge, false
|
28
|
+
fail "Parameter #{File.join(Dir.pwd, dir)} is not a valid directory" unless File.directory? dir
|
29
|
+
|
30
|
+
@yaml_files ||= []
|
31
|
+
@config ||= OpenStruct.new
|
32
|
+
Find.find(dir) do |yaml_file|
|
33
|
+
next unless yaml_file =~ /.*\.yml$/ or yaml_file =~ /.*\.yaml$/
|
34
|
+
new_config = YAML.load_file(yaml_file)
|
35
|
+
|
36
|
+
attr_name = File.basename(yaml_file, File.extname(yaml_file)).to_sym
|
37
|
+
if @config.respond_to?(attr_name)
|
38
|
+
old_config = @config.send(attr_name).to_hash
|
39
|
+
if deep_merge
|
40
|
+
new_config = new_config.deep_merge(old_config)
|
41
|
+
else
|
42
|
+
new_config = old_config.merge(new_config)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
@config.send("#{attr_name}=", new_config.to_hashugar)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/yaml-sugar/version.rb
CHANGED
@@ -3,46 +3,24 @@ require 'find'
|
|
3
3
|
require 'hashugar'
|
4
4
|
require 'yaml'
|
5
5
|
require 'ostruct'
|
6
|
+
require 'yaml-sugar/sugar'
|
6
7
|
|
7
|
-
# YamlSugar
|
8
|
+
# YamlSugar module
|
8
9
|
module YamlSugar
|
9
10
|
attr_reader :config
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
if @config.respond_to? method_sym
|
14
|
-
@config.send method_sym
|
15
|
-
else
|
16
|
-
super
|
17
|
-
end
|
12
|
+
def self.new
|
13
|
+
Sugar.new
|
18
14
|
end
|
19
15
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
end
|
16
|
+
# Define on self, since it's a module method
|
17
|
+
def self.method_missing(method_sym, *_args)
|
18
|
+
@config.send method_sym, *_args
|
24
19
|
end
|
25
20
|
|
26
21
|
def self.load(dir, args = {})
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
@yaml_files ||= []
|
31
|
-
@config ||= OpenStruct.new
|
32
|
-
Find.find(dir) do |yaml_file|
|
33
|
-
next unless yaml_file =~ /.*\.yml$/ or yaml_file =~ /.*\.yaml$/
|
34
|
-
new_config = YAML.load_file(yaml_file)
|
35
|
-
|
36
|
-
attr_name = File.basename(yaml_file, File.extname(yaml_file)).to_sym
|
37
|
-
if @config.respond_to?(attr_name)
|
38
|
-
old_config = @config.send(attr_name).to_hash
|
39
|
-
if deep_merge
|
40
|
-
new_config = new_config.deep_merge(old_config)
|
41
|
-
else
|
42
|
-
new_config = old_config.merge(new_config)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
@config.send("#{attr_name}=", new_config.to_hashugar)
|
46
|
-
end
|
22
|
+
@config ||= Sugar.new
|
23
|
+
@config.load(dir, args)
|
47
24
|
end
|
48
25
|
end
|
26
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaml-sugar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -49,6 +49,7 @@ executables: []
|
|
49
49
|
extensions: []
|
50
50
|
extra_rdoc_files: []
|
51
51
|
files:
|
52
|
+
- lib/yaml-sugar/sugar.rb
|
52
53
|
- lib/yaml-sugar/version.rb
|
53
54
|
- lib/yaml-sugar/yaml-sugar.rb
|
54
55
|
- lib/yaml-sugar.rb
|