Sha256: 9b64fdb9690cc8ff9574478f1c6d9a1b97e72d1ea9a91e603ad12e0572ca579d

Contents?: true

Size: 1.99 KB

Versions: 13

Compression:

Stored size: 1.99 KB

Contents

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

require 'yaml'
require 'pathname'
require 'milkode/common/dbdir'

module Milkode
  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 initialize(yaml_file, data)
      @yaml_file = yaml_file
      @data = data
    end

    def add(dirs)
      contents.concat(dirs.map{|v|{'directory' => v}})
      contents.uniq!
    end

    def remove(query)
      r = query.select(contents)
      r.each {|v| contents.delete v}
    end

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

    def contents
      @data['contents']
    end

    def package_num
      @data['contents'].size
    end

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

    def version
      @data['version']
    end

    def list(query = nil)
      query ? query.select(contents) : contents
    end

    def exist?(shortname)
      @data['contents'].find {|v| File.basename(v['directory']) == shortname }   
    end

    def cleanup
      contents.delete_if do |v|
        if (!File.exist? v['directory'])
          yield v if block_given?
          true
        else
          false
        end
      end
    end

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

    class Query
      def initialize(keywords)
        @keywords = keywords
      end

      def select(contents)
        contents.find_all do |v|
          @keywords.any? {|s| File.basename(v['directory']).include? s }
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
milkode-0.2.2 lib/milkode/cdstk/cdstk_yaml.rb
milkode-0.2.1 lib/milkode/cdstk/cdstk_yaml.rb
milkode-0.2.0 lib/milkode/cdstk/cdstk_yaml.rb
milkode-0.1.9 lib/milkode/cdstk/cdstk_yaml.rb
milkode-0.1.8 lib/milkode/cdstk/cdstk_yaml.rb
milkode-0.1.7 lib/milkode/cdstk/cdstk_yaml.rb
milkode-0.1.6 lib/milkode/cdstk/cdstk_yaml.rb
milkode-0.1.5 lib/milkode/cdstk/cdstk_yaml.rb
milkode-0.1.4 lib/milkode/cdstk/cdstk_yaml.rb
milkode-0.1.3 lib/milkode/cdstk/cdstk_yaml.rb
milkode-0.1.2 lib/milkode/cdstk/cdstk_yaml.rb
milkode-0.1.1 lib/milkode/cdstk/cdstk_yaml.rb
milkode-0.1.0 lib/milkode/cdstk/cdstk_yaml.rb