Module: Wmiirc::Import

Extended by:
Import
Included in:
Import
Defined in:
lib/wmiirc/import.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) expand(result, src_data, src_file, origins = {}, imported = {})



29
30
31
32
33
34
# File 'lib/wmiirc/import.rb', line 29

def expand result, src_data, src_file, origins={}, imported={}
  to_import = expand_paths src_data['import']
  to_ignore = expand_paths src_data['ignore']
  import result, to_import - to_ignore, origins, imported, src_file
  result
end

- (Object) expand_paths(virtual_paths)



36
37
38
39
40
# File 'lib/wmiirc/import.rb', line 36

def expand_paths virtual_paths
  Array(virtual_paths).flat_map do |virtual_path|
    Dir[File.join(DIR, virtual_path)]
  end
end

- (Object) import(result, paths, origins = {}, imported = {}, importer = $0)



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/wmiirc/import.rb', line 8

def import result, paths, origins={}, imported={}, importer=$0
  Array(paths).each do |path|
    next if imported[path]
    imported[path] = true

    begin
      data = YAML.load_file(path)
    rescue => error
      error.message << ' when importing %s into %s' %
        [path, importer].map(&:inspect)
      raise error
    end

    mark_origin data, path, origins
    expand result, data, path, origins, imported
    merge result, data, path, origins
  end

  result
end

- (Object) merge(dst_hash, src_hash, src_file, origins = {}, backtrace = [])



42
43
44
45
46
47
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
76
77
78
79
80
81
82
# File 'lib/wmiirc/import.rb', line 42

def merge dst_hash, src_hash, src_file, origins={}, backtrace=[]
  src_hash.each do |key, src_val|
    backtrace.push key

    catch :merged do
      if dst_hash.key? key
        dst_val = dst_hash[key]

        dst_file = origins[dst_val]
        section = backtrace.join(':')

        if src_val.nil?
          LOG.warn 'empty section %s in %s removes value %s from %s' %
          [section, src_file, dst_val, dst_file].map(&:inspect)

          dst_hash.delete key
          throw :merged

        elsif dst_val.is_a? Hash and src_val.is_a? Hash
          merge dst_val, src_val, src_file, origins, backtrace
          throw :merged

        elsif dst_val.is_a? Array
          dst_val.concat Array(src_val)
          throw :merged

        else
          LOG.warn 'value %s from %s overrides %s from %s in section %s' %
          [src_val, src_file, dst_val, dst_file, section].map(&:inspect)
          # fall through
        end
      end

      dst_hash[key] = src_val
    end

    backtrace.pop
  end

  dst_hash
end