class YamlOstruct::YamlOstructImpl

YamlOstructImpl class

Attributes

config[R]
deep_merge[RW]
omit_path[RW]
skip_error[RW]

Public Class Methods

new(args = {}) click to toggle source
# 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

Public Instance Methods

delete(key) click to toggle source
# File lib/yaml/yaml_ostruct_impl.rb, line 22
def delete(key)
  @config.delete_field(key)
end
load(dir) click to toggle source
# 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) click to toggle source
# 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 = load_yaml(yaml_file)

    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) click to toggle source
# File lib/yaml/yaml_ostruct_impl.rb, line 69
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 = load_yaml("#{dir}/#{file_name}")

    config.send("#{File.basename(file_name, extension)}=", new_config.to_hashugar)
  end

  config
end
load_yaml(file_name) click to toggle source
# File lib/yaml/yaml_ostruct_impl.rb, line 90
def load_yaml(file_name)
  YAML.load_file(file_name)
rescue StandardError => e
  raise e unless @skip_error
  nil
end
method_missing(method_sym, *args) click to toggle source
# 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