class Tengine::Job::Structure::Category

Public Class Methods

update_for(base_dir) click to toggle source
# File lib/tengine/job/structure/category.rb, line 24
def update_for(base_dir)
  root_dir = File.basename(base_dir)
  dic_dir_base = File.dirname(base_dir)
  root_jobnets = Tengine::Job::Template::RootJobnet.all
  root_jobnets.each do |root_jobnet|
    dirs = File.dirname(root_jobnet.dsl_filepath || "").split('/') - ['.', '..']
    dirs.unshift(root_dir)
    last_category = nil
    dic_dir = dic_dir_base
    dirs.each do |dir|
      caption = nil
      dic_path = File.expand_path("dictionary.yml", dic_dir)
      if File.exist?(dic_path)
        # TODO dictionary.yml が不正な形の場合の処理が必要
        hash = YAML.load_file(dic_path)
        caption = hash[dir]
      end
      category = self.find_or_create_by(
        :name => dir,
        :caption => caption || dir,
        :parent_id => last_category ? last_category.id : nil)
      dic_dir = File.join(dic_dir, dir)
      last_category = category
    end
    if last_category
      root_jobnet.category_id = last_category.id
      root_jobnet.save!
    end
  end

end