Class: YamlOstruct::YamlOstructImpl
- Inherits:
-
Object
- Object
- YamlOstruct::YamlOstructImpl
- Defined in:
- lib/yaml/yaml_ostruct_impl.rb
Overview
YamlOstructImpl class
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#deep_merge ⇒ Object
Returns the value of attribute deep_merge.
-
#omit_path ⇒ Object
Returns the value of attribute omit_path.
-
#skip_error ⇒ Object
Returns the value of attribute skip_error.
Instance Method Summary collapse
- #delete(key) ⇒ Object
-
#initialize(args = {}) ⇒ YamlOstructImpl
constructor
A new instance of YamlOstructImpl.
- #load(dir) ⇒ Object
- #load_omit_path(dir, config) ⇒ Object
- #load_recursively_with_path(dir, config) ⇒ Object
- #method_missing(method_sym, *args) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ YamlOstructImpl
15 16 17 18 19 20 |
# File 'lib/yaml/yaml_ostruct_impl.rb', line 15 def initialize(args = {}) @config = OpenStruct.new @omit_path = args.fetch :omit_path, false @deep_merge = args.fetch :deep_merge, false @skip_error = args.fetch :skip_error, false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *args) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/yaml/yaml_ostruct_impl.rb', line 26 def method_missing(method_sym, *args) if @config.respond_to? method_sym @config.send method_sym, *args elsif method_sym.to_s.end_with?('=') @config.send method_sym, *args elsif method_sym == :clear || method_sym == :delete_all @config = OpenStruct.new else nil end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
10 11 12 |
# File 'lib/yaml/yaml_ostruct_impl.rb', line 10 def config @config end |
#deep_merge ⇒ Object
Returns the value of attribute deep_merge.
12 13 14 |
# File 'lib/yaml/yaml_ostruct_impl.rb', line 12 def deep_merge @deep_merge end |
#omit_path ⇒ Object
Returns the value of attribute omit_path.
11 12 13 |
# File 'lib/yaml/yaml_ostruct_impl.rb', line 11 def omit_path @omit_path end |
#skip_error ⇒ Object
Returns the value of attribute skip_error.
13 14 15 |
# File 'lib/yaml/yaml_ostruct_impl.rb', line 13 def skip_error @skip_error end |
Instance Method Details
#delete(key) ⇒ Object
22 23 24 |
# File 'lib/yaml/yaml_ostruct_impl.rb', line 22 def delete(key) @config.delete_field(key) end |
#load(dir) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/yaml/yaml_ostruct_impl.rb', line 38 def load(dir) fail "Parameter #{File.join(Dir.pwd, dir)} is not a valid directory" unless File.directory? dir if @omit_path load_omit_path(dir, @config) else load_recursively_with_path(dir, @config) end end |
#load_omit_path(dir, config) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/yaml/yaml_ostruct_impl.rb', line 48 def load_omit_path(dir, config) fail "Parameter #{File.join(Dir.pwd, dir)} is not a valid directory" unless File.directory? dir Find.find(dir) do |yaml_file| next unless yaml_file =~ /.*\.yml$/ or yaml_file =~ /.*\.yaml$/ new_config = begin YAML.load_file(yaml_file) rescue StandardError => e if @skip_error nil else raise e end end attr_name = File.basename(yaml_file, File.extname(yaml_file)).to_sym if config.respond_to?(attr_name) old_config = config.send(attr_name).to_hash new_config = if @deep_merge new_config.deep_merge(old_config) else old_config.merge(new_config) end end config.send("#{attr_name}=", new_config.to_hashugar) end end |
#load_recursively_with_path(dir, config) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/yaml/yaml_ostruct_impl.rb', line 77 def load_recursively_with_path(dir, config) files = Dir.entries(dir) files.each do |file_name| next if file_name.start_with?('.') if File.directory?("#{dir}/#{file_name}") new_config = OpenStruct.new config.send("#{file_name}=", load_recursively_with_path("#{dir}/#{file_name}", new_config)) end extension = File.extname(file_name) next unless extension == '.yml' or extension == '.yaml' new_config = begin YAML.load_file("#{dir}/#{file_name}") rescue StandardError => e if @skip_error nil else raise e end end config.send("#{File.basename(file_name, extension)}=", new_config.to_hashugar) end config end |