Module: Zena::Loader

Defined in:
lib/zena/loader/yaml_loader.rb

Overview

mass yaml loader for ActiveRecord (a single file can contain different classes)

Defined Under Namespace

Classes: YamlLoader

Class Method Summary collapse

Class Method Details

.load_file(filepath) ⇒ Object

Is this used ?

Raises:

  • (Exception)


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
# File 'lib/zena/loader/yaml_loader.rb', line 51

def self.load_file(filepath)
  raise Exception.new("Invalid filepath for loader (#{filepath})") unless ((filepath =~ /.+\.yml$/) && File.exist?(filepath))
  base_objects = {}
  YAML::load_documents( File.open( filepath ) ) do |doc|
    doc.each do |elem|
      list = elem[1].map do |l|
        hash = {}
        l.each_pair do |k, v|
          hash[k.to_sym] = v
        end
        hash
      end
      tbl = elem[0].to_sym
      if base_objects[tbl]
        base_objects[tbl] += list
      else
        base_objects[tbl] = list
      end
    end
  end

  base_objects.each_pair do |tbl, list|
    YamlLoader.set_table(tbl.to_s)
    list.each do |record|
      YamlLoader.create_or_update(record)
    end
  end
end