Sha256: cbaead7e9f5a4180afea164f1d3bdbd4232ff947ec809b0799ee198cf3458314

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

# -*- coding: utf-8 -*-
#
# @file 
# @brief
# @author ongaeshi
# @date   2011/02/20

require 'yaml'
require 'milkode/common/dbdir'
require 'milkode/cdstk/milkode_yaml'

module Milkode
  class YamlFileWrapper
    class YAMLAlreadyExist < RuntimeError ; end
    class YAMLNotExist < RuntimeError     ; end

    def self.yaml_file(path)
      Dbdir.yaml_path(path)
    end

    def self.create(path = ".")
      yf = yaml_file(path)
      raise YAMLAlreadyExist.new if FileTest.exist? yf
      obj = YamlFileWrapper.new(yf, MilkodeYaml.new)
      obj.save
      return obj
    end

    def self.load(path = ".")
      yf = yaml_file(path)
      raise YAMLNotExist.new unless FileTest.exist? yf
      open(yf) do |f|
        return YamlFileWrapper.new(yf, MilkodeYaml.new(f.read()))
      end
    end

    def self.load_if(path = ".")
      begin
        load(path)
      rescue YAMLNotExist
        nil
      end
    end

    def initialize(yaml_file, data)
      @yaml_file = yaml_file
      @data = data
      migrate
    end

    def contents
      @data.contents
    end

    def find_name(name)
      @data.find_name(name)
    end

    def find_dir(dir)
      @data.find_dir(dir)
    end

    def add(package)
      @data.add package
    end

    def update(package)
      @data.update package
    end

    def remove(package)
      @data.remove package
    end

    def package_root(dir)
      @data.package_root(dir)
    end

    def save
      open(@yaml_file, "w") { |f| f.write(@data.dump) }
    end

    def version
      @data.version
    end

    def migrate
      if (@data.migrate)
        puts "milkode.yaml is old '#{version}'. Convert to '#{MilkodeYaml::MILKODE_YAML_VERSION}'."
        save
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
milkode-0.5.1 lib/milkode/cdstk/yaml_file_wrapper.rb
milkode-0.5.0 lib/milkode/cdstk/yaml_file_wrapper.rb