Sha256: eae91d01f28935bd9e73914f136d45c470128757fa13805cce11c55f7fb42203

Contents?: true

Size: 1.33 KB

Versions: 4

Compression:

Stored size: 1.33 KB

Contents

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

require 'yaml'
require 'pathname'

module CodeStock
  class CdstkYaml
    class YAMLAlreadyExist < RuntimeError
    end
    
    class YAMLNotExist < RuntimeError
    end

    def self.create(path = ".")
      yf = yaml_file(path)
      raise YAMLAlreadyExist.new if FileTest.exist? yf
      obj = CdstkYaml.new(yf, {'contents' => [], 'version' => 0.1})
      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 CdstkYaml.new(yf, YAML.load(f.read()))
      end
    end

    def add(*dirs)
      contents.push(*dirs.map{|v|{"directory" => v}})
      contents.uniq!
    end

    def remove(*dirs)
      dirs.each {|f| contents.delete( {"directory" => f} ) }
    end

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

    def contents
      @data['contents']
    end

    def directorys
      contents.map{|v|v["directory"]}
    end

    def version
      @data['version']
    end

    def list
      directorys
    end

    def self.yaml_file(path)
      (Pathname.new(path) + 'grendb.yaml').to_s
    end

    private

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

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
codestock-0.1.4 lib/cdstk/cdstk_yaml.rb
codestock-0.1.3 lib/cdstk/cdstk_yaml.rb
codestock-0.1.2 lib/cdstk/cdstk_yaml.rb
codestock-0.1.1 lib/cdstk/cdstk_yaml.rb