Class: YamlOstruct::YamlOstructImpl

Inherits:
Object
  • Object
show all
Defined in:
lib/yaml/yaml_ostruct_impl.rb

Overview

YamlOstructImpl class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ YamlOstructImpl

Returns a new instance of YamlOstructImpl.



14
15
16
17
18
# File 'lib/yaml/yaml_ostruct_impl.rb', line 14

def initialize(args = {})
  @config = OpenStruct.new
  @omit_path = args.fetch :omit_path, false
  @deep_merge = args.fetch :deep_merge, false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/yaml/yaml_ostruct_impl.rb', line 24

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

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/yaml/yaml_ostruct_impl.rb', line 10

def config
  @config
end

#deep_mergeObject

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_pathObject

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

Instance Method Details

#delete(key) ⇒ Object



20
21
22
# File 'lib/yaml/yaml_ostruct_impl.rb', line 20

def delete(key)
  @config.delete_field(key)
end

#load(dir) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/yaml/yaml_ostruct_impl.rb', line 36

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/yaml/yaml_ostruct_impl.rb', line 46

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 = YAML.load_file(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) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/yaml/yaml_ostruct_impl.rb', line 66

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 = YAML.load_file("#{dir}/#{file_name}")
    config.send("#{File.basename(file_name, extension)}=", new_config.to_hashugar)
  end
  config
end