yaml-ostruct 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/yaml/ostruct.rb CHANGED
@@ -17,8 +17,8 @@ module YamlOstruct
17
17
  @config.send method_sym, *args
18
18
  end
19
19
 
20
- def self.load(dir)
20
+ def self.load(dir, args = {})
21
21
  @config ||= YamlOstructImpl.new
22
- @config.load(dir)
22
+ @config.load(dir, args)
23
23
  end
24
24
  end
data/lib/yaml/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # yaml-ostruct gem version
2
2
  module YamlOstruct
3
- VERSION = '0.1.1'
3
+ VERSION = '0.2.0'
4
4
  end
@@ -2,6 +2,7 @@ require 'find'
2
2
  require 'yaml'
3
3
  require 'ostruct'
4
4
  require 'hashugar'
5
+ require 'deep_merge'
5
6
 
6
7
  module YamlOstruct
7
8
  # YamlOstructImpl class
@@ -25,9 +26,35 @@ module YamlOstruct
25
26
  end
26
27
  end
27
28
 
28
- def load(dir)
29
+ def load(dir, args = {})
29
30
  fail "Parameter #{File.join(Dir.pwd, dir)} is not a valid directory" unless File.directory? dir
30
- load_recursively(dir, @config)
31
+
32
+ if args[:omit_path]
33
+ load_omit_path(dir, @config, args)
34
+ else
35
+ load_recursively(dir, @config)
36
+ end
37
+ end
38
+
39
+ def load_omit_path(dir, config, args)
40
+ deep_merge = args.fetch :deep_merge, false
41
+ fail "Parameter #{File.join(Dir.pwd, dir)} is not a valid directory" unless File.directory? dir
42
+
43
+ Find.find(dir) do |yaml_file|
44
+ next unless yaml_file =~ /.*\.yml$/ or yaml_file =~ /.*\.yaml$/
45
+ new_config = YAML.load_file(yaml_file)
46
+
47
+ attr_name = File.basename(yaml_file, File.extname(yaml_file)).to_sym
48
+ if config.respond_to?(attr_name)
49
+ old_config = config.send(attr_name).to_hash
50
+ new_config = if deep_merge
51
+ new_config.deep_merge(old_config)
52
+ else
53
+ old_config.merge(new_config)
54
+ end
55
+ end
56
+ config.send("#{attr_name}=", new_config.to_hashugar)
57
+ end
31
58
  end
32
59
 
33
60
  def load_recursively(dir, config)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml-ostruct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-05-27 00:00:00.000000000 Z
12
+ date: 2016-06-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hashugar
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
29
  version: '1.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: deep_merge
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.0'
30
46
  description: Read yaml files recursively from a given directory and return an OpenStruct
31
47
  retaining the path
32
48
  email: hex0cter@gmail.com